ios 手势 事件
来源:互联网 发布:情书dota淘宝店地址 编辑:程序博客网 时间:2024/05/17 02:54
#import "ViewController.h"#import "TouchView.h"#import "TouchPinch.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. //单击 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)]; [self.view addGestureRecognizer:tap]; [tap release]; //双击, 只能识别双击 UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTap:)]; doubleTap.numberOfTapsRequired = 2; [self.view addGestureRecognizer:doubleTap]; [doubleTap release]; //取消单击,只有双击,如果存在双击,则点击失效 [tap requireGestureRecognizerToFail:doubleTap]; //轻扫手势 每个手势只能监听一个方向 UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip:)]; swipe.direction = UISwipeGestureRecognizerDirectionLeft; [self.view addGestureRecognizer:swipe]; [swipe release]; //平移手势(滑动) UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)]; [self.view addGestureRecognizer:panGesture]; [panGesture release]; //长按手势,长按时间 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)]; longPress.minimumPressDuration = 2; [self.view addGestureRecognizer:longPress]; [longPress release]; //旋转手势 UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)]; [self.view addGestureRecognizer:rotation]; [rotation release];}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}#pragma mark action- (void)tap:(UITapGestureRecognizer *)tapGesture{ NSLog(@"单击");}- (void)doubleTap:(UITapGestureRecognizer *)tapGesture{ NSLog(@"双击");}- (void)swip:(UISwipeGestureRecognizer *)swipGesture{ NSLog(@"轻扫手势");}- (void)pan:(UIPanGestureRecognizer *)panGesture{ //平移的坐标,移动 CGPoint p = [panGesture locationInView:self.view]; NSLog(@"平移手势%@", NSStringFromCGPoint(p));}- (void)longPress:(UILongPressGestureRecognizer *)longGesture{ //长按结束才打印。 if (longGesture.state == UIGestureRecognizerStateEnded){ return; } NSLog(@"长按");}- (void)rotation:(UIRotationGestureRecognizer *)rotationGesture{ NSLog(@"旋转弧度 %f, 角度,%f", rotationGesture.rotation, rotationGesture.rotation * 180 / M_PI);}@end