博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS - UITouch
阅读量:6279 次
发布时间:2019-06-22

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

前言

NS_CLASS_AVAILABLE_IOS(2_0)  @interface UITouch : NSObject    @available(iOS 2.0, *)       public class UITouch : NSObject
  • 触摸事件基本都是发生在 viewController 中,首先触摸的对象是视图,而视图的类 UIView 继承了 UIRespnder 类,但是要对事件作出处理,还需要重写 UIResponder 类中定义的事件处理函数。根据不同的触摸状态,程序会调用相应的处理函数。

1、touch 的创建

  • 在系统触摸事件处理方法中实现

  • Objective-C

    // 获取任意一个触摸对象    UITouch *touch = [touches anyObject];    // 获取任意一个触摸对象    UITouch *touch = [[event allTouches] anyObject];    // 获取指定的 view 触摸对象    UITouch *touch = [[event touchesForView:myView] anyObject];    // 获取指定的 window 触摸对象    UITouch *touch = [[event touchesForWindow:self.view.window] anyObject];
  • Swift

    // 获取任意一个触摸对象    let touch:UITouch = touches.first! as UITouch    // 获取任意一个触摸对象    let touch2:UITouch = (event?.allTouches()?.first!)! as UITouch    // 获取指定的 view 触摸对象    let touch3:UITouch = (event!.touchesForView(myView!)?.first)! as UITouch    // 获取指定的 window 触摸对象    let touch4:UITouch = (event!.touchesForWindow(self.view.window!)?.first)! as UITouch

2、touch 的设置

  • 在系统触摸事件处理方法中实现

  • Objective-C

    // 设置接收多点触摸    /*        默认为 NO,即视图默认不接收多点触摸    */    self.view.multipleTouchEnabled = YES;
  • Swift

    // 设置接收多点触摸    /*        默认为 NO,即视图默认不接收多点触摸    */    self.view.multipleTouchEnabled = true

3、touch 的获取

  • 在系统触摸事件处理方法中实现

  • Objective-C

    // 获取触摸窗口    UIWindow *touchWindow = touch.window;    // 获取触摸视图    UIView *touchView = touch.view;    // 获取触摸手势    NSArray *touchGesture = touch.gestureRecognizers;    // 获取触摸次数    NSUInteger tapCount = touch.tapCount;    // 获取触摸时间    NSTimeInterval timestamp = touch.timestamp;    // 获取触摸状态    /*        UITouchPhaseBegan,         // whenever a finger touches the surface.     触摸开始        UITouchPhaseMoved,         // whenever a finger moves on the surface.    接触点移动        UITouchPhaseStationary,    // whenever a finger is touching the surface but hasn't moved                                    // since the previous event.                  接触点无移动        UITouchPhaseEnded,         // whenever a finger leaves the surface.      触摸结束        UITouchPhaseCancelled,     // whenever a touch doesn't end but we need to stop tracking                                    // (e.g. putting device to face)              触摸取消    */    UITouchPhase touchPhase = touch.phase;    // 获取触摸位置        // 上次触摸点的位置        CGPoint lastPoint = [touch previousLocationInView:self.view];        // 当前触摸点的位置        CGPoint currentPoint = [touch locationInView:self.view];    // 获取触摸半径    CGFloat majorRadius = touch.majorRadius;    CGFloat majorRadiusTolerance = touch.majorRadiusTolerance;
  • Swift

    // 获取触摸窗口    let touchWindow:UIWindow = touch.window!    // 获取触摸视图    let touchView:UIView = touch.view!    // 获取触摸手势    let touchGesture:[AnyObject] = touch.gestureRecognizers!    // 获取触摸次数    let tapCount:Int = touch.tapCount    // 获取触摸时间    let timestamp:NSTimeInterval = touch.timestamp    // 获取触摸状态    /*        case Began,        // whenever a finger touches the surface.     触摸开始        case Moved,        // whenever a finger moves on the surface.    接触点移动        case Stationary,   // whenever a finger is touching the surface but hasn't moved since                            // the previous event.                        接触点无移动        case Ended,        // whenever a finger leaves the surface.      触摸结束        case Cancelled,    // whenever a touch doesn't end but we need to stop tracking                            // (e.g. putting device to face)              触摸取消    */    let touchPhase:UITouchPhase = touch.phase    // 获取触摸位置        // 上次触摸点的位置        let lastPoint:CGPoint = touch.previousLocationInView(self.view)        // 当前触摸点的位置        let currentPoint:CGPoint = touch.locationInView(self.view)    // 获取触摸半径    let majorRadius:CGFloat = touch.majorRadius    let majorRadiusTolerance:CGFloat = touch.majorRadiusTolerance

