iOS——常用的手势总结

iOS——常用的手势总结

前言

我们在平时玩手机的时候都知道关于手势的重要性,在加入手势的应用时,使我们的App变得更加方便使用,那么具体iOS有几种常用的手势呢?下面我简单的总结一下那些关于iOS最常用的手势。关于手势的应用iOS提供了UIGestureRecognizer类。手势识别UIGestureRecognizer类是个抽象类,下面的子类是具体的手势,开发这可以直接使用这些手势识别:
在这里插入图片描述

使用手势的步骤

使用手势很简单,分为两步:

  1. 创建手势实例。当创建手势时,指定一个回调方法,当手势开始,改变、或结束时,回调方法被调用。
  2. 添加到需要识别的View中。每个手势只对应一个View,当屏幕触摸在View的边界内时,如果手势和预定的一样,那就会回调方法。

需要注意的是:一个手势只能对应一个View,但是一个View可以有多个手势。

UIPanGestureRecognizer (拖动手势)

新建一个ImageView,然后添加手势

UIImageView *panImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pan.png"]];  
panImageView.frame = CGRectMake(50, 50, 100, 160);  
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]  
                                                initWithTarget:self  
                                                action:@selector(handlePan:)];      
[panImageView addGestureRecognizer:panGestureRecognizer];  
[self.view setBackgroundColor:[UIColor whiteColor]];  
[self.view addSubview:panImageView];  

它 的回调方法:

- (void) handlePan:(UIPanGestureRecognizer*) recognizer  
{  
    CGPoint translation = [recognizer translationInView:self.view];  
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,  
                                   recognizer.view.center.y + translation.y);  
    [recognizer setTranslation:CGPointZero inView:self.view];  
      
}  

UIPinchGestureRecognizer(缩放手势)

//创建手势对象
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pichGestureClick:)];

//添加到视图
[imageView addGestureRecognizer:pinch];

关联方法:

- (void)pichClick:(UIPinchGestureRecognizer *)pinch {
    //缩放的系数
    NSLog(@"%.2lf", pinch.scale);
    //固定写法
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    //重置缩放系数(否则系数会累加)
    pinch.scale = 1.0;
}

UIRotationGestureRecognizer(旋转手势)

//创建手势对象
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureClick:)];

//添加到视图
[imageView addGestureRecognizer:rotation];

关联方法:


- (void)rotationClick:(UIRotationGestureRecognizer *)rotation {
    //rotation.rotation 手势旋转的角度
    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
    //重置角度
    rotation.rotation = 0;
}

UITapGestureRecognizer (点按手势)

//创建手势对象
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];

//设置相关属性
//点击次数(默认1)
tap.numberOfTapsRequired = 1;
//手指的个数(默认1)
tap.numberOfTouchesRequired = 1;
//添加到视图
[testView addGestureRecognizer:tap];

关联方法:

- (void)tapClick:(UITapGestureRecognizer *)tap{
    NSLog(@"点按手势响应!");
}

添加两个ImagView并添加手势

前面说过一个手势只能对应一个View,但是一个View可以有多个手势,那么如何将两个View都设定同样的手势呢?

- (void)viewDidLoad  
{  
    [super viewDidLoad];  
  
    UIImageView *image1ImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1.png"]];  
    UIImageView *image2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image2.png"]];  
    snakeImageView.frame = CGRectMake(120, 120, 100, 160);  
    dragonImageView.frame = CGRectMake(50, 50, 100, 160);  
    [self.view addSubview:image1ImageView];  
    [self.view addSubview:image2ImageView];  
      
    for (UIView *view in self.view.subviews) {  
        UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]  
                                                        initWithTarget:self  
                                                        action:@selector(handlePan:)];  
          
        UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc]  
                                                            initWithTarget:self  
                                                            action:@selector(handlePinch:)];  
          
        UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc]  
                                                         initWithTarget:self  
                                                         action:@selector(handleRotate:)];  
          
        [view addGestureRecognizer:panGestureRecognizer];  
        [view addGestureRecognizer:pinchGestureRecognizer];  
        [view addGestureRecognizer:rotateRecognizer];  
        [view setUserInteractionEnabled:YES];  
    }  
    [self.view setBackgroundColor:[UIColor whiteColor]];       
}  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值