Last active
August 29, 2015 14:16
-
-
Save thomasdegry/aea8e21dd83f8a2b2b41 to your computer and use it in GitHub Desktop.
Determin Which layer was panned
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // TriangleWrapper.m | |
| // dot | |
| // | |
| // Created by LOANER on 3/10/15. | |
| // Copyright (c) 2015 Thomas Degry. All rights reserved. | |
| // | |
| #import "TriangleWrapper.h" | |
| @interface TriangleWrapper() | |
| @property (assign, nonatomic) CGFloat radius; | |
| @property (assign, nonatomic) NSInteger numberOfItems; | |
| @property (strong, nonatomic) CALayer *wrapperLayer; | |
| @property (strong, nonatomic) NSMutableArray *layers; | |
| @end | |
| @implementation TriangleWrapper | |
| - (instancetype)initWithFrame:(CGRect)frame radius:(CGFloat)radius andNumberOfItems:(NSInteger)numberOfItems { | |
| self = [super initWithFrame:frame]; | |
| if (self) { | |
| self.radius = radius; | |
| self.numberOfItems = numberOfItems; | |
| self.layers = [[NSMutableArray alloc] init]; | |
| self.wrapperLayer = [CALayer layer]; | |
| [self.layer addSublayer:self.wrapperLayer]; | |
| UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panned:)]; | |
| gesture.maximumNumberOfTouches = 1; | |
| gesture.minimumNumberOfTouches = 1; | |
| [self addGestureRecognizer:gesture]; | |
| } | |
| return self; | |
| } | |
| - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event { | |
| return YES; | |
| } | |
| - (void)panned:(UIPanGestureRecognizer *)gesture { | |
| CGPoint point = [gesture locationInView:self]; | |
| for (NSInteger i = 0; i < [self.wrapperLayer.sublayers count]; i++) { | |
| CALayer *currentLayer = self.wrapperLayer.sublayers[i]; | |
| if ([currentLayer containsPoint:point]) { | |
| NSLog(@"Swiping over layer with name %@", currentLayer.name); | |
| } | |
| } | |
| } | |
| // Only override drawRect: if you perform custom drawing. | |
| // An empty implementation adversely affects performance during animation. | |
| - (void)drawRect:(CGRect)rect { | |
| CGPoint center = self.center; | |
| for (NSInteger i = 0; i < self.numberOfItems; i++) { | |
| CGPoint point1 = CGPointMake(center.x + self.radius * sinf(2 * (i - 0.5) * M_PI / self.numberOfItems), | |
| center.y + self.radius * -cosf(2 * (i - 0.5) * M_PI / self.numberOfItems)); | |
| CGPoint point2 = CGPointMake(center.x + self.radius * sinf(2 * (i + 0.5) * M_PI / self.numberOfItems), | |
| center.y + self.radius * -cosf(2 * (i + 0.5) * M_PI / self.numberOfItems)); | |
| 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]; | |
| 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]; | |
| CAShapeLayer *triangle = [CAShapeLayer layer]; | |
| triangle.fillColor = color.CGColor; | |
| triangle.name = [NSString stringWithFormat:@"triange%li", (long)i]; | |
| [triangle setPath:trianglePath.CGPath]; | |
| [self.wrapperLayer addSublayer:triangle]; | |
| [self.layers addObject:triangle]; | |
| } | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment