Skip to content

Instantly share code, notes, and snippets.

@thomasdegry
Created March 10, 2015 15:43
Show Gist options
  • Save thomasdegry/fab0354150e6441249eb to your computer and use it in GitHub Desktop.
Save thomasdegry/fab0354150e6441249eb to your computer and use it in GitHub Desktop.
Pan over CAShapeLayers and determine which one is panned on
//
// 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) 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;
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.layer.sublayers count]; i++) {
CAShapeLayer *currentLayer = (CAShapeLayer *)self.layer.sublayers[i];
if (CGPathContainsPoint(currentLayer.path, NULL, point, NO)) {
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 {
[super drawRect: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.layer addSublayer:triangle];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment