- 轻击 UITapGestureRecognizer
- 长按 UILongPressGestureRecognizer
- 轻扫 UISwipeGestureRecognizer
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//轻击
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage)];
tap.delegate = self;
[self.imageView addGestureRecognizer:tap];
//长按
UILongPressGestureRecognizer *lPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.imageView addGestureRecognizer:lPress];
//轻扫
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self.imageView addGestureRecognizer:leftSwipe];
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[self.imageView addGestureRecognizer:rightSwipe];
UISwipeGestureRecognizer *upSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
upSwipe.direction = UISwipeGestureRecognizerDirectionUp;
[self.imageView addGestureRecognizer:upSwipe];
UISwipeGestureRecognizer *downSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
downSwipe.direction = UISwipeGestureRecognizerDirectionDown;
[self.imageView addGestureRecognizer:downSwipe];
}
//轻击
-(void)tapImage{
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//layer层的3D变换
//self.imageView.layer.transform = CATransform3DMakeRotation(M_PI, 1, 1, 0);
self.imageView.layer.transform = CATransform3DRotate(self.imageView.layer.transform, M_PI, 0, 0, 0);
//imageView的2D变换
if (imageWidth < screenWidth) {
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, screenWidth/imageWidth, screenWidth/imageWidth);
}else{
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, 180/imageWidth, 180/imageWidth);
}
} completion:nil];
}
//轻击代理
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
CGPoint point = [touch locationInView:self.imageView];
if (point.x > 180*0.5) {
return YES;
}else{
return NO;
}
}
//长按
-(void)longPress:(UILongPressGestureRecognizer *)longP{
if(longP.state==UIGestureRecognizerStateBegan){ //开始长按
[UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.imageView.transform = CGAffineTransformMakeScale(screenWidth/imageWidth, screenWidth/imageWidth);
} completion:^(BOOL finished) {}];
}else if (longP.state==UIGestureRecognizerStateChanged){ //开始移动
}else if (longP.state==UIGestureRecognizerStateEnded){ //结束长按
self.imageView.transform = CGAffineTransformMakeScale(1, 1);
}
}
-(void)swipe:(UISwipeGestureRecognizer *)swipe{
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) { //左扫
NSLog(@"左扫");
} else if(swipe.direction == UISwipeGestureRecognizerDirectionRight){ //右扫
NSLog(@"右扫");
} else if(swipe.direction == UISwipeGestureRecognizerDirectionUp){
NSLog(@"上扫");
} else if(swipe.direction == UISwipeGestureRecognizerDirectionDown){
NSLog(@"下扫");
}
}
@end
- 拖动 UIPanGestureRecognizer
- 缩放 UIPinchGestureRecognizer
- 旋转 UIRotationGestureRecognizer
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.imageView.userInteractionEnabled = YES;
//拖动
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.imageView addGestureRecognizer:pan];
//缩放
UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
pinch.delegate = self;
[self.imageView addGestureRecognizer:pinch];
//旋转
UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
[self.imageView addGestureRecognizer:rotation];
}
-(void)pan:(UIPanGestureRecognizer *)pan{
CGPoint point = [pan translationInView:self.imageView];
//self.imageView.transform = CGAffineTransformMakeTranslation(point.x, point.y);
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, point.x, point.y);
[pan setTranslation:CGPointZero inView:self.imageView]; //将imageView的translation以当前点作为零点
}
-(void)pinch:(UIPinchGestureRecognizer *)pinch{
//self.imageView.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
[pinch setScale:1];
}
-(void)rotation:(UIRotationGestureRecognizer *)rotation{
//self.imageView.transform = CGAffineTransformMakeRotation(rotation.rotation);
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
[rotation setRotation:0];
}
//UIGestureRecognizerDelegate: shouldRecognizeSimultaneously
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
@end