- 響應(yīng)觸摸方法:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesBegan");
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesMoved");
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesEnded");
}
//當(dāng)系統(tǒng)事件(如內(nèi)存不足、電話(huà)呼入)終止了觸摸事件時(shí)響應(yīng)該方法
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesCancelled");
}
- 添加了手勢(shì)的控件常需要設(shè)置的屬性:
(UILabel,UIImageVIew默認(rèn)不允許用戶(hù)交互)
//UILabel默認(rèn)不允許用戶(hù)交互,故如此設(shè)置
_lbtext.userInteractionEnabled = YES;
//設(shè)置控件為多點(diǎn)觸控
_lbtext.multipleTouchEnabled = YES;
-
手勢(shì)處理器 UIGestureRecognizer
UIGestureRecognizer是所有手勢(shì)控制器的基類(lèi),它提供了如下子類(lèi):- UITapGestureRecognizer:輕拍手勢(shì)
- UIPinchGestureRecognizer : 捏合手勢(shì)
- UIPanGestureRecognizer :拖拽手勢(shì)
- UISwipeGestureRecognizer : 輕掃手勢(shì)
- UIRotationGestureRecognizer :旋轉(zhuǎn)手勢(shì)
- UILongPressGestureRecognizer :長(zhǎng)按手勢(shì)
UITapGestureRecognizer 輕拍 -常用于 “加在空白處霞幅,關(guān)閉鍵盤(pán)”
UITapGestureRecognizer *tapGesture2
= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKey)];
[self.view addGestureRecognizer:tapGesture2];
-(void)closeKey{
[self.view endEditing:YES];
}
- UISwipeGestureRecognizer 輕掃
特有屬性: dirction:設(shè)置輕掃的方向胳泉,支持上宿亡,下来氧,左舒憾,右四個(gè)方向稚疹。
(設(shè)置四個(gè)方向的輕掃)
for (int i=0; i<4; i++) {
UISwipeGestureRecognizer *swipeGesture
= [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandle:)];
[swipeGesture setDirection:1<<i];
[self.view addGestureRecognizer:swipeGesture];
}
-(void)swipeHandle:(UISwipeGestureRecognizer *)recognizer{
switch (recognizer.direction) {
case UISwipeGestureRecognizerDirectionUp:
NSLog(@"向上滑");
break;
case UISwipeGestureRecognizerDirectionDown:
NSLog(@"向下滑");
break;
case UISwipeGestureRecognizerDirectionRight:
NSLog(@"向右滑");
break;
case UISwipeGestureRecognizerDirectionLeft:
NSLog(@"向左滑");
break;
default:
break;
}
}
- 拖動(dòng)時(shí)候肯定有個(gè)速度居灯,返回值就是你拖動(dòng)時(shí)X和Y軸上的速度,速度是矢量内狗,有方向怪嫌。
CGPoint velocity = [(UIPanGestureRecognizer *)gestureRecognizer velocityInView:self.view];
參考資料: