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