UITouch 對象用於位置、 大小、 運動和一根手指在屏幕上為某一特定事件的力度。觸摸的力度是從開始在 iOS 9 支持 3D 的觸摸的設備上可用。你可以通過UIEvent對象傳遞給響應者對象訪問。一個UITouch對象包括訪問器:
引起觸摸的視圖或Window.
@property(nullable,nonatomic,readonly,strong) UIWindow *window
@property(nullable,nonatomic,readonly,strong) UIView *view
觸摸在視圖或Window的位置坐標.
- (CGPoint)locationInView:(nullable UIView *)view
觸摸的半徑.
@property(nonatomic,readonly) CGFloat altitudeAngle
觸摸的力度(支持iOS9.0以上)
@property(nonatomic,readonly) CGFloat force
UITouch對象還包含一個指示觸摸發生時間的時間戳,一個整數表示用戶點擊屏幕的次數,在觸摸階段以常量的形式描述觸摸是否開始,移動,或結束,或者是否為系統取消觸摸。
一個觸摸對象始終存留一個觸摸序列。處理事件時,永遠不會保留一個觸摸對象。如果你需要從一個觸摸階段到另一個階段保留有關觸摸信息,就應該復制該信息。
觸摸的 gestureRecognizers 屬性包含當前正在處理的觸摸手勢識別器。每個手勢識別器是 UIGestureRecognizer 具體子類的一個實例。
下面是一個實例
我在ViewController定義2個UIVIEW實例對象
@interface ViewController : UIViewController @property (nonatomic, strong) UIView *viewA; @property (nonatomic, strong) UIView *viewB; @end
然后
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.viewA =[[UIView alloc] initWithFrame:CGRectMake(10, 30, 48, 48)];
self.viewA.backgroundColor = [UIColor blackColor];
[self.view addSubview:self.viewA];
self.viewB =[[UIView alloc] initWithFrame:CGRectMake(10, 100, 48, 48)];
self.viewB.backgroundColor = [UIColor redColor];
[self.view addSubview:self.viewB];
NSLog(@"viewA:%@ \n viewB:%@ \n window:%@",self.viewA,self.viewB,[[[UIApplication sharedApplication] windows] objectAtIndex:0]);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// NSLog(@"%@",[touches anyObject]);
UITouch *touctObj = [touches anyObject];
NSLog(@"touch:%@ \n view:%@ \n window:%@",touctObj,[touctObj view],[touctObj window]);
}
觸摸以外區域我們可以看到view不是viewa也不是viewb,它是控制器的view.window是應用程序的window.
總之,UITouch對象包含一些發生觸摸的信息。引起觸摸的視圖或window。