Skip to content

Instantly share code, notes, and snippets.

@thomasdegry
Last active August 29, 2015 14:16
Show Gist options
  • Save thomasdegry/2ef7b5090484e29cf8ed to your computer and use it in GitHub Desktop.
Save thomasdegry/2ef7b5090484e29cf8ed to your computer and use it in GitHub Desktop.
- (void)createTriangles {
UIView *triangleWrapper = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.collectionView.frame.size.width, self.collectionView.frame.size.height)];
[self.collectionView addSubview:triangleWrapper];
CGFloat radius = MIN(self.collectionView.frame.size.width, self.collectionView.frame.size.height) / 2.5;
CGPoint center = triangleWrapper.center;
for (NSInteger i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++) {
CGPoint point1 = CGPointMake(center.x + radius * sinf(2 * (i - 0.5) * M_PI / [self.collectionView numberOfItemsInSection:0]),
center.y + radius * -cosf(2 * (i - 0.5) * M_PI / [self.collectionView numberOfItemsInSection:0]));
CGPoint point2 = CGPointMake(center.x + radius * sinf(2 * (i + 0.5) * M_PI / [self.collectionView numberOfItemsInSection:0]),
center.y + radius * -cosf(2 * (i + 0.5) * M_PI / [self.collectionView numberOfItemsInSection:0]));
UIBezierPath* trianglePath = [UIBezierPath bezierPath];
[trianglePath moveToPoint:CGPointMake(center.x, center.y)];
[trianglePath addLineToPoint:CGPointMake(point1.x, point1.y)];
[trianglePath addLineToPoint:CGPointMake(point2.x, point2.y)];
[trianglePath closePath];
CAShapeLayer *triangleMaskLayer = [CAShapeLayer layer];
[triangleMaskLayer setPath:trianglePath.CGPath];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0, triangleWrapper.frame.size.width, triangleWrapper.frame.size.height)];
CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
UIColor *color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
view.backgroundColor = color;
view.layer.mask = triangleMaskLayer;
view.tag = i;
view.layer.masksToBounds = YES;
[triangleWrapper addSubview:view];
// Pan gesture recognizer
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didPanOnCollectionView:)];
gesture.minimumNumberOfTouches = 1;
gesture.maximumNumberOfTouches = 1;
[view addGestureRecognizer:gesture];
}
- (void)didPanOnCollectionView:(UIPanGestureRecognizer *)gesture {
if (gesture.numberOfTouches > 0) {
CGPoint panPoint = [gesture locationOfTouch:0 inView:self.collectionView];
UIView *sender = gesture.view;
NSLog(@"Panned over view with tag %li", (long)sender.tag);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment