只有多尝试,多写才能更好。
// 手势识别器
// 1.轻拍手势
// 手势需要在定义时绑定一个触发方法(SEL)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
// 轻拍的设置
// 需要轻拍两次 才响应事件
tap.numberOfTapsRequired = 2;
// 需要用几个手指去拍
tap.numberOfTouchesRequired = 2;
// 给view添加手势
[imageView addGestureRecognizer:tap];
[tap release];
// 2.长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
// 长按 触发方法 所需要的事件
longPress.minimumPressDuration = 3;
// 长按 允许用户移动手指的距离
longPress.allowableMovement = 100;
[imageView addGestureRecognizer:longPress];
[longPress release];
// 3.轻扫手势 (swipe)
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
// 设置 轻扫 的方向 (一个对象只能一个方向 要不左右 要不上下)!!!
swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
[imageView addGestureRecognizer:swipe];
// 4. 拖拽
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[imageView addGestureRecognizer:pan];
// 5.旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(roration:)];
[imageView addGestureRecognizer:rotation];
// 6.捏合手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[imageView addGestureRecognizer:pinch];
// 7.屏幕边缘拖拽
UIScreenEdgePanGestureRecognizer *sceenEdge = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(sceenEdgeAction:)];
sceenEdge.edges = UIRectEdgeLeft;
[imageView addGestureRecognizer:sceenEdge];
// 将UIImageView的用户交互打开,使他能响应轻拍
[imageView setUserInteractionEnabled:YES];
- (void)tapAction:(UITapGestureRecognizer *)tap
{
NSLog(@"轻拍");
}
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
// 当长按手势开始触发时
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按");
}
}
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"轻扫");
}
- (void)panAction:(UIPanGestureRecognizer *)pan
{
NSLog(@"拖拽");
// 通过手势的view属性 获取当前手势添加到的 view
UIImageView *imageView = (UIImageView *) pan.view;
// 获取到 当前手指接触的点
CGPoint point = [pan translationInView:imageView];
// 让view变形
imageView.transform = CGAffineTransformTranslate(imageView.transform, point.x, point.y);
[pan setTranslation:CGPointZero inView:imageView];
}
- (void)roration:(UIRotationGestureRecognizer *)rotation
{
NSLog(@"旋转");
UIImageView *imageView = (UIImageView *) rotation.view;
imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);
rotation.rotation = 0;
}
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
NSLog(@"捏合");
UIImageView *imageView = (UIImageView *) pinch.view;
// 在x,y轴方向 放大/缩小
imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
pinch.scale = 1;
}
- (void)sceenEdgeAction:(UIScreenEdgePanGestureRecognizer *)sceen
{
NSLog(@"屏幕边缘");
}