美文网首页
UITouch触摸操作

UITouch触摸操作

作者: yz_wang | 来源:发表于2016-11-19 13:31 被阅读0次

    1. 触摸操作的生命周期

    • 手指触摸屏幕的瞬间
      在这个过程中,可以通过 touch.tapCount 判断是单击还是双击。
      可以通过 [touch locationInView:self.view] 来获取点击位置坐标
    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}span.s1 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s6 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s7 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s8 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s9 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s10 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s11 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s12 {font-variant-ligatures: no-common-ligatures; color: #4f8187}
    
    //当点击屏幕开始的瞬间,调用此函数,即状态1
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"手指触碰瞬间");
        //获取点击对象。anyObeject获取任何一个点击对象
        //一般只有一个点击对象
        UITouch* touch=[touches anyObject];
        
        
        //记录点击的次数
        
        if(touch.tapCount==1)
            NSLog(@"单次点击");
        else if (touch.tapCount==2)
            NSLog(@"双击");
        
    }
    
    • 手指在屏幕上未离开过程
      在这个过程中可以通过 [touch locationInView: ]来获取手指相对于当前视图的坐标。
    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #d12f1b}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s6 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s7 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}
    
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"手指在屏幕上, 并且移动。可以获取坐标数据");
        
        //获取手指相对当前视图的坐标
        UITouch* touch=[touches anyObject];
        CGPoint pt=[touch locationInView:self.view];
    
    • 手指离开屏幕的瞬间
    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s6 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}
    
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"手指离开屏幕");
    }
    
    • 还有一个特殊情况下终止触屏操作的协议,比如游戏过程中来电话
    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s6 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}
    
    -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"特殊情况下终端触屏.比如电话");
    }
    

    2.实现手指拖拽控制图片移动的功能

    手指移动的偏移量决定图片移动的向量,所以需要计算此时手指坐标和上一时刻手指坐标的差值。

    在ViewController.h中声明一个CGPoint,用来存放上一时刻手指位置:

    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}span.s1 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s2 {font-variant-ligatures: no-common-ligatures}
    
    CGPoint _mPtList;
    

    然后获得此时手指位置并计算偏移量大小:
    注意 计算完要把此时的位置赋给 _mPtList

    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4f8187}span.s1 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s2 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s6 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s7 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s8 {font-variant-ligatures: no-common-ligatures; color: #000000}
    
        //获取手指相对当前视图的坐标
        UITouch* touch=[touches anyObject];
        CGPoint pt=[touch locationInView:self.view];
        
        //每次移动偏移量大小
        float xOffset=pt.x-_mPtList.x;
        float yOffset=pt.y-_mPtList.y;
        
        _mPtList=pt;
    

    然后根据偏移量改变图像视图的位置:

    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s5 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s6 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}
    
        UIImageView* iView=[self.view viewWithTag:101];
        
        NSLog(@"x=%f,y=%f",pt.x,pt.y);
        
        iView.frame=CGRectMake(iView.frame.origin.x+xOffset, iView.frame.origin.y+yOffset, iView.frame.size.width, iView.frame.size.height);
    

    相关文章

      网友评论

          本文标题:UITouch触摸操作

          本文链接:https://www.haomeiwen.com/subject/ytxepttx.html