一.Touch常用屬性
window 觸摸產生時所處的窗口
view 觸摸時所產生的視圖
tapCount 短時間內點按屏幕的次數,可以判斷tapCount時點擊,雙擊,或更多點擊
timestamp 記錄觸摸事件產生或變化時的時間
phase 當前事件所處狀態
touchu常用方法
返回的位置是針對view的原點
-(CGPoint)locationInView:(UIView *)view
//記錄前一個觸摸點位置
-(CGPoint)previousLocationInView:(UIView *)view
UIEvent
事件對象,記錄時間產生時刻和類型,每產生一個事件,就會產生一個Event對象
屬性
type
subtype
事件產生時間
timestamp
二.代理方法
//觸摸開始
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//獲取到點擊對象
UITouch *touch = [touches anyObject];
NSLog(@"觸摸開始--是幾根手指%ld",touch.tapCount);
}
//觸摸移動
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//獲取移動偏移量
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
CGPoint preLocation = [touch previousLocationInView:self];
CGPoint offset = CGPointMake(location.x-preLocation.x, location.y-preLocation.y);
NSLog(@"offset.x--%f offset.y--%f",preLocation.x,preLocation.y);
}
//觸摸結束
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"觸摸結束");
}
//觸摸取消,可能會經歷
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}