iOS開發UITouch觸摸API簡介

一、UITouch簡介ide

  • 當用戶觸摸屏幕時,會建立一個UITouch對象;
  • UITouch的做用保存着觸摸相關的信息,好比觸摸的位置、時間、階段等;
  • 當從開始到結束,系統會更新UITouch對象,結束時會被銷燬。
  • 期間全部的UITouch對象都被包含在UIEvent事件對象中,由管理程序UIApplication對象將事件分發。

 

二、觸摸事件調用方法性能

//響應觸摸事件
- (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 方法更新額外信息。

 

三、UITouch相關APIatom

//
//  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

 

四、其它spa

  4.一、類型:直接接觸、隔空、筆觸;code

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

  4.二、多點觸控:重寫 isMultipleTouchEnabled 返回爲YES對象

- (BOOL)isMultipleTouchEnabled{
    return YES;
}

  4.三、視圖上多個Button單獨觸發blog

- (BOOL)isExclusiveTouch{
    return YES;
}

  4.四、同時設置 isMultipleTouchEnabled 和 isExclusiveTouch時,isExclusiveTouch無效。索引

 

五、簡單使用事件

  5.一、獲取一個UITouch對象ip

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

  5.二、開啓多點觸控獲取touches集合

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //獲取全部的觸摸對象
    NSArray * array = [touches allObjects];
}

雙點觸摸示例圖

相關文章
相關標籤/搜索