前言

	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<UITouch>, withEvent event: UIEvent?) { } // 触摸移动,重写 UIResponder 中定义的方法
    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { } // 触摸结束,重写 UIResponder 中定义的方法
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { } // 触摸取消,重写 UIResponder 中定义的方法
    override func touchesCancelled(touches: Set<UITouch>!, 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<UITouch>, 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<UITouch>, 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()
    }

iOS - UITouch的更多相关文章

  1. iOS开发——UI进阶篇(十二)事件处理,触摸事件,UITouch,UIEvent,响应者链条,手势识别

    触摸事件 在用户使用app过程中,会产生各种各样的事件 一.iOS中的事件可以分为3大类型 触摸事件加速计事件远程控制事件 响应者对象在iOS中不是任何对象都能处理事件,只有继承了UIResponde ...

  2. iOS中文API之UITouch详解

    UITouch 对象用于位置. 大小. 运动和一根手指在屏幕上为某一特定事件的力度.触摸的力度是从开始在 iOS 9 支持 3D 的触摸的设备上可用.你可以通过UIEvent对象传递给响应者对象访问. ...

  3. iOS:触摸控件UITouch、事件类UIEvent

    UITouch:触摸控件类   UIEvent:事件类 ❤️❤️❤️UITouch的介绍❤️❤️❤️ 一.触摸状态类型枚举 typedef NS_ENUM(NSInteger, UITouchPhas ...

  4. iOS开发UITouch触摸API简介

    1.UITouch简介 当用户触摸屏幕时,会创建一个UITouch对象: UITouch的作用保存着触摸相关的信息,比如触摸的位置.时间.阶段等: 当从开始到结束,系统会更新UITouch对象,结束时 ...

  5. iOS总结_UI层自我复习总结

    UI层复习笔记 在main文件中,UIApplicationMain函数一共做了三件事 根据第三个参数创建了一个应用程序对象 默认写nil,即创建的是UIApplication类型的对象,此对象看成是 ...

  6. iOS开发中静态库之".framework静态库"的制作及使用篇

    iOS开发中静态库之".framework静态库"的制作及使用篇 .framework静态库支持OC和swift .a静态库如何制作可参照上一篇: iOS开发中静态库之" ...

  7. iOS开发中静态库制作 之.a静态库制作及使用篇

    iOS开发中静态库之".a静态库"的制作及使用篇 一.库的简介 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.库的类型? 根据源代码的公开情况,库可以分为2种类 ...

  8. iOS中数据库应用基础

    iOS 数据库入门 一.数据库简介 1.什么是数据库? 数据库(Database) 是按照数据结构来组织,存储和管理数据的仓库 数据库可以分为2大种类 关系型数据库(主流) PC端 Oracle My ...

  9. iOS手势解锁、指纹解锁--Swift代码

    一.手势密码 1. 1.1.用UIButton组成手势的节点. 1.2.当手指接触屏幕时,调用重写的 touchesBegan:withEvent方法(在touchesBegan里调用setNeeds ...

随机推荐

  1. umask:遮罩码

    查看umask:umask 创建文件:-umask 文件默认不能具有执行权限 创建目录:-umask 设置umask:umask 0022 生效访范围:当前shell

  2. log4j里面的info,debug,error级别有什么区别

    一共分为五个级别:DEBUG.INFO.WARN.ERROR和FATAL.这五个级别是有顺序的,DEBUG < INFO < WARN < ERROR < FATAL,明白这一 ...

  3. DomainHelper

    public class DomainHelper { public static void SetDomainValue(string key, object password) { AppDoma ...

  4. oracle进制-10进制跟2进制互转

    CREATE OR REPLACE FUNCTION NUMBER_TO_BIT(V_NUM NUMBER) RETURN VARCHAR IS V_RTN );--注意返回列长度 V_N1 NUMB ...

  5. Asp.net Vnext ModelBinding

    Model Binding 本文已经同步到<Asp.net Vnext 系列教程 >中] Model binding(绑定)简单来说就是通过遍历ValueProvider(值提供者)获取的 ...

  6. 【转】MYSQL入门学习之十二:存储过程的基本操作

    转载地址:http://www.2cto.com/database/201212/177380.html 存储过程简单来说,就是为以后的使用而保存的一条或多条MySQL语句的集合.可将其视为批文件,虽 ...

  7. TelephonyManager类:Android手机及Sim卡状态的获取

    TelephonyManager这个类很有用,可以得到很多关于手机和Sim卡的信息. 直接上注释后的代码,请享用 package net.sunniwell.app;import android.ap ...

  8. 敌兵布阵(线段树HDU 1166)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissi ...

  9. ubuntu下环境变量

    01:/etc/environment:是设置整个系统的环境: 02:/etc/profile:是设置所有用户的环境: 03:.bashrc :本地用户:

  10. C#For循环

    for循环基本格式 for(初始条件;循环条件;状态改变) { 循环体 } 1.小明单位发了100元的购物卡,小明到超市买三类洗化用品,洗发水(15元),香皂(2元),牙刷(5元).要把100元整好花 ...