手势的分类
(一) 手势分类
state 状态 (UIGestureRecognizerStateBegan Change ended)不同状态
view当前被点击的View
- tap 轻触
- tap.numberOfTapsRequired = 2 连续点击次数
- tap.numberOfTouchesRequired = 2 手指的个数
- swipe 轻扫
direction = directionLeft | directionRight 但是 这会改变direction的值,所以一般是手动添加多个 - pan 拖拽
CGPoint panPoint = [ pan translationInView : pan.view ] - rotate 旋转
rotation 角度 每一次的调度变化都会累加的 - pinch 捏合
scale 放大比例 每一缩放都会累加 - longPress 长按
- minimumPressDuration 长按多长时间触发
- allowableMovement 误差值
- if (longPress . state == UIGestureRecognizerStateBegan ){ //长按开始 执行 } 分状态执行方法
拖拽简单使用
@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. 实现监听方法
-(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
旋转的简单使用
@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. 实现监听方法
-(void)rotate:(UIRotationGestureRecognizer *)rotate {
NSLog(@"------ %f", rotate.rotation);
// 如果手指头停止旋转, 当再次旋转的时候 就会从0 开始
// rotate.view.transform = CGAffineTransformMakeRotation(rotate.rotation);
rotate.view.transform = CGAffineTransformRotate(rotate.view.transform, rotate.rotation);
// 每一次旋转之后, 旋转的角度都从0开始计算
rotate.rotation = 0;
}
@end
网友评论