iOS 七大手势之轻拍,长按,旋转手势识别器方法-赵小波
一、监听触摸事件的做法 如果想监听一个view上面的触摸事件,之前的做法通常是:先自定义一个view,然后再实现view的touches方法,在方法内部实现具体处理代码 通过touches方法监听view触摸事件,有很明显的几个缺点 (1)必须得自定义view (2)由于是在view内部的touches方法中监听触摸事件,因此默认情况下,无法让其他外界对象监听view的触摸事件(需要通过代理) (3)不容易区分用户的具体手势行为 iOS 3.2之后,苹果推出了手势识别功能(Gesture Recognizer),在触摸事件处理方面,大大简化了开发者的开发难度 二、手势识别器 为了完成手势识别,必须借助于手势识别器----UIGestureRecognizer 利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势 UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势 UITapGestureRecognizer(轻拍) UIPinchGestureRecognizer(捏合,用于缩放) UIPanGestureRecognizer(拖拽) UISwipeGestureRecognizer(轻扫) UIRotationGestureRecognizer(旋转) UILongPressGestureRecognizer(长按) UIScreenEdgePanGestureRecognizer(屏幕边缘轻扫) (一)、轻拍手势 每一个手势识别器的用法都差不多,比如UITapGestureRecognizer的使用步骤如下: (1)创建手势识别器对象 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init]; (2)设置手势识别器对象的具体属性 // 连续轻拍2次 tap.numberOfTapsRequired = 2; // 需要2根手指一起触摸 tap.numberOfTouchesRequired = 2; (3)添加手势识别器到对应的view上 [self.iconView addGestureRecognizer:tap]; (4)监听手势的触发 [tap addTarget:self action:@selector(tapIconView:)]; 属性介绍: numberOfTouchesRequired //需要多少根手指一起轻拍(默认为1根) numberOfTapsRequired //需要轻拍多少下(默认为1) 实现方法 (二)、长按手势 UILongPressGestureRecognizer * longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo:)]; longPressGr.minimumPressDuration = 1.0; [self.tableView addGestureRecognizer:longPressGr]; -(void)longPressToDo:(UILongPressGestureRecognizer *)gesture { if(gesture.state == UIGestureRecognizerStateBegan) { CGPoint point = [gesture locationInView:self.tableView]; NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:point]; if(indexPath == nil) return ; //add your code here } } 三)、旋转手势 UIRotationGestureRecognizer手势识别器,就像名称一样,这个类能用来监听和捕获旋转的手势,能帮助你创建出更直观的图形用户界面,比如一种场景,当你的应用中有一个展示图片的视图,用户需要通过旋转图片来调整图片的方向。 UIRotationGestureRecognizer这个类有一个rotation的属性,这个属性可以用来设置旋转的方向和旋转的弧度。旋转是从手指的初始位置(UIGestureRecognizerStateBegan)到最终位置(UIGestureRecognizerStateBegan)决定的。 为了对继承自UIView的UI元素进行旋转,你可以将旋转手势识别器的rotation属性传递给CGAffineTransformMakeRotation方法,以制作一个仿射转场 UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)]; [view addGestureRecognizer:rotationGestureRecognizer]; - (void) rotateView:(UIRotationGestureRecognizer *)rotationGestureRecognizer { UIView *view = rotationGestureRecognizer.view; if (rotationGestureRecognizer.state == UIGestureRecognizerStateBegan || rotationGestureRecognizer.state == UIGestureRecognizerStateChanged) { view.transform = CGAffineTransformRotate(view.transform, rotationGestureRecognizer.rotation); [rotationGestureRecognizer setRotation:0]; } } (四)、平移手势 UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)]; // [imageView addGestureRecognizer:panGesture]; - (void)panGesture:(UIPanGestureRecognizer *)pan { //获取平移手势在视图上的坐标 CGPoint point = [pan locationInView:self.view]; //把视图的中心点确定在平移手势的坐标上,即把视图拖动到平移点 self.mView.center = point; NSLog(@"%@",NSStringFromCGPoint(point)); } (五)、捏合手势 self.pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinches:)]; [self.myBlackLabel addGestureRecognizer:self.pinchGestureRecognizer]; -(void)handlePinches:(UIPinchGestureRecognizer *)paramSender{ if (paramSender.state == UIGestureRecognizerStateEnded) { self.currentScale = paramSender.scale; }else if(paramSender.state == UIGestureRecognizerStateBegan && self.currentScale != 0.0f){ paramSender.scale = self.currentScale; } if (paramSender.scale !=NAN && paramSender.scale != 0.0) { paramSender.view.transform = CGAffineTransformMakeScale(paramSender.scale, paramSender.scale); } } (六)、轻扫手势 UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)]; /*Swipes that are performed from right to left are to be detected*/ swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; /*just one finger needed*/ swipeGestureRecognizer.numberOfTouchesRequired = 1; /*add it to the view*/ [self.view addGestureRecognizer:swipeGestureRecognizer]; -(void)handleSwipes:(UISwipeGestureRecognizer *)paramSender{ NSLog(@"swipe to left"); } 八)、同时触发两个view的手势 手势之间是互斥的,如果你想同时触发蛇和龙的view,那么需要实现协议 (1)、遵循协议。 (2)、同意添加多个手势。 (3)、把self作为代理设置给手势
作者:SIBU iOS DEV
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.