Skip to content

Instantly share code, notes, and snippets.

@vgmoose
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgmoose/a78cbf3a74bedcf3a3af to your computer and use it in GitHub Desktop.
Save vgmoose/a78cbf3a74bedcf3a3af to your computer and use it in GitHub Desktop.
Taps Touches and Gestures

Simple Taps

Snippet 1.1

this is what the resulting control+drag from the UILabel to STViewController.m should look like

@property (weak, nonatomic) IBOutlet UILabel *tapStatus;

Snippet 1.2

goes in body of STViewController.m

-(void)singleTapResponder: (UISwipeGestureRecognizer *) sender
{
    [_tapStatus setText:@"Single Tap Detected"];
}

Snippet 1.3

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];

Snippet 1.4

goes in body of STViewController.m

-(void)doubleTapResponder: (UISwipeGestureRecognizer *) sender
{
    [_tapStatus setText:@"Double Tap Detected"];
}

Snippet 1.5

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];

Touches

Snippet 2.1

- (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]];
}

Snippet 2.2

- (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"];
}

Snippet 2.3

- (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];
}

Snippet 2.4

- (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];
}

Swipes

Snippet 3.1

goes in viewDidLoad of GCViewController.m

image1 = [UIImage imageNamed:@"one.png"];
image2 = [UIImage imageNamed:@"two.png"];

myImageView.image = image1;

Snippet 3.2

top of GCViewController.h

@property (strong, nonatomic) UIImage * image1;
@property (strong, nonatomic) UIImage * image2;

Snippet 3.3

@synthesize myImageView, image1, image2;

Snippet 3.4

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];

Snippet 3.5

-(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];

    }
}

Snippet 3.6

- (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]];
}

Snippet 3.7

- (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;
    }
    
}

Bop It

Replace the whole GCViewController.m file with the following

Snippet 4.1

#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

Snippet 4.2

int score;
int rando;
NSArray* myArray;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment