- (void)viewDidLoad
{
[super viewDidLoad];
_rotateImageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
_rotateImageview.image = [UIImage imageNamed:@"1.png"];
_rotateImageview.userInteractionEnabled = YES;
[self.view addSubview:_rotateImageview];
// Tap 单击手势
UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
[_rotateImageview addGestureRecognizer:singleTapGestureRecognizer];
// Tap 双击手势
UITapGestureRecognizer *doubleGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTap:)];
doubleGestureRecognizer.numberOfTapsRequired = 2;
// [doubleGestureRecognizer setNumberOfTouchesRequired:2]; //两根手指点击
[_rotateImageview addGestureRecognizer:doubleGestureRecognizer];
/*单击手势和双击手势共存问题*/
// 没有检测到doubleTapGestureRecognizer或者检测doubleTapGestureRecognizer失败singleTapGestureRecognizer才有效
[singleTapGestureRecognizer requireGestureRecognizerToFail:doubleGestureRecognizer];
// LongPress 长按手势
UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
[_rotateImageview addGestureRecognizer:longPressGestureRecognizer];
// Pan 拖动手势
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePan:)];
[_rotateImageview addGestureRecognizer:panGestureRecognizer];
// Pinch 缩放手势
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:@selector(handlePinch:)];
[_rotateImageview addGestureRecognizer:pinchGestureRecognizer];
// Rotation 旋转手势
UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleRotate:)];
[_rotateImageview addGestureRecognizer:rotateRecognizer];
}
#pragma mark 单击
- (void)handleTap:(UITapGestureRecognizer *)recognizer {
NSLog(@"图片被单击");
}
#pragma mark 双击
- (void)handleDoubleTap:(UITapGestureRecognizer *)recognizer {
NSLog(@"图片被双击");
}
// 其实时因为响应不同的状态。所以,要在你的方法里加上状态的判断。可以看到,开始响应,结束响应,如果你不判断的话,都会调用你的方法。
#pragma mark 长按手势
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
NSLog(@"图片被长按");
}
}
#pragma mark 拖动手势
- (void)handlePan:(UIPanGestureRecognizer*) recognizer
{
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointZero inView:self.view];
}
#pragma mark 缩放手势
- (void)handlePinch:(UIPinchGestureRecognizer*) recognizer
{
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;
}
#pragma mark 旋转手势
- (void)handleRotate:(UIRotationGestureRecognizer*) recognizer
{
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
recognizer.rotation = 0;
}