Skip to content

Instantly share code, notes, and snippets.

@C4Tutorials
Last active December 16, 2015 10:49
Show Gist options
  • Save C4Tutorials/5423123 to your computer and use it in GitHub Desktop.
Save C4Tutorials/5423123 to your computer and use it in GitHub Desktop.
Advanced Swipes Tutorial
//
// C4WorkSpace.m
// Advanced Swipes Tutorial
//
// Created by Travis Kirton.
//
#import "C4WorkSpace.h"
@implementation C4WorkSpace {
C4Shape *arrow;
}
-(void)setup {
[self createArrow];
for(int i = 1; i < 4; i++) {
NSString *name;
name = [NSString stringWithFormat:@"down%d",i];
[self addGesture:SWIPEDOWN name:name action:@"swipe:"];
[self numberOfTouchesRequired:i forGesture:name];
name = [NSString stringWithFormat:@"left%d",i];
[self addGesture:SWIPELEFT name:name action:@"swipe:"];
[self numberOfTouchesRequired:i forGesture:name];
name = [NSString stringWithFormat:@"up%d",i];
[self addGesture:SWIPEUP name:name action:@"swipe:"];
[self numberOfTouchesRequired:i forGesture:name];
name = [NSString stringWithFormat:@"right%d",i];
[self addGesture:SWIPERIGHT name:name action:@"swipe:"];
[self numberOfTouchesRequired:i forGesture:name];
}
self.canvas.multipleTouchEnabled = YES;
}
-(void)createArrow {
CGMutablePathRef arrowPath = CGPathCreateMutable();
CGPathMoveToPoint(arrowPath, nil, 0, 10);
CGPathAddLineToPoint(arrowPath, nil, 192, 10);
CGPathAddLineToPoint(arrowPath, nil, 187, 0);
CGPathAddLineToPoint(arrowPath, nil, 222, 15);
CGPathAddLineToPoint(arrowPath, nil, 187, 30);
CGPathAddLineToPoint(arrowPath, nil, 192, 20);
CGPathAddLineToPoint(arrowPath, nil, 0, 20);
CGPathCloseSubpath(arrowPath);
arrow = [C4Shape rect:CGRectMake(0, 0, 1, 1)];
arrow.path = arrowPath;
arrow.lineWidth = 0.0f;
}
-(void)swipe:(UISwipeGestureRecognizer *)swipeGesture {
NSInteger touchCount = swipeGesture.numberOfTouches;
CGFloat rotation;
UIColor *arrowColor;
switch (swipeGesture.direction) {
case UISwipeGestureRecognizerDirectionUp:
rotation = -HALF_PI;
arrowColor = C4RED;
break;
case UISwipeGestureRecognizerDirectionLeft:
rotation = PI;
arrowColor = C4BLUE;
break;
case UISwipeGestureRecognizerDirectionDown:
rotation = HALF_PI;
arrowColor = [UIColor colorWithPatternImage:[C4Image imageNamed:@"lines"].UIImage];
break;
default:
rotation = 0;
arrowColor = C4GREY;
break;
}
C4Shape *arrowSet = [self createArrowSet:touchCount rotation:rotation color:arrowColor];
CGPoint touchPoint = [swipeGesture locationInView:self.canvas];
arrowSet.center = touchPoint;
[self.canvas addShape:arrowSet];
[self runMethod:@"fadeOut:" withObject:arrowSet afterDelay:0.25f];
}
-(C4Shape *)createArrowSet:(NSInteger)touchCount rotation:(CGFloat)rotation color:(UIColor *)arrowColor {
CGFloat gap = 5.0f;
CGRect arrowSetFrame = CGRectMake(0, 0, arrow.width, arrow.height * touchCount + gap * (touchCount - 1));
C4Shape *arrowSet = [C4Shape rect:arrowSetFrame];
arrowSet.lineWidth = 0.0f;
arrowSet.fillColor = [UIColor clearColor];
arrowSet.userInteractionEnabled = NO;
for(int i = 0; i < touchCount; i++) {
C4Shape *newArrow = [arrow copy];
newArrow.origin = CGPointMake(0,arrow.height*i + gap * i);
newArrow.fillColor = arrowColor;
[arrowSet addShape:newArrow];
}
arrowSet.anchorPoint = CGPointMake(0,0.5f);
arrowSet.rotation = rotation;
return arrowSet;
}
-(void)fadeOut:(C4Shape *)shape {
shape.animationDuration = 2.0f;
shape.alpha = 0.0f;
[shape runMethod:@"removeFromSuperview" afterDelay:shape.animationDuration];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment