GestureRecognizer手势

打开图片交互信息,默认关闭

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:不透明的
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
苹果提供了UIGestureRecognizer类,可以用来实现自定义手势。下面是一个简单的例子,实现在屏幕上绘制一个“V”形手势来触发事件: 1. 首先在视图控制器中创建一个手势识别器对象: ``` @property (nonatomic, strong) UIGestureRecognizer *gestureRecognizer; ``` 2. 在viewDidLoad方法中,创建手势识别器并将其添加到视图上: ``` self.gestureRecognizer = [[UIGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; [self.view addGestureRecognizer:self.gestureRecognizer]; ``` 3. 实现手势处理方法,当手势被识别时,执行自定义的事件: ``` - (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer { if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { NSArray *points = [gestureRecognizer valueForKey:@"touches"]; CGPoint startPoint = [[points firstObject] locationInView:self.view]; CGPoint endPoint = [[points lastObject] locationInView:self.view]; // 计算手势的方向 CGFloat dx = endPoint.x - startPoint.x; CGFloat dy = endPoint.y - startPoint.y; if (dx > 0 && dy < 0 && fabs(dx) > fabs(dy)) { // 手势为向右上方,执行自定义事件 [self doSomething]; } } } ``` 在上面的代码中,我们使用了touches属性获取手势的起点和终点,然后计算手势的方向,最后根据手势的方向执行自定义事件。 注意:在创建手势识别器时,需要指定手势的类型,例如: ``` UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:swipeRecognizer]; ``` 上面的代码创建了一个向右滑动的手势识别器,并将其添加到视图上。当手势被识别时,会执行handleSwipe:方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值