(六)IOS手势和触摸的用法

简介: (六)IOS手势和触摸的用法

一、事件



1、在iOS上,事件有多种形式


1)触摸事件
2)运动事件
3)远程控制事件

2、UIView不接收触摸事件的三种情况


1.不接收用户交互(交互是指的两个没有关系的东西之间的交互,比如触摸的一些方法,平时很少有人注意到交互,因为默认的是交互)
  userInteractionEnabled = NO(这种不交互式可以看得见的情况下,无法进行交互)
  例如:self.view.userinteraction = NO;
  假如你单独操作一个视图上的label,可以在外面设置他的交互(在其他视图上Label默认的是不可交互),也可以给它单独设置一个类,继承于UILabel ,进行重写initWithFrame来设置
  例如
        -(instancetype)initWithFrame:(CGRect)frame
        {
           self = [super initWithFrame:frame];
             if (self) {
                self.userInteractionEnabled = YES;支持交互
                self.multipleTouchEnabled = YES;//支持多触点触摸
             }
            return self;
        }
 2.隐藏
  hidden = YES(看不见的不交互)
 3.透明(看不见,但是存在的交互)
  alpha = 0.0 ~ 0.01
 提示:UIImageView的userInteractionEnabled默认就是NO,因此UIImageView以及它的子控件默认是不能接收触摸事件的

二、事件处理基本方法:基本的触摸方法(这里面只是一些触摸的方法,具体的用法还是要和其他的用法结合在一起)



1、一个或多个手指触碰屏幕
   - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
  2、一个或多个手指在屏幕上移动
   - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
  3、一个或多个手指离开屏幕
   - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
  4、触摸序列被诸如电话呼入这样的系统事件取消
  - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

三、UITouch触摸对象



当用户触摸屏幕时,事件会被封装成一个event实例,包含了用户触摸的相关信息,event实例中包含着若干个UITouch实例,一个touch代表着用户的一个手指


1、UITouch常用属性

1)window
   触摸产生时所处的窗口
 2)view
   触摸产生时所处的试图
 3)tapCount
   tap(轻击)操作,和鼠标单独单击操作类似,"tapCount表示短时间内轻击屏幕的次数",因此可以根据tapCount判断单击、双击或更多的轻击
      双击试图是时单击也会执行的解决方法
         if (touch.tapCount == 1) {
           //延迟0.5秒执行 runLoop会计算这个时间
           [self performSelector:@selector(singleTap) withObject:nil afterDelay:0.5];
         }else{
            //告诉runLoop取消调用某个延迟的方法
           [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap) object:nil];
           [self doubleTap];
         }
 4)timestamp
   记录了触摸事件产生或变化时的时间,单位是秒
 5)phase  //触摸的生命周期
   触摸事件在屏幕上有一个周期,即触摸开始、触摸点移动、触摸点结束,还有中途取消,通过phase可以查看当前触摸事件在一个周期中所处的状态,pase是一个枚举,包含:
   //触摸开始
   UITouchPhaseBegan
   //接触点移动
   UITouchPhaseMoved
   //接触点无移动
   UITouchPhaseStationary
   //触摸结束
   UITouchPhaseEnded
   //触摸取消
   UITouchPhaseCancelled

2、UITouch常用方法

1)返回一个CGPoint类型的值,表示触摸在view这个视图上的位置,这里返回的位置是针对view的坐标系的
   - (CGPoint)locationInView:(UIView *)view
 2)该方法记录了前一个坐标值
   - (CGPoint)previousLocationInView:(UIView *)view:

四.举三个例子来解释Touch上面的一些用法

1.首先是放大和缩小:label不可移动,原地单击放大,双击缩小(你可以设置一个label或者一个button或者其他)



此程序代码写在新建的UILabel里面

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
   {
     UITouch *touch = [touches anyObject];
     if (touch.tapCount == 1)
    {
         NSLog(@"单击");
         [self performSelector:@selector(singleTap) withObject:nil afterDelay:0.5];
     }
     else if (touch.tapCount == 2)
    {
          NSLog(@"双击");
         //告诉runLoop取消调用某个延迟的方法
         [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap) object:nil];
         [self doubleTap];//调用双击的方法
     }
   }
 - (void)singleTap
 {
     //NSLog(@"单击等待");
     self.transform = CGAffineTransformScale(self.transform, 0.5, 0.5);
 }
 - (void)doubleTap
 {
    // NSLog(@"双击取消单击");
    self.transform = CGAffineTransformScale(self.transform, 2, 2);
  }

2.移动label的位置三种:拖动移动(move),刚点击开始移动和点击结束移动


(1)拖动移动(move): 在设定label的时候定义一个全局的UILabel对象

建立一个Label
   label = [[MyLabel alloc]initWithFrame:CGRectMake(100, 250, 200, 200)];
   label.text = @"我是label";
   label.userInteractionEnabled = YES;
   label.backgroundColor = [UIColor yellowColor];
   [self.view addSubview:label];
  -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
 {
    //返回一个CGPoint类型的值,表示触摸在view这个视图上的位置,这里返回的位置是针对view的坐标系的   - (CGPoint)     locationInView:(UIView *)view
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    label.center = point;
 }
(2)刚点击开始移动

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
 {
  上面移动的方法写在这里面即可
 }
(3)点击结束移动

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
   上面移动的方法写在这里面即可
}

3.模拟捏合(也就是双手缩放图片,这里用的是自定义的Label)

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (touches.count == 1) 
    {
      NSLog(@"移动时调用");
      UITouch *touch = [touches anyObject];
      CGPoint point = [touch locationInView:self.view];
      label.center = point;
      NSLog(@"%ld",touches.count);//查看几个手指触摸屏幕
    }
   if (touches.count == 2)
   {
     NSArray *array = [touches allObjects];//求出所有的触摸点放到数组里
     UITouch *touch1 = array[0];
     UITouch *touch2 = array[1];
     CGPoint point1 = [touch1 locationInView:self.view];//求point1出当前位置
     CGPoint point2 = [touch2 locationInView:self.view];//求point2出当前位置
     double distance = [self distance:point1 point:point2];//调用方法求出两个手指间的距离
     NSLog(@"位置%f",distance);
     CGPoint point = label.frame.origin; // origin |ˈɒrɪdʒɪn  求 label 的x,y的坐标  起源 size只是大小设置
     CGPoint center = label.center;
    label.frame = CGRectMake(point.x, point.y, distance, distance);
    label.center = center;
     NSLog(@"%lf",point.x);
   }
}
//求两个点之间的距离方法
- (double)distance:(CGPoint)p1 point:(CGPoint)p2 
{
    // ((x1-x2)平方+(y1-y2)平方)开方
     double distnce = sqrt(pow(p1.x-p2.x, 2)+pow(p1.y-p2.y, 2));
     return distnce;
}


目录
相关文章
|
8月前
|
Swift iOS开发
iOS @available 和 #available 的用法
iOS @available 和 #available 的用法
216 0
|
3月前
|
JSON 安全 数据安全/隐私保护
​iOS Class Guard github用法、工作原理和安装详解及使用经验总结
​iOS Class Guard github用法、工作原理和安装详解及使用经验总结
17 0
|
4月前
|
JSON 安全 数据安全/隐私保护
​iOS Class Guard github用法、工作原理和安装详解及使用经验总结
iOS Class Guard是一个用于OC类、协议、属性和方法名混淆的命令行工具。它是class-dump的扩展。这个工具会生成一个symbol table,这个table在编译期间会包含进工程中。iOS-Class-Guard能有效的隐藏绝大多数的类、协议、方法、属性和 实例变量 名。iOS-Class-Guard不是应用安全的最终解决方案,但是它绝对能让攻击者更难读懂你的程序。iOS-Class-Guard会加大代码分析和runtime检查的难度,这个工具可以认为是一个简单基础的混淆方法。由于OC的架构决定了iOS应用程序的剖析相当简单,check out一下链接就知晓了:
|
iOS开发
如何使用iOS手势UIGestureRecognizer
如何使用iOS手势UIGestureRecognizer
74 0
|
API iOS开发
iOS手势与变形
手势在用户交互中有着举足轻重的作用,这篇文字简单的介绍了iOS中的手势,并通过手势对控件进行变形处理。
88 0
|
程序员 API iOS开发
iOS开发:个人对于textView基础用法的总结(其一)
从事了这么久ios开发,对于textView的使用并不陌生,它和textfield有相似的地方,也有不同的地方,这里只对textView的一些基础用法进行描述,textfield不在这里描述。
300 0
|
数据安全/隐私保护 iOS开发
iOS 手势密码锁
首先看一下效果
99 0
|
iOS开发
(七) IOS 响应者链和手势
(七) IOS 响应者链和手势
316 0
|
前端开发 小程序 数据处理
iOS上传图片视图的封装:用法 【下篇】
iOS上传图片视图的封装:用法 【下篇】
222 0
iOS上传图片视图的封装:用法 【下篇】
|
设计模式 iOS开发
iOS手势全埋点
iOS手势全埋点
201 0
iOS手势全埋点