Last active
August 29, 2015 14:07
-
-
Save imack/d5722809f071a38916ff to your computer and use it in GitHub Desktop.
Pan Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.m | |
// GestureRecognizers | |
// | |
// Created by Ian MacKinnon on 2014-10-16. | |
// Copyright (c) 2014 Ian MacKinnon. All rights reserved. | |
// | |
#import "ViewController.h" | |
@interface ViewController (){ | |
CGPoint startLocation; | |
} | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)]; | |
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft; | |
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didPan:)]; | |
[self.view addGestureRecognizer:panGesture]; | |
} | |
- (void)panGesture:(UIPanGestureRecognizer *)sender { | |
CGFloat distance =0.0; | |
if (startLocation.x != 0.0){ | |
CGPoint currentLocation = [sender locationInView:self.view]; | |
CGFloat dx = currentLocation.x - startLocation.x; | |
CGFloat dy = currentLocation.y - startLocation.y; | |
distance = sqrt(dx*dx + dy*dy ); | |
NSLog(@"Distance: %f", distance); | |
} | |
if (sender.state == UIGestureRecognizerStateBegan) { | |
startLocation = [sender locationInView:self.view]; | |
} else if (sender.state == UIGestureRecognizerStateEnded) { | |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"YOU DONE SWIPED" message:[NSString stringWithFormat:@"%f", distance] delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil]; | |
[alert show]; | |
} | |
- (void)didReceiveMemoryWarning { | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment