this is what the resulting control+drag from the UILabel to STViewController.m should look like
@property (weak, nonatomic) IBOutlet UILabel *tapStatus;
goes in body of STViewController.m
-(void)singleTapResponder: (UISwipeGestureRecognizer *) sender
{
[_tapStatus setText:@"Single Tap Detected"];
}
goes in viewDidLoad of STViewController.m
UITapGestureRecognizer * singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapResponder:)];
// instantiate gesture recognizer
UITapGestureRecognizer * singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapResponder:)];
// number of taps to activate gesture
singleTapRecognizer.numberOfTapsRequired = 1;
// add the gesture recognizer to this view controller's UIView
[self.view addGestureRecognizer:singleTapRecognizer];
goes in body of STViewController.m
-(void)doubleTapResponder: (UISwipeGestureRecognizer *) sender
{
[_tapStatus setText:@"Double Tap Detected"];
}
goes in viewDidLoad of STViewController.m
// add the gesture recognizer to this view controller's UIView
[self.view addGestureRecognizer:singleTapRecognizer];
// instantiate gesture recognizer
UITapGestureRecognizer * doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapResponder:)];
// number of taps to activate gesture
doubleTapRecognizer.numberOfTapsRequired = 2;
// add the gesture recognizer to this view controller's UIView
[self.view addGestureRecognizer:doubleTapRecognizer];
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
[_touchInfo setText:[NSString stringWithFormat:@"(%d, %d)", (int)point.x, (int)point.y]];
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchHappened: touches activatedBy:@"Touch down"];
}
- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchHappened: touches activatedBy:@"Touch cancelled"];
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchHappened: touches activatedBy:@"Dragging"];
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchHappened: touches activatedBy:@"Touch up"];
}
- (void) touchHappened:(NSSet*) touches activatedBy:(NSString*) type
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
[_touchInfo setText:[NSString stringWithFormat:@"(%d, %d)", (int)point.x, (int)point.y]];
[_touchStatus setText:type];
}
- (void) touchHappened:(NSSet*) touches activatedBy:(NSString*) type
{
NSString* coords = @"";
for (UITouch* touch in touches)
{
CGPoint point = [touch locationInView:self.view];
coords = [NSString stringWithFormat:@"%@ (%d, %d)", coords, (int)point.x, (int)point.y];
}
[_touchInfo setText:coords];
[_touchStatus setText:type];
}
goes in viewDidLoad of GCViewController.m
image1 = [UIImage imageNamed:@"one.png"];
image2 = [UIImage imageNamed:@"two.png"];
myImageView.image = image1;
top of GCViewController.h
@property (strong, nonatomic) UIImage * image1;
@property (strong, nonatomic) UIImage * image2;
@synthesize myImageView, image1, image2;
goes in viewDidLoad of GCViewController.m
//instantiate our gesture recognizer
UISwipeGestureRecognizer * rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
[myImageView addGestureRecognizer:rightSwipe];
UISwipeGestureRecognizer * leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
[myImageView addGestureRecognizer:leftSwipe];
-(void)randomize
{
int rando = drand48()*2;
if(rando)
{
[UIView transitionWithView:self.myImageView
duration:.3f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.myImageView.image = image1;
} completion:nil];
}
else
{
[UIView transitionWithView:self.myImageView
duration:.3f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.myImageView.image = image2;
} completion:nil];
}
}
- (void)gameOver
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Game Over"
message:[NSString stringWithFormat:@"Final Score : %d", score]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
score = 0;
[myScore setText: [NSString stringWithFormat:@"Score %d", score]];
}
- (void)didSwipe: (UISwipeGestureRecognizer *) sender
{
UISwipeGestureRecognizerDirection direction = sender.direction;
switch(direction)
{
case UISwipeGestureRecognizerDirectionRight:
if(myImageView.image == image1)
{
[self randomize];
score++;
[myScore setText: [NSString stringWithFormat:@"Score %d", score]];
}
else
[self gameOver];
break;
case UISwipeGestureRecognizerDirectionLeft:
if(myImageView.image == image2)
{
[self randomize];
score++;
[myScore setText: [NSString stringWithFormat:@"Score %d", score]];
}
else
[self gameOver];
break;
default: break;
}
}
Replace the whole GCViewController.m file with the following
#import "GCViewController.h"
@interface GCViewController ()
@end
@implementation GCViewController
@synthesize myScore;
@synthesize instruction;
- (void)viewDidLoad
{
[super viewDidLoad];
srand48(time(0));
myArray = @[ @"swipe left", @"swipe right", @"tap once", @"tap twice",];
//instantiate our gesture recognizer
UISwipeGestureRecognizer * rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
[self.view addGestureRecognizer:rightSwipe];
UISwipeGestureRecognizer * leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:leftSwipe];
UITapGestureRecognizer * singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapResponder:)];
UITapGestureRecognizer * doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapResponder:)];
singleTapRecognizer.numberOfTapsRequired = 1;
doubleTapRecognizer.numberOfTapsRequired = 2;
singleTapRecognizer.numberOfTouchesRequired = 1;
doubleTapRecognizer.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:singleTapRecognizer];
[self.view addGestureRecognizer:doubleTapRecognizer];
[self randomize];
[myScore setText: [NSString stringWithFormat:@"Score %d", score]];
[instruction setText: [NSString stringWithFormat:myArray[rando]]];
}
-(void)singleTapResponder: (UISwipeGestureRecognizer *) sender
{
if (rando == 3)
return;
if(rando == 2)
[self score];
else
[self gameOver];
}
-(void)doubleTapResponder: (UISwipeGestureRecognizer *) sender
{
if(rando == 3)
[self score];
else
[self gameOver];
}
-(void)randomize
{
rando = drand48()*4;
}
- (void)gameOver
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Game Over"
message:[NSString stringWithFormat:@"Final Score : %d", score]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
score = 0;
[myScore setText: [NSString stringWithFormat:@"Score %d", score]];
}
- (void)score
{
score++;
[myScore setText: [NSString stringWithFormat:@"Score %d", score]];
[self randomize];
[instruction setText: [NSString stringWithFormat:myArray[rando]]];
}
- (void)didSwipe: (UISwipeGestureRecognizer *) sender
{
UISwipeGestureRecognizerDirection direction = sender.direction;
switch(direction)
{
case UISwipeGestureRecognizerDirectionRight:
if(rando == 1)
[self score];
else
[self gameOver];
break;
case UISwipeGestureRecognizerDirectionLeft:
if(rando == 0)
[self score];
else
[self gameOver];
break;
default: break;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
int score;
int rando;
NSArray* myArray;