iOS开发UITouch触摸API简介

1、UITouch简介

  • 当用户触摸屏幕时,会创建一个UITouch对象;
  • UITouch的作用保存着触摸相关的信息,比如触摸的位置、时间、阶段等;
  • 当从开始到结束,系统会更新UITouch对象,结束时会被销毁。
  • 期间所有的UITouch对象都被包含在UIEvent事件对象中,由管理程序UIApplication对象将事件分发。

 

2、触摸事件调用方法

//响应触摸事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;//手指按下的时候调用
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;//手指移动的时候调用
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;//手指抬起的时候调用
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;//取消(非正常离开屏幕,意外中断)
- (void)touchesEstimatedPropertiesUpdated:(NSSet<UITouch *> *)touches NS_AVAILABLE_IOS(9_1);// Apple Pencil 产生的 touch 事件的部分信息(如 Pencil 的方向等)传递到 iPad 或 iPhone 上会有一定的延时。
//UIKit 的回调方法 touchBegan 是立即产生的,其返回的参数 touch 中包含了 Pencil 产生的额外信息,这个额外信息是有延时的。所以,首次回调时会给出额外信息的预估值,延时获取真实值之后会调用 touchesEstimatedPropertiesUpdated 方法更新额外信息。

 

3、UITouch相关API

//
//  UITouch.h
//  UIKit
//
//  Copyright (c) 2007-2017 Apple Inc. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIKitDefines.h>

NS_ASSUME_NONNULL_BEGIN

@class UIWindow, UIView, UIGestureRecognizer;

typedef NS_ENUM(NSInteger, UITouchPhase) {
    UITouchPhaseBegan,             // 开始触摸
    UITouchPhaseMoved,             // 移动
    UITouchPhaseStationary,        // 停留
    UITouchPhaseEnded,             // 结束
    UITouchPhaseCancelled,         // 取消
};

typedef NS_ENUM(NSInteger, UIForceTouchCapability) {
    UIForceTouchCapabilityUnknown = 0,          // 3D Touch检测失败
    UIForceTouchCapabilityUnavailable = 1,      // 3D Touch不可用
    UIForceTouchCapabilityAvailable = 2         // 3D Touch可用
};

typedef NS_ENUM(NSInteger, UITouchType) {
    UITouchTypeDirect,                       // 手指和屏幕直接接触
    UITouchTypeIndirect,                     // 间接接触(不直接接触屏幕)
    UITouchTypeStylus NS_AVAILABLE_IOS(9_1), // 笔触
} NS_ENUM_AVAILABLE_IOS(9_0);

typedef NS_OPTIONS(NSInteger, UITouchProperties) {
    UITouchPropertyForce = (1UL << 0),      //力度
    UITouchPropertyAzimuth = (1UL << 1),    //方位
    UITouchPropertyAltitude = (1UL << 2),   //高度
    UITouchPropertyLocation = (1UL << 3),   //位置
} NS_AVAILABLE_IOS(9_1);

NS_CLASS_AVAILABLE_IOS(2_0) @interface UITouch : NSObject

@property(nonatomic,readonly) NSTimeInterval      timestamp;    // 时间
@property(nonatomic,readonly) UITouchPhase        phase;        // 状态
@property(nonatomic,readonly) NSUInteger          tapCount;     // 点击次数
@property(nonatomic,readonly) UITouchType         type NS_AVAILABLE_IOS(9_0);   //接触类型

// 接触面积的半径
// 接触半径的误差
@property(nonatomic,readonly) CGFloat majorRadius NS_AVAILABLE_IOS(8_0);
@property(nonatomic,readonly) CGFloat majorRadiusTolerance NS_AVAILABLE_IOS(8_0);

@property(nullable,nonatomic,readonly,strong) UIWindow                        *window;  //触摸所在窗口
@property(nullable,nonatomic,readonly,strong) UIView                          *view;    //触摸所在视图
@property(nullable,nonatomic,readonly,copy)   NSArray <UIGestureRecognizer *> *gestureRecognizers NS_AVAILABLE_IOS(3_2);    //触摸手势

- (CGPoint)locationInView:(nullable UIView *)view;   //在view上的触摸位置
- (CGPoint)previousLocationInView:(nullable UIView *)view;  //记录前一个触摸点的在view上的位置,view为nil就是整个窗口

//现在触摸的精确的坐标
//上一次触摸的精确的坐标
- (CGPoint)preciseLocationInView:(nullable UIView *)view NS_AVAILABLE_IOS(9_1);
- (CGPoint)precisePreviousLocationInView:(nullable UIView *)view NS_AVAILABLE_IOS(9_1);

// 触摸压力值
@property(nonatomic,readonly) CGFloat force NS_AVAILABLE_IOS(9_0);
// 最大触摸压力值
@property(nonatomic,readonly) CGFloat maximumPossibleForce NS_AVAILABLE_IOS(9_0);

//沿着x轴正向的方位角,当与x轴正向方向相同时,该值为0;当view参数为nil时,默认为keyWindow返回触针的方位角(弧度)。
- (CGFloat)azimuthAngleInView:(nullable UIView *)view NS_AVAILABLE_IOS(9_1);
//当前触摸对象的方向上的单位向量当view参数为nil时,默认为keyWindow返回在触针的方位角的方向指向的单位矢量。
- (CGVector)azimuthUnitVectorInView:(nullable UIView *)view NS_AVAILABLE_IOS(9_1);

//当笔平行于平面时,该值为0
//当笔垂直于平面时,该值为Pi / 2
//触针的高度(单位为弧度)。
@property(nonatomic,readonly) CGFloat altitudeAngle NS_AVAILABLE_IOS(9_1);

//当每个触摸对象的触摸特性发生变化时,该值将会单独增加,返回值是NSNumber 索引号,让您关联与原来的触摸更新的联系
@property(nonatomic,readonly) NSNumber * _Nullable estimationUpdateIndex NS_AVAILABLE_IOS(9_1);
//当前触摸对象估计的触摸特性,返回值是UITouchPropertyies一组触摸属性,这些属性将得到更新。
@property(nonatomic,readonly) UITouchProperties estimatedProperties NS_AVAILABLE_IOS(9_1);
//一组期望在未来的更新报文的触摸性能。
@property(nonatomic,readonly) UITouchProperties estimatedPropertiesExpectingUpdates NS_AVAILABLE_IOS(9_1);


@end

NS_ASSUME_NONNULL_END
UITouch相关API

 

4、其它

  4.1、类型:直接接触、隔空、笔触;

typedef NS_ENUM(NSInteger, UITouchType) {
    UITouchTypeDirect,                       // 手指和屏幕直接接触
    UITouchTypeIndirect,                     // 间接接触(不直接接触屏幕)
    UITouchTypeStylus NS_AVAILABLE_IOS(9_1), // 笔触
} NS_ENUM_AVAILABLE_IOS(9_0);

  4.2、多点触控:重写 isMultipleTouchEnabled 返回为YES

- (BOOL)isMultipleTouchEnabled{
    return YES;
}

  4.3、视图上多个Button单独触发

- (BOOL)isExclusiveTouch{
    return YES;
}

  4.4、同时设置 isMultipleTouchEnabled 和 isExclusiveTouch时,isExclusiveTouch无效。

 

5、简单使用

  5.1、获取一个UITouch对象

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch * touch = [touches anyObject];
}

  5.2、开启多点触控获取touches集合

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //获取所有的触摸对象
    NSArray * array = [touches allObjects];
}

posted @ 2020-06-22 11:41  brave-sailor  阅读(405)  评论(0编辑  收藏  举报