4、系统触摸事件处理方法

  • 不用手动调用

  • Objective-C

    // 触摸开始,重写 UIResponder 中定义的方法    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    }    // 触摸移动,重写 UIResponder 中定义的方法    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {    }    // 触摸结束,重写 UIResponder 中定义的方法    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {    }    // 触摸取消,重写 UIResponder 中定义的方法    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {    }
  • Swift

    // 触摸开始,重写 UIResponder 中定义的方法    override func touchesBegan(touches: Set
    , withEvent event: UIEvent?) { } // 触摸移动,重写 UIResponder 中定义的方法 override func touchesMoved(touches: Set
    , withEvent event: UIEvent?) { } // 触摸结束,重写 UIResponder 中定义的方法 override func touchesEnded(touches: Set
    , withEvent event: UIEvent?) { } // 触摸取消,重写 UIResponder 中定义的方法 override func touchesCancelled(touches: Set
    !, withEvent event: UIEvent!) { }

5、视图随触摸移动

  • Objective-C

    // 触摸移动    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {        // 获取触摸对象        UITouch *touch = [touches anyObject];        UIView *tapView = touch.view;        // 获取触摸点位置        CGPoint lastPoint = [touch previousLocationInView:self.view];        CGPoint currentPoint = [touch locationInView:self.view];        // 改变视图中心坐标        CGPoint tapViewCenter = tapView.center;        tapViewCenter.x += currentPoint.x - lastPoint.x;        tapViewCenter.y += currentPoint.y - lastPoint.y;        tapView.center = tapViewCenter;    }
  • Swift

    // 触摸移动    override func touchesMoved(touches: Set
    , withEvent event: UIEvent?) { // 获取触摸对象 let touch:UITouch = touches.first! as UITouch let tapView = touch.view // 获取触摸点位置 let lastPoint:CGPoint = touch.previousLocationInView(self.view) let currentPoint:CGPoint = touch.locationInView(self.view) // 改变视图中心坐标 var tapViewCenter = tapView!.center tapViewCenter.x += currentPoint.x - lastPoint.x tapViewCenter.y += currentPoint.y - lastPoint.y tapView!.center = tapViewCenter }

6、单击/双击触摸

  • Objective-C

    // 触摸结束    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {        UITouch *touch = [touches anyObject];        // 单击        if (touch.tapCount == 1) {             // 响应单击触摸事件            [self performSelector:@selector(singleTapClick) withObject:nil afterDelay:0];        }        // 双击        else if (touch.tapCount == 2) {            // 取消单击触摸响应事件            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTapClick) object:nil];            // 响应双击触摸事件            [self performSelector:@selector(doubleTapClick) withObject:nil afterDelay:0];        }    }    // 单击触摸响应事件处理    - (void)singleTapClick {        self.view.backgroundColor = [UIColor greenColor];    }    // 双击触摸响应事件处理    - (void)doubleTapClick {        self.view.backgroundColor = [UIColor orangeColor];    }
  • Swift

    // 触摸结束    override func touchesEnded(touches: Set
    , withEvent event: UIEvent?) { let touch = touches.first! as UITouch // 单击 if touch.tapCount == 1 { // 响应单击触摸事件 self.performSelector(#selector(singleTapClick), withObject: nil, afterDelay: 0) } // 双击 if touch.tapCount == 2 { // 取消单击触摸响应事件 NSObject.cancelPreviousPerformRequestsWithTarget(self, selector: #selector(singleTapClick), object: nil) // 响应双击触摸事件 self.performSelector(#selector(doubleTapClick), withObject: nil, afterDelay: 0) } } // 单击触摸响应事件处理 func singleTapClick() { self.view.backgroundColor = UIColor.greenColor() } // 双击触摸响应事件处理 func doubleTapClick() { self.view.backgroundColor = UIColor.orangeColor() }

转载于:https://www.cnblogs.com/QianChia/p/5759175.html

你可能感兴趣的文章
CCNP-6 OSPF试验2(BSCI)
查看>>
Excel 2013 全新的图表体验
查看>>
openstack 制作大于2TB根分区自动扩容的CENTOS镜像
查看>>
Unbuntu安装遭遇 vmware上的Easy install模式
查看>>
几个常用的ASP木马
查看>>
python分析postfix邮件日志的状态
查看>>
Mysql-5.6.x多实例配置
查看>>
psutil
查看>>
在git@osc上托管自己的代码
查看>>
机器学习算法:朴素贝叶斯
查看>>
小五思科技术学习笔记之扩展访问列表
查看>>
使用Python脚本检验文件系统数据完整性
查看>>
使用MDT部署Windows Server 2003 R2
查看>>
Redhat as5安装Mysql5.0.28
查看>>
通过TMG发布ActiveSync
查看>>
Web服务器的配置与管理(4) 配置访问权限和安全
查看>>
Linux系统安装VMware Tools
查看>>
asp.net 页面右下角弹出类似QQ或MSN的消息提示
查看>>
游戏开发经常使用算法概述
查看>>
EDM制作要点
查看>>