Skip to content

Instantly share code, notes, and snippets.

@C4Tutorials
Last active December 16, 2015 10:39
Show Gist options
  • Save C4Tutorials/5421700 to your computer and use it in GitHub Desktop.
Save C4Tutorials/5421700 to your computer and use it in GitHub Desktop.
Taps & Touches Tutorial
//
// C4WorkSpace.m
// Taps & Touches Tutorial
//
// Created by Travis Kirton.
//
#import "C4WorkSpace.h"
@implementation C4WorkSpace {
C4Label *tapsAndTouches;
C4Timer *resetTimer;
}
-(void)setup {
[self createLabel];
NSInteger tapCount = 5;
NSInteger touchCount = 5;
for(int i = 1; i < tapCount + 1; i++) {
for(int j = 1; j < touchCount + 1; j++) {
NSString *tapName = [@"tap" stringByAppendingFormat:@"%d_%d",i,j];
[self addGesture:TAP name:tapName action:@"tap:"];
[self numberOfTapsRequired:i forGesture:tapName];
[self numberOfTouchesRequired:j forGesture:tapName];
}
}
NSDictionary *allGestures = [self allGestures];
NSArray *gestNames = [allGestures allKeys];
gestNames = [gestNames sortedArrayUsingFunction:strSort context:NULL];
for(int i = 0; i < [gestNames count]; i++) {
UIGestureRecognizer *g = [self gestureForName:gestNames[i]];
for(int j = i+1; j < [gestNames count]; j++) {
[g requireGestureRecognizerToFail:[self gestureForName:gestNames[j]]];
}
}
self.canvas.multipleTouchEnabled = YES;
}
-(void)createLabel {
C4Font *font = [C4Font fontWithName:@"AvenirNextCondensed-Heavy" size:96];
tapsAndTouches = [C4Label labelWithText:@"TAPS\n&\nTOUCHES" font:font];
tapsAndTouches.numberOfLines = 3;
[tapsAndTouches sizeToFit];
tapsAndTouches.textAlignment = ALIGNTEXTCENTER;
tapsAndTouches.center = self.canvas.center;
tapsAndTouches.userInteractionEnabled = NO;
[self.canvas addLabel:tapsAndTouches];
}
-(void)tap:(UITapGestureRecognizer *)tapGesture {
NSString *tapString;
switch (tapGesture.numberOfTapsRequired) {
case 1:
tapString = @"SINGLE TAP";
break;
case 2:
tapString = @"DOUBLE TAP";
break;
case 3:
tapString = @"TRIPLE TAP";
break;
default:
tapString = [NSString stringWithFormat:@"%d TAPS",tapGesture.numberOfTapsRequired];
break;
}
NSString *touchString;
switch (tapGesture.numberOfTouches) {
case 1:
touchString = @"1 TOUCH";
break;
default:
touchString = [NSString stringWithFormat:@"%d TOUCHES",tapGesture.numberOfTouches];
break;
}
[self updateLabelTaps:tapString touches:touchString];
}
-(void)updateLabelTaps:(NSString *)tapString touches:(NSString *)touchString {
tapsAndTouches.text = [NSString stringWithFormat:@"%@\n&\n%@",tapString,touchString];
[tapsAndTouches sizeToFit];
tapsAndTouches.width = self.canvas.width;
tapsAndTouches.center = self.canvas.center;
[resetTimer invalidate];
resetTimer = [C4Timer automaticTimerWithInterval:2.0f target:self method:@"resetLabel" repeats:NO];
}
-(void)resetLabel {
tapsAndTouches.text = @"TAPS\n&\nTOUCHES";
[tapsAndTouches sizeToFit];
tapsAndTouches.width = self.canvas.width;
tapsAndTouches.center = self.canvas.center;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment