打开图片交互信息,默认关闭
self.imageView.userInteractionEnabled = YES;
把gesture加到指定的视图上
[self.imageView addGestureRecognizer:tap];
[tap release];
手势类型:
1.轻拍tap
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
触发一次事件需要点击次数
tap.numberOfTapsRequired = 2;
触发一次事件需要的手指数
tap.numberOfTouchesRequired = 2;
2.长按 longPress
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
长按触发的时间
longPress.minimumPressDuration = 3;
3.旋转 rotation
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation{
通过gesture,知道手势所在的视图
UIImageView *imageView = (UIImageView *)rotation.view;
imageView.transform 调整视图旋转/缩放/大小/位置偏移
imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);
//控制转速
rotation.rotation = 0;
}
4.缩放
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch{
UIImageView *imageView = (UIImageView *)pinch.view;
imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale); 当前图的属性
设置实现缓慢缩放效果
pinch.scale = 1;
}
5.拖拽
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
- (void)panAction:(UIPanGestureRecognizer *)pan{
UIImageView *imageView = (UIImageView *)pan.view;
通过gesture来获取移动的点
CGPoint p = [pan translationInView:imageView];
通过设置transform 实现拖拽
imageView.transform = CGAffineTransformTranslate(imageView.transform, p.x, p.y);
和置0道理一样, 变化一点,停止下,重新置0,否则运动越来越快
[pan setTranslation:CGPointZero inView:imageView];
}
6.轻扫/划一下
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
设置轻扫方向
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe{
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"向左进行轻扫");
}
}
scale 比例
Affinity 类同、关系
transform 改变
translate 翻译
swipe 挥、划
UserInteraction 用户交互
opaque:不透明的