博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS手势识别的简单应用
阅读量:5774 次
发布时间:2019-06-18

本文共 4338 字,大约阅读时间需要 14 分钟。

hot3.png

六种手势

1.手势分类
  • UITapGestureRecognizer(敲击)
  • 设置numberOfTapsRequired点击的次数
  • 结合代理(BOOL) gestureRecognizer可以选定可点击的区域
  • UIPinchGestureRecognizer(捏合,用于缩放)
  • 注意复位问题
  • UIPanGestureRecognizer(拖拽)
  • UISwipeGestureRecognizer(轻扫)
  • UIRotationGestureRecognizer(旋转)
  • UILongPressGestureRecognizer(长按)
    • 长按的两次响应问题的解决
2.对每一个手势的注意点及代码实现进行说明
  • UITapGestureRecognizer(敲击)

    #pragma mark - tap敲击/** *  1.numberOfTapsRequired设置点击几次响应 *  2.代理与(BOOL) gestureRecognizer结合,可确定图片哪个区域可以点,哪些不能点 */-(void) setTap{    //创建点按手势    //initWithTarget:谁监听这个事件呢,一般控制器    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];    //delegate    tap.delegate=self;    //click counts    tap.numberOfTapsRequired=2;    //添加手势,用的是多态addGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer(父类)    [self.imageView addGestureRecognizer:tap];}-(void) tap:(UITapGestureRecognizer *) tap{    NSLog(@"%s",__func__);}
     

#pragma mark - UIGestureRecognizerDelegate //与tap结合 -(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch )touch{ //获取当前触摸点在self.imageView的哪个位置 CGPoint curP=[touch locationInView:self.imageView]; if (curP.x<self.imageView.bounds.size.width0.5) { return YES; }else{ return NO; } }

```
  • UIPinchGestureRecognizer(捏合,用于缩放)

     

#pragma mark - pinch捏合,用于缩放 -(void) setPinch{ UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)]; pinch.delegate=self; [self.imageView addGestureRecognizer:pinch]; } -(void) pinch:(UIPinchGestureRecognizer *) pinch{ NSLog(@"%s",func); self.imageView.transform=CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale); pinch.scale=1; } ```

  • UIPanGestureRecognizer(拖拽)

    #pragma mark - pan拖拽/** *  复位是指返回到起始的状态,再能过相对上一个状态tansform来解决一些问题 */-(void) setPan{    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];    [self.imageView addGestureRecognizer:pan];}-(void) pan:(UIPanGestureRecognizer *) pan{    NSLog(@"%s",__func__);//    CGPoint curP=[pan locationInView:self.imageView];    //获取图片移动距离    CGPoint curP=[pan translationInView:self.imageView];    self.imageView.transform=CGAffineTransformTranslate(self.imageView.transform, curP.x, curP.y);    //复位    [pan setTranslation:CGPointZero inView:self.imageView];}
  • UISwipeGestureRecognizer(轻扫)

    #pragma mark - swipe轻扫-(void) setSwipe{    //添加向左滑动    UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];    swipe.direction=UISwipeGestureRecognizerDirectionLeft;    [self.imageView addGestureRecognizer:swipe];    //添加向右滑动    UISwipeGestureRecognizer *swipeDown=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];    swipeDown.direction=UISwipeGestureRecognizerDirectionRight;    [self.imageView addGestureRecognizer:swipeDown];}-(void) swipe:(UISwipeGestureRecognizer *)swipe{    NSLog(@"%s",__func__);}
  • UIRotationGestureRecognizer(旋转)

    #pragma mark - rotation旋转-(void) setRotation{    UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];    rotation.delegate=self;    [self.imageView addGestureRecognizer:rotation];}-(void) rotation:(UIRotationGestureRecognizer *) rotation{    NSLog(@"%s",__func__);    NSLog(@"%f",rotation.rotation);    self.imageView.transform=CGAffineTransformRotate(self.imageView.transform, rotation.rotation);    [rotation setRotation:0];//    rotation.rotation=0;}
  • UILongPressGestureRecognizer(长按)

    #pragma mark - longPress长按-(void) setLongPress{    UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];    [self.imageView addGestureRecognizer:longPress];}/** *  长按的一些注意点,那就是长按时触发一次,离开时再触发一次 *  加个判断,结束与开始选一个触发的时间 *  @param longPress 传参 */-(void) longPress:(UILongPressGestureRecognizer *) longPress{    if (longPress.state==UIGestureRecognizerStateBegan) {        NSLog(@"%s",__func__);    }}
  • 当同时多个手势可以处理时(代理)

     

/**

  • 是否支持多个手势同时处理
  • @param gestureRecognizer 。。
  • @param otherGestureRecognizer 。。
  • @return return value bool */ -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; }
     
3.源代码的地址

转载于:https://my.oschina.net/HYLApple/blog/699128

你可能感兴趣的文章
AS3.0 Bitmap类实现图片3D旋转效果
查看>>
Eigen ,MKL和 matlab 矩阵乘法速度比较
查看>>
带三角的面包屑导航栏(新增递增数字)
查看>>
Web应用程序安全与风险
查看>>
codeforces 984 A. Game
查看>>
CSS居中
查看>>
One Person Game(概率+数学)
查看>>
CodeForces 258B Little Elephant and Elections :于1-m中找出七个数,使六个数里面的4和7个数比第七个数严格小:数位dp+dfs...
查看>>
MAP
查看>>
手把手教你测——上网快鸟
查看>>
用javascript获取地址栏参数
查看>>
一起谈.NET技术,你应该知道的15个Silverlight诀窍
查看>>
商教助手!解析夏普液晶高清宽屏投影机系列
查看>>
云南去年有望实现151万贫困人口净脱贫
查看>>
Java架构师面试题系列整理(大全)
查看>>
延伸产业链 中国产粮大省向“精深”问发展
查看>>
消费贷用户70%月收入低于5000元 80、90后是主要人群
查看>>
2018年内蒙古外贸首次突破1000亿元
查看>>
CTOR有助于BCH石墨烯技术更上一层楼
查看>>
被遗忘的CSS
查看>>