Wednesday, August 10, 2011

Detecting direction of a swipe

Detecting direction of a swipe

MainView.h
Code:

#import
#import

@interface MainView : UIView {
IBOutlet UILabel *whataction;
CGPoint gestureStartPoint;
}

@end

MainView.m
Code:

#import "MainView.h"

@implementation MainView

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
gestureStartPoint = [touch locationInView:self];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self];
if (currentPosition.x - 30 > gestureStartPoint.x) {
[whataction setText:@"FAR RIGHT"];
}
else if (currentPosition.x > gestureStartPoint.x) {
[whataction setText:@"RIGHT"];
}
if (currentPosition.x + 30 < gestureStartPoint.x) {
[whataction setText:@"FAR LEFT"];
}
else if (currentPosition.x < gestureStartPoint.x) {
[whataction setText:@"LEFT"];
}
if (currentPosition.y - 30 > gestureStartPoint.y) {
[whataction setText:@"FAR DOWN"];
}
else if (currentPosition.y > gestureStartPoint.y) {
[whataction setText:@"DOWN"];
}
if (currentPosition.y + 30 < gestureStartPoint.y) {
[whataction setText:@"FAR UP"];
}
else if (currentPosition.y < gestureStartPoint.y) {
[whataction setText:@"UP"];
}
}
@end