目录
1.1 点击手势
1.2 长按手势
1.3 移动手势
1.4 旋转手势
1.5 缩放手势
1.6 方向滑动手势
1.1 点击手势
//轻触 彩蛋
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
//触摸次数
tap.numberOfTapsRequired = 2;
//触摸点数量
//tap.numberOfTouchesRequired = 2;
[imageView addGestureRecognizer:tap];
1.2 长按手势
UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[imageView addGestureRecognizer:longPress];
//长按事件
- (void)longPress:(UILongPressGestureRecognizer*)longPress{
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"开始长按");
}
if (longPress.state == UIGestureRecognizerStateChanged) {
NSLog(@"长按中");
}
if (longPress.state == UIGestureRecognizerStateEnded) {
NSLog(@"长按结束");
}
}
1.3 移动手势
UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[imageView addGestureRecognizer:pan];
//移动事件
- (void)pan:(UIPanGestureRecognizer*)pan{
CGPoint point = [pan translationInView:self.view];
imageView.center = CGPointMake(imageView.center.x + point.x, imageView.center.y + point.y);
//以当前点为原点计算
[pan setTranslation:CGPointZero inView:self.view];
}
1.4 旋转手势
UIRotationGestureRecognizer* rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate=self;
[imageView addGestureRecognizer:rotation];
- (void)rotation:(UIRotationGestureRecognizer*)rotation{
_imageView.transform = CGAffineTransformMakeRotation(_rotation + rotation.rotation);
if (rotation.state == UIGestureRecognizerStateEnded) {
_rotation += rotation.rotation;
}
}
1.5 缩放手势
UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
pinch.delegate=self;
[imageView addGestureRecognizer:pinch];
- (void)pinch:(UIPinchGestureRecognizer*)pinch{
//1.1 1.1
_imageView.bounds = CGRectMake(0, 0, _imageView.bounds.size.width * pinch.scale, _imageView.bounds.size.height * pinch.scale);
[pinch setScale:1];
}
1.6 方向滑动手势
需要分开写
self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
self.leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:self.leftSwipeGestureRecognizer];
[self.view addGestureRecognizer:self.rightSwipeGestureRecognizer];
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender
{
if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
}
if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
}
}