标签:style blog color art cti 代码
//单指单击 UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerEvent:)]; singleFingerOne.numberOfTouchesRequired = 1; //手指数 singleFingerOne.numberOfTapsRequired = 1; // tap次数 singleFingerOne.delegate = self; //单指双击 UITapGestureRecognizer *singleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerEvent:)]; singleFingerTwo.numberOfTouchesRequired = 1; singleFingerTwo.numberOfTapsRequired = 2; singleFingerTwo.delegate = self; //双指单击 UITapGestureRecognizer *doubleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleFingerEvent:)]; doubleFingerOne.numberOfTouchesRequired = 2; doubleFingerOne.numberOfTapsRequired = 1; doubleFingerOne.delegate = self; //双指双击 UITapGestureRecognizer *doubleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleFingerEvent:)]; doubleFingerTwo.numberOfTouchesRequired = 2; doubleFingerTwo.numberOfTapsRequired = 2; doubleFingerTwo.delegate = self; //如果不加下面的话,当单指双击时,会先调用单指单击中的处理,再调用单指双击中的处理 [singleFingerOne requireGestureRecognizerToFail:singleFingerTwo]; //同理双指亦是如此 [doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo]; [self.view addGestureRecognizer:singleFingerOne]; [self.view addGestureRecognizer:singleFingerTwo]; [self.view addGestureRecognizer:doubleFingerOne]; [self.view addGestureRecognizer:doubleFingerTwo];
处理事件的方法:
//处理单指事件 - (void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender { if (sender.numberOfTapsRequired == 1) { //单指单击 NSLog(@"单指单击"); } else if (sender.numberOfTapsRequired == 2) { //单指双击 NSLog(@"单指双击"); } } //处理双指事件 - (void)handleDoubleFingerEvent:(UITapGestureRecognizer *)sender { if (sender.numberOfTapsRequired == 1) { //双指单击 NSLog(@"双指单击"); } else if (sender.numberOfTapsRequired == 2) { //双指双击 NSLog(@"双指双击"); } }
如果要使UIImageView响应点击事件:
UIImageView *imageView = [self yourImageView]; imageView.image = [self yourImage]; imageView.userInteractionEnabled = YES; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourHandlingCode:)]; [imageView addGestureRecognizer:singleTap];
UIKit中包含了UIGestureRecognizer类,用于检测发生在设备中的手势。UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,它有下面一些子类用于处理具体的手势:
1、拍击UITapGestureRecognizer (任意次数的拍击)
2、向里或向外捏UIPinchGestureRecognizer (用于缩放)
3、摇动或者拖拽UIPanGestureRecognizer
4、擦碰UISwipeGestureRecognizer (以任意方向)
5、旋转UIRotationGestureRecognizer (手指朝相反方向移动)
6、长按UILongPressGestureRecognizer
对于不同类型的手势识别器,具有不同的配置属性。比如UITapGestureRecognizer,可以配置拍击次数。界面接收到手势之后,可以发送一个消息,用于处理响应手势动作后的任务。当然,不同的手势识别器,发送的消息方法也会有所不同。下面列举几个具体示例代码:
//一个手指,拍击两次手势 UITapGestureRecognizer *oneFingerTwoTaps = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerTwoTaps)]; [oneFingerTwoTaps setNumberOfTapsRequired:2]; [oneFingerTwoTaps setNumberOfTouchesRequired:1]; [[self view] addGestureRecognizer:oneFingerTwoTaps]; //两个手指,拍击两次手势 UITapGestureRecognizer *twoFingersTwoTaps = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersTwoTaps)]; [twoFingersTwoTaps setNumberOfTapsRequired:2]; [twoFingersTwoTaps setNumberOfTouchesRequired:2]; [[self view] addGestureRecognizer:twoFingersTwoTaps]; //一个手指向上擦碰 UISwipeGestureRecognizer *oneFingerSwipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeUp:)] ; [oneFingerSwipeUp setDirection:UISwipeGestureRecognizerDirectionUp]; [[self view] addGestureRecognizer:oneFingerSwipeUp]; //一个手指向下擦碰 UISwipeGestureRecognizer *oneFingerSwipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeDown:)] ; [oneFingerSwipeDown setDirection:UISwipeGestureRecognizerDirectionDown]; [[self view] addGestureRecognizer:oneFingerSwipeDown]; //旋转手势 UIRotationGestureRecognizer *twoFingersRotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersRotate:)]; [[self view] addGestureRecognizer:twoFingersRotate]; //向里或向外捏的手势 UIPinchGestureRecognizer *twoFingerPinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)] ; [[self view] addGestureRecognizer:twoFingerPinch];
处理事件的方法:
- (void)oneFingerTwoTaps { NSLog(@"Action: One finger, two taps"); } - (void)twoFingersTwoTaps { NSLog(@"Action: Two fingers, two taps"); } - (void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer { CGPoint point = [recognizer locationInView:[self view]]; NSLog(@"Swipe up - start location: %f,%f", point.x, point.y); } - (void)oneFingerSwipeDown:(UISwipeGestureRecognizer *)recognizer { CGPoint point = [recognizer locationInView:[self view]]; NSLog(@"Swipe down - start location: %f,%f", point.x, point.y); } - (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer { NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] * (180 / M_PI)); } - (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer { NSLog(@"Pinch scale: %f", recognizer.scale); }
UITapGestureRecognizer,布布扣,bubuko.com
标签:style blog color art cti 代码
原文地址:http://www.cnblogs.com/luhaoming/p/3819886.html