简单的手势操作

头文件:

 

#define kMinimumGestureLength  25
#define kMaximumVariance  5

#import <UIKit/UIKit.h>

@interface TouchesViewController : UIViewController {
	IBOutlet UILabel *label;
	CGPoint gestureStartPoint;
}

@end

 

实现文件:

 

#import "TouchesViewController.h"

@implementation TouchesViewController

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

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
	UITouch *touch = [touches anyObject];
	CGPoint currentPosition = [touch locationInView:self.view];    
	
	CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
	CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);
	
	if(deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance){
		label.text = @"Horizontal Swipe Detected";
		[self performSelector:@selector(eraseText) withObject:nil afterDelay:3];
	}
	else if(deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance){
		label.text = @"Vertical Swipe Detected";
		[self performSelector:@selector(eraseText) withObject:nil afterDelay:3];
	}
}

- (void)eraseText{
	label.text = @"";
}

- (void)dealloc {
	[super dealloc];
}

@end

 

你可能感兴趣的:(ios,iPhone,手势)