UIGestureRecognizer 手势识别器 抽象类
手势: 有规律的触摸
七种手势: 轻拍(tap) 长按(longPress) 旋转(rotation)
捏合(pinch) 拖拽(pan) 轻扫(swipe)
屏幕边缘拖拽(screenEdgePan)
1.先创建一个图片,在图片上实现手势功能
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375, 667)];
imageView.center = self.view.center;
imageView.image = [UIImage imageNamed:@"h5.jpeg"];
[self.view addSubview:imageView];
[imageView release];
// 打开用户交互
imageView.userInteractionEnabled = YES;
2.轻拍tap
获取到轻拍手势时 让self调用tapAction:方法
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
// 添加手势
[imageView addGestureRecognizer:tap];
// 内存管理
[tap release];
//点击次数
tap.numberOfTapsRequired = 2;
// 手指个数
tap.numberOfTouchesRequired = 2;
- (void)tapAction:(UITapGestureRecognizer *)tap
{
NSLog(@"轻拍");
}
3.长按longPress
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
[imageView addGestureRecognizer:longPress];
[longPress release];
// 长按时间
longPress.minimumPressDuration = 1;
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
// 长按手势是很多手势的基础
// 通过状态区分
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按");
}
}
4.旋转rotation
UIRotationGestureRecognizer *rotain = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[imageView addGestureRecognizer:rotain];
[rotain release];
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation
{
// UIView transform属性 专门用来进行形变(位置position/旋转rotation/缩放scale)设置
// 获取当前手势触发的视图
UIImageView *imgView = (UIImageView *)rotation.view;
// 设置transform变化旋转
imgView.transform = CGAffineTransformMakeRotation(rotation.rotation);
NSLog(@"旋转");
}
5.捏合pinch
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[imageView addGestureRecognizer:pinch];
[pinch release];
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch{
// 获取view
UIImageView *imgView = (UIImageView *)pinch.view;
// 缩放scale(比例)
imgView.transform = CGAffineTransformScale(imgView.transform, pinch.scale, pinch.scale);
pinch.scale = 1;
NSLog(@"捏合");
}