手勢(shì)的分類(lèi)
(一) 手勢(shì)分類(lèi)
state 狀態(tài) (UIGestureRecognizerStateBegan Change ended)不同狀態(tài)
view當(dāng)前被點(diǎn)擊的View
- tap 輕觸
- tap.numberOfTapsRequired = 2 連續(xù)點(diǎn)擊次數(shù)
- tap.numberOfTouchesRequired = 2 手指的個(gè)數(shù)
- swipe 輕掃
direction = directionLeft | directionRight 但是 這會(huì)改變direction的值,所以一般是手動(dòng)添加多個(gè) - pan 拖拽
CGPoint panPoint = [ pan translationInView : pan.view ] - rotate 旋轉(zhuǎn)
rotation 角度 每一次的調(diào)度變化都會(huì)累加的 - pinch 捏合
scale 放大比例 每一縮放都會(huì)累加 - longPress 長(zhǎng)按
- minimumPressDuration 長(zhǎng)按多長(zhǎng)時(shí)間觸發(fā)
- allowableMovement 誤差值
- if (longPress . state == UIGestureRecognizerStateBegan ){ //長(zhǎng)按開(kāi)始 執(zhí)行 } 分狀態(tài)執(zhí)行方法
拖拽簡(jiǎn)單使用
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *purpleView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
_imageView.userInteractionEnabled = YES;
_imageView.multipleTouchEnabled = YES;
// 輕觸
// 1. 實(shí)例化手勢(shì)識(shí)別對(duì)象
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[_imageView addGestureRecognizer:pan];
}
// 3. 實(shí)現(xiàn)監(jiān)聽(tīng)方法
-(void)pan:(UIPanGestureRecognizer *)pan {
CGPoint translatePoint = [pan translationInView:pan.view];
// 讓view進(jìn)行移動(dòng)
pan.view.transform = CGAffineTransformTranslate(pan.view.transform,translatePoint.x, translatePoint.y);
[pan setTranslation:CGPointZero inView:pan.view];
}
@end
旋轉(zhuǎn)的簡(jiǎn)單使用
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *purpleView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
_imageView.userInteractionEnabled = YES;
_imageView.multipleTouchEnabled = YES;
// 輕觸
// 1. 實(shí)例化手勢(shì)識(shí)別對(duì)象
UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[_imageView addGestureRecognizer:rotate];
}
// 3. 實(shí)現(xiàn)監(jiān)聽(tīng)方法
-(void)rotate:(UIRotationGestureRecognizer *)rotate {
NSLog(@"------ %f", rotate.rotation);
// 如果手指頭停止旋轉(zhuǎn), 當(dāng)再次旋轉(zhuǎn)的時(shí)候 就會(huì)從0 開(kāi)始
// rotate.view.transform = CGAffineTransformMakeRotation(rotate.rotation);
rotate.view.transform = CGAffineTransformRotate(rotate.view.transform, rotate.rotation);
// 每一次旋轉(zhuǎn)之后, 旋轉(zhuǎn)的角度都從0開(kāi)始計(jì)算
rotate.rotation = 0;
}
@end