您的位置:首页 > 产品设计 > UI/UE

iOS触摸手势——UITouch

2016-10-15 08:36 253 查看
Demo实例

UITouch,主要是重写四个方法(触摸开始、触摸移动、触摸结束、触摸退出)以实现触摸响应方法

 1、触摸开始 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { }

 2、触摸移动 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { }

 3、触摸结束 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { }

 4、触摸退出(注意:通常是受到设备其他方法的影响:如来电、电量不足关机等) - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { }

 

 使用注意事项:

 1、触摸开始方法中,一般实现的属性有:

 1-1、获取触摸对象,如:

[objc] view
plain copy

 





UITouch *touch = touches.anyObject;  

 1-2、触摸次数,以便判断实现对应方法,如:

[objc] view
plain copy

 





NSInteger clickNumber = touch.tapCount;  

NSLog(@"click number = %@", @(clickNumber));  

 1-3、触摸位置是否用户想要处理的子视图,如:

[objc] view
plain copy

 





CGPoint currentPoint = [touch locationInView:self.view];  

if (CGRectContainsPoint(self.label.frame, currentPoint))  

{  

   // 改变标题  

   self.label.text = @"触摸位置在当前label";  

}  

 2、触摸移动方法中,一般实现的属性有:

 2-1、获取触摸对象,如:

 UITouch *touch = touches.anyObject;

 2-2、获取当前点击位置,以及上一个点击位置,如:

[objc] view
plain copy

 





CGPoint currentPoint = [touch locationInView:self.view];  

CGPoint previousPoint = [touch previousLocationInView:self.view];  

 注:可通过触摸移动改变子视图的位置,如:

[objc] view
plain copy

 





float moveX = currentPoint.x - previousPoint.x;  

float moveY = currentPoint.y - previousPoint.y;  

CGFloat centerX = self.label.center.x + moveX;  

CGFloat centerY = self.label.center.y + moveY;  

self.label.center = CGPointMake(centerX, centerY);  

[objc] view
plain copy

 





// 触摸开始  

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event  

{  

//    self.view.backgroundColor = [UIColor colorWithRed:(arc4random() % 255 / 255.0) green:(arc4random() % 255 / 255.0) blue:(arc4random() % 255 / 255.0) alpha:1.0];  

      

    NSLog(@"触摸手势 开始");  

      

      

    // 获取触摸对象  

    UITouch *touch = touches.anyObject;  

    // 获取触摸次数  

    NSInteger clickNumber = touch.tapCount;  

    NSLog(@"click number = %@", @(clickNumber));  

    if (2 == clickNumber)  

    {  

        self.label.backgroundColor = [UIColor redColor];  

    }  

    else if (3 == clickNumber)  

    {  

        self.label.backgroundColor = [UIColor greenColor];  

    }  

    // 获取触摸状态  

    UITouchPhase state = touch.phase;  

    NSLog(@"state = %ld", state);  

    // 获取响应视图对象  

    UIView *view = touch.view;  

    NSLog(@"view = %@", view);  

    // 获取当前触摸位置  

    CGPoint currentPoint = [touch locationInView:self.view];  

    NSLog(@"touch.locationInView = {%2.3f, %2.3f}", currentPoint.x, currentPoint.y);  

    // 获取当前触摸的前一个位置  

    CGPoint previousPoint = [touch previousLocationInView:self.view];  

    NSLog(@"touch.previousLocationInView = {%2.3f, %2.3f}", previousPoint.x, previousPoint.y);  

    // 获取触摸区域的子视图  

    // 方法1  

//    for (UIView *oneView in self.view.subviews)  

//    {  

//        if ([oneView isKindOfClass:[UILabel class]])  

//        {  

//            // 是否选中图标(当前坐标是否包含所选图标)  

//            if (CGRectContainsPoint(oneView.frame, currentPoint))  

//            {  

//                // 获取当前被选择图标  

//                UILabel *selectedView = (UILabel *)oneView;  

//                // 改变标题  

//                selectedView.text = @"触摸位置在当前label";  

//            }  

//        }  

//    }  

    // 方法2  

    if (CGRectContainsPoint(self.label.frame, currentPoint))  

    {  

        // 改变标题  

        self.label.text = @"触摸位置在当前label";  

    }  

}  

[objc] view
plain copy

 





// 触摸移动  

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event  

{  

//    self.view.backgroundColor = [UIColor colorWithRed:(arc4random() % 255 / 255.0) green:(arc4random() % 255 / 255.0) blue:(arc4random() % 255 / 255.0) alpha:1.0];  

      

    NSLog(@"触摸手势 移动");  

      

    // 移动UI视图控件  

    // 方法1 获得触摸点的集合,可以判断多点触摸事件  

    for (UITouch *touch in event.allTouches)  

    {  

        // 获取当前触摸坐标  

        CGPoint currentPoint = [touch locationInView:self.view];  

        NSLog(@"touch.locationInView = {%2.3f, %2.3f}", currentPoint.x, currentPoint.y);  

          

        // 获取当前触摸前一个坐标  

        CGPoint previousPoint = [touch previousLocationInView:self.view];  

        NSLog(@"touch.previousLocationInView = {%2.3f, %2.3f}", previousPoint.x, previousPoint.y);  

          

        // 坐标偏移量 (效果异常??)  

        float moveX = currentPoint.x - previousPoint.x;  

        float moveY = currentPoint.y - previousPoint.y;  

        CGFloat centerX = self.label.center.x + moveX;  

        CGFloat centerY = self.label.center.y + moveY;  

        self.label.center = CGPointMake(centerX, centerY);  

    }  

    // 方法2  

//    UITouch *touch = touches.anyObject;  

//    // 获取当前触摸坐标  

//    CGPoint currentPoint = [touch locationInView:self.view];  

//    NSLog(@"touch.locationInView = {%2.3f, %2.3f}", currentPoint.x, currentPoint.y);  

//    // 获取当前触摸前一个坐标  

//    CGPoint previousPoint = [touch previousLocationInView:self.view];  

//    NSLog(@"touch.previousLocationInView = {%2.3f, %2.3f}", previousPoint.x, previousPoint.y);  

//    // 坐标偏移量  

//    float moveX = currentPoint.x - previousPoint.x;  

//    float moveY = currentPoint.y - previousPoint.y;  

//    CGFloat centerX = self.label.center.x + moveX;  

//    CGFloat centerY = self.label.center.y + moveY;  

//    self.label.center = CGPointMake(centerX, centerY);  

}  

[objc] view
plain copy

 





// 触摸结束  

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event  

{  

//    self.view.backgroundColor = [UIColor whiteColor];  

      

    NSLog(@"触摸手势 结束");  

      

    self.label.text = @"touch触摸手势";  

}  

[objc] view
plain copy

 





// 触摸退出(注意:通常是受到设备其他方法的影响:如来电、电量不足关机等)  

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event  

{  

    NSLog(@"触摸手势 退出");  

}  

效果图





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: