zoukankan      html  css  js  c++  java
  • UIGestureRecognizer(手势)、touch(触摸)

      1 // touch(触摸)、和手势
      2 // 手势核心就是设置delegate和在需要手势监测的view上使用addGestureRecognizer添加指定的手势监测
      3 #import "ViewController.h"
      4 @interface ViewController ()<UIGestureRecognizerDelegate> // 添加手势代理协议
      5 {
      6     /** 定义UIImageView类型全局变量image */
      7     UIImageView* image;
      8 }
      9 
     10 // touch的四种方法 (3.2以前主要使用这四种方法) touch可以用来自定义手势
     11 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; // 开始
     12 //{
     13 //    UITouch* touch = [touches anyObject]; // 点击对象
     14 //    CGPoint poin = [touch locationInView:self]; // 获取点击位置
     15 //}
     16 -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;  // 解除,取消
     17 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;  // 结束
     18 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;  // 移动
     19 @end
     20 @implementation ViewController
     21 - (void)viewDidLoad {
     22     [super viewDidLoad];
     23     
     24     image  =[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
     25     // 添加图片到视图
     26     image.image = [UIImage imageNamed:@"素材07-6"];
     27     // 设置图片类型为合适
     28     image.contentMode = UIViewContentModeScaleAspectFit;
     29     // 添加视图上
     30     [self.view addSubview:image];
     31     // 允许与用户交互
     32     image.userInteractionEnabled = YES;
     33     
     34     // 手势  继承UIGestureRecognizer
     35     /*
     36     UITapGestureRecognizer, // 点击
     37     UIPinchGestureRecognizer, //  捏合
     38     UIRotationGestureRecognizer, // 旋转
     39     UISwipeGestureRecognizer,  // 滑动,快速移动
     40     UIPanGestureRecognizer,  // 拖动,慢速移动
     41     UILongPressGestureRecognizer  // 长按
     42      
     43      有些手势是关联的,例如:Tap与LongPress、Swipe与Pan、或是Tap一次与Tap两次
     44      
     45      手势识别是具有互斥的原则的,比如单击和双击,如果他识别出一种手势,其后的手势将不被识别;
     46      
     47      关联手势的处理:比如单击和双击,如果不做处理,它就只能发送单击的消息。为了能够识别出双击手势,就需要做一个特殊的处理逻辑,即先判断手势是否是双击,在双击失效的情况下作为单击手势处理。
     48      [A requireGestureRecognizerToFail: B];函数,它可以指定当A手势发生时,即便A已经满足条件了,也不会立刻触发,会等到指定的手势B确定失败之后才触发。
     49      */
     50     // 点击(单击)
     51     UITapGestureRecognizer* oneTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(oneTap:)];
     52     // 单击,numberOfTapsRequired = 2 是双击 // 设置一个时间间隔
     53     oneTap.numberOfTapsRequired = 1; // 设置点击次数
     54     // 点击的手指数 默认为1
     55     oneTap.numberOfTouchesRequired = 1;
     56     // 默认为0
     57     int a= oneTap.numberOfTouches;
     58     NSLog(@"a = %d",a);
     59     // 添加手势监测
     60     [image addGestureRecognizer:oneTap];
     61     
     62     // 双击
     63     UITapGestureRecognizer* twoTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(twoTap:)];
     64     twoTap.numberOfTapsRequired = 2;
     65     [image addGestureRecognizer:twoTap];
     66 
     67     //(关键)、双击手势确定监测失败才会触发单击手势的响应操作(手势互斥时使用)
     68     [oneTap requireGestureRecognizerToFail:twoTap]; // 手势的依赖性
     69     
     70     
     71     // 捏合
     72     UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
     73     [image addGestureRecognizer:pinch];
     74     
     75     // 旋转
     76     UIRotationGestureRecognizer* rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
     77     [image addGestureRecognizer:rotation];
     78     
     79     // 滑动,快速移动
     80     UISwipeGestureRecognizer* swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
     81     // 设置滑动方向 默认时向右
     82     swipe.direction = UISwipeGestureRecognizerDirectionRight;
     83     typedef  enum{
     84         UISwipeGestureRecognizerDirectionDown, //
     85         UISwipeGestureRecognizerDirectionLeft, // 向左
     86         UISwipeGestureRecognizerDirectionRight, // 向右
     87         UISwipeGestureRecognizerDirectionUp, //
     88     }UISwipeGestureRecognizerDirection;
     89     
     90     [image addGestureRecognizer:swipe];
     91     
     92     // 拖动,慢速移动
     93     UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
     94     // 默认为-1;最大触摸点数
     95     pan.maximumNumberOfTouches = 1; //
     96     // 默认为1;最小触摸点数
     97     pan.minimumNumberOfTouches = 2; // 这两个属性只有Pan有
     98     
     99     [image addGestureRecognizer:pan];
    100     
    101     // 长按
    102     UILongPressGestureRecognizer* zer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(lon:)];
    103     // 设置长按时间
    104     zer.minimumPressDuration = 0.5; // 2秒
    105     [image addGestureRecognizer:zer];
    106 }
    107 // 点击(单击)
    108 - (void)oneTap:(UITapGestureRecognizer*)tap{
    109 //    if (tap.numberOfTapsRequired == 1) {
    110 //        NSLog(@"单击");
    111 //    }else if(tap.numberOfTapsRequired == 2){
    112 //        NSLog(@"双击");
    113 //    }
    114     
    115         NSLog(@"单击");
    116 }
    117 // 双击
    118 - (void)twoTap:(UITapGestureRecognizer*)two{
    119     NSLog(@"双击");
    120 }
    121 
    122 // 捏合
    123 - (void)pinch:(UIPinchGestureRecognizer*)pinch{
    124     // 放大缩小 (形变属性)
    125     pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    126     // 恢复标准 标准是1,大于1放大,小于1缩小
    127     pinch.scale = 1;
    128     // 清空形变属性
    129     // pinch.view.transform = CGAffineTransformIdentity;
    130     
    131     // 和上面等效 (但是这种方法缩放时没有滑动而是直接闪现缩放)
    132     pinch.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, pinch.scale, pinch.scale);
    133 }
    134 // 旋转
    135 - (void)rotation:(UIRotationGestureRecognizer*)rotation{
    136     // 旋转(形变属性) // 和上面同理
    137     rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
    138     //
    139     rotation.rotation = 0;
    140     
    141     // rotation.view.transform = CGAffineTransformRotate(CGAffineTransformIdentity, rotation.rotation);
    142 }
    143 // 滑动,快速移动
    144 - (void)swipe:(UISwipeGestureRecognizer*)swipe{
    145     NSLog(@"快速移动");
    146 }
    147 // 拖动,慢速移动
    148 - (void)pan:(UIPanGestureRecognizer*)pan{
    149     // 移动(形变属性) // 和上面同理
    150     CGPoint translation = [pan translationInView:self.view];
    151     pan.view.center = CGPointMake(pan.view.center.x + translation.x, pan.view.center.y + translation.y);
    152     [pan setTranslation:CGPointZero inView:self.view];
    153 }
    154 // 长按
    155 -(void)lon:(UILongPressGestureRecognizer*)longe{
    156     NSLog(@"长按");
    157 }
    158 
    159 // 代理方法
    160 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    161     return YES;
    162 }
    163 -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    164     return YES;
    165 }
    166 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    167     return YES;
    168 }
    169 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    170     return YES;
    171 }
    172 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    173     return YES;
    174 }
  • 相关阅读:
    Java面试宝典
    如何写一个webService接口
    webservice接口的开发和调用
    基于CXF框架下的SOAP Webservice服务端接口开发
    WebService—CXF整合Spring实现接口发布和调用过程
    使用cxf开发webservice接口
    webservice接口与HTTP接口学习笔记
    C#图解教程 第二十五章 其他主题
    C#图解教程 第二十四章 反射和特性
    C#图解教程 第二十三章 预处理指令
  • 原文地址:https://www.cnblogs.com/WillingToAsk1946zzh/p/4504473.html
Copyright © 2011-2022 走看看