IOS中各种手势操作实例代码

  

  

 IOS中各种手势操作实例代码

  

  

IOS中手势操作一般是类的几个手势子类去实现,一般我们用到的手势就这么5种:

  

1,点击,

  

2,平移

  

3,缩放,

  

4,旋转,

  

5,轻扫,

  

我们上面这个实例中就用到了上面这5种手势,不过其中点击与轻扫没有体现出来,只是输出了下日志而已,一会看代码

  

下面我们来分别介绍下这几种手势

  

        UITapGestureRecognizer * tapGes=[[UITapGestureRecognizer alloc] initWithTarget:自我行动:@ selector (tapGes:)];//点击次数,默认为1,1为单击,2为双击   tapGes。 numberOfTapsRequired=2;      

这个点击手势类有一个属性numberOfTapsRequired用于设置点击数,就是点击几次才触发这个事件

  

     //平移手势   - (void) initPanGes {   UIPanGestureRecognizer *彭日成=[[UIPanGestureRecognizer alloc] initWithTarget:自我行动:@ selector(庞:)];   (自我。imgView addGestureRecognizer彭日成):;   }   - (void)彭日成:(UIPanGestureRecognizer *) ges {//获取平移的坐标点   CGPoint transPoint=(ges translationInView: self.imgView);   }      

平移手势本身没太多可设置的属性,在平移事件触发手,可以用,translationInView方法获取当前平移坐标点

  

     //缩放手势   - (void) initPinGes {   UIPinchGestureRecognizer *平=[[UIPinchGestureRecognizer alloc] initWithTarget:自我行动:@ selector(萍:)];   (自我。imgView addGestureRecognizer萍):;   }   - (void)萍:(UIPinchGestureRecognizer *) ges {//缩放   self.imgView。变换=CGAffineTransformScale (self.imgView。变换,充电器。规模,ges.scale);   }      

缩放手势在事件里面可以获取规模属性,表示当前缩放值

  

     //旋转手势   - (void) initRotation {   UIRotationGestureRecognizer * rotationGes=[[UIRotationGestureRecognizer alloc] initWithTarget:自我行动:@ selector (rotationGes:)];   (自我。imgView addGestureRecognizer rotationGes):;   }   - (void) rotationGes: (UIRotationGestureRecognizer *) ges {//旋转图片   self.imgView。变换=CGAffineTransformRotate (self.imgView。变换,ges.rotation);   }      

旋转手势在事件里面可以通过获取旋转属性获取当前旋转的角度

  

     //轻扫手势   - (void) initSwipeGes {//创建从右向左轻扫的手势   UISwipeGestureRecognizer * swipeLeftGes=[[UISwipeGestureRecognizer alloc] initWithTarget:自我行动:@ selector (swipeGes:)];//方向,默认是从左往右//最多只能开启一个手势,如果要开启多个就得创建多个手势//监听从右向左的方向   swipeLeftGes。方向=UISwipeGestureRecognizerDirectionLeft;   (自我。imgView addGestureRecognizer swipeLeftGes):;   }   - (void) swipeGes: (UISwipeGestureRecognizer *) ges {//ges.direction方向值   NSLog (@ % s diection: % lu, __func__,(无符号长)ges.direction);   }      

轻扫手势对象需要设置方向属性,默认是只监听从左向右,这是一个枚举值UISwipeGestureRecognizerDirection

        UISwipeGestureRecognizerDirectionRight从左向右(默认值)   UISwipeGestureRecognizerDirectionLeft从右向左   UISwipeGestureRecognizerDirectionUp从下向上   UISwipeGestureRecognizerDirectionDown从上向下      

下面看一下我们上面那个效果图实现代码吧

     ////ViewController.m//各种手势操作////由xgao>//是否允许多手势操作,不是多触摸点   UIGestureRecognizer (BOOL)手势识别器:(*)手势识别器shouldRecognizeSimultaneouslyWithGestureRecognizer: UIGestureRecognizer (*) otherGestureRecognizer {   返回YES;   }      

以上所述是小编给大家介绍的IOS中各种手势操作实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留的言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

IOS中各种手势操作实例代码