ios 常见手势

下面来一起学习下常见的手势:

UIPanGestureRecognizer(拖动)
UIPinchGestureRecognizer(捏合)
UIRotationGestureRecognizer(旋转)
UITapGestureRecognizer(点按)
UILongPressGestureRecognizer(长按)
​UISwipeGestureRecognizer(轻扫)

1 、UISwipeGestureRecognizer 轻扫手势

UISwipeGestureRecognizer 我们有两个属性:numberOfTouchesRequired和direction
direction 是轻扫的方向默认UISwipeGestureRecognizerDirectionRight;
numberOfTouchesRequired 是轻扫手指的数量 默认是1;

代码使用

//轻扫
- (void)createSwipeGesture:(UIView *)view {
    UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
    //轻扫手指的数量
    swipe.numberOfTouchesRequired = 1;
    //轻扫的方向默认UISwipeGestureRecognizerDirectionRight
//    swipe.direction = UISwipeGestureRecognizerDirectionRight;//向右
//    swipe.direction = UISwipeGestureRecognizerDirectionUp;//向上
//    swipe.direction = UISwipeGestureRecognizerDirectionLeft;//向左
    swipe.direction = UISwipeGestureRecognizerDirectionDown;//向下
    [view addGestureRecognizer:swipe];
}

- (void)swipeGesture:(UIGestureRecognizer *)swipe {
    UISwipeGestureRecognizer * gesture = (UISwipeGestureRecognizer *)swipe;
    if (gesture.direction == UISwipeGestureRecognizerDirectionUp) {
        NSLog(@"向上轻扫");
    } else if (gesture.direction == UISwipeGestureRecognizerDirectionDown) {
        NSLog(@"向下轻扫");
    } else if (gesture.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"向左轻扫");
    } else {
        NSLog(@"向右轻扫");
    }
}

2 、UITapGestureRecognizer 点按手势
UITapGestureRecognizer 属性 numberOfTapsRequired和numberOfTouchesRequired
@property (nonatomic) NSUInteger numberOfTapsRequired; //点击次数 默认是1
@property (nonatomic) NSUInteger numberOfTouchesRequired//点击手指 默认是1

代码使用

//点按
- (void)createTapGesture:(UIView *)view {
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    tap.numberOfTouchesRequired = 1;
    tap.numberOfTapsRequired = 2;//双击
    [view addGestureRecognizer:tap];
}

- (void)tapGesture:(UIGestureRecognizer *)tap {
    UITapGestureRecognizer * gesture = (UITapGestureRecognizer *)tap;
    NSLog(@"点击了%lu次触发",(unsigned long)gesture.numberOfTapsRequired);
}

numberOfTapsRequired上面设置的是2 双击 ,单击的时候 并不会执行tapGesture 方法

3 、UILongPressGestureRecognizer 长按手势
UILongPressGestureRecognizer 长按也可以拖拽,常用的属性 minimumPressDuration、numberOfTouchesRequired、numberOfTapsRequired、allowableMovement

numberOfTapsRequired://长点击响应前点击次数,默认0;
numberOfTouchesRequired://用户触摸的手指数,默认1
minimumPressDuration://指定用户至少在屏幕上按下多少秒才会触发该长按手势。该默认值为0.5
allowableMovement//手指长按期间可移动的区域,默认10像素

代码使用

//长按
- (void)createLongGesture:(UIView *)view {
    UILongPressGestureRecognizer * longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longGesture:)];
    longGesture.numberOfTapsRequired = 0;//长点击响应前点击次数,默认0;
    longGesture.numberOfTouchesRequired = 1;// 用户触摸的手指数,默认1
    longGesture.minimumPressDuration = 0.5;//长按最低时间,默认0.5秒
    longGesture.allowableMovement = 5;//手指长按期间可移动的区域,默认10像素
    [view addGestureRecognizer:longGesture];
}

- (void)longGesture:(UILongPressGestureRecognizer *)longGesture {
    if (longGesture.state == UIGestureRecognizerStateBegan)
    {
        NSLog(@"开始");
    }
    else if (longGesture.state == UIGestureRecognizerStateChanged)
    {
        NSLog(@"进行中");
    }
    else if (longGesture.state == UIGestureRecognizerStateEnded)
    {
        NSLog(@"结束");
    }
}

4、UIPanGestureRecognizer 拖拽手势
UIPanGestureRecognizer 属性
minimumNumberOfTouches//最小的拖动范围
maximumNumberOfTouches //最大拖动范围
方法
// 获取移动后相对与view的坐标系的偏移量

  • (CGPoint)translationInView:(nullable UIView *)view;
    // 获取移动后的坐标
  • (void)setTranslation:(CGPoint)translation inView:(nullable UIView *)view;

代码实现

//拖动
- (void)createPanGesture:(UIView *)view {
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(gestureEvent:)];
    [view addGestureRecognizer:pan];
}

//拖动
- (void)gestureEvent:(UIGestureRecognizer *)sender {
    UIView *btn = (UIView *)sender.view;
    if (sender.state == UIGestureRecognizerStateBegan)
    {
        startPoint = [sender locationInView:sender.view];
        originPoint = btn.center;
    }
    else if (sender.state == UIGestureRecognizerStateChanged)
    {
        CGPoint newPoint = [sender locationInView:sender.view];
        CGFloat deltaX = newPoint.x-startPoint.x;
        CGFloat deltaY = newPoint.y-startPoint.y;
        btn.center = CGPointMake(btn.center.x+deltaX,btn.center.y+deltaY);
        [UIView animateWithDuration:1 animations:^{
            CGPoint temp = CGPointZero;
            temp = btn.center;
            btn.center = temp;
            originPoint = btn.center;
        }];
    }
    else if (sender.state == UIGestureRecognizerStateEnded)
    {
        [UIView animateWithDuration:1 animations:^{
            if (originPoint.x - btn.frame.size.width/2<=0) {
                originPoint.x = btn.frame.size.width/2 + 10;
            }
            if (originPoint.y  - btn.frame.size.height/2 - 20 <=0) {
                originPoint.y = btn.frame.size.height/2 + 44 + 10;
            }
            if (originPoint.x >= [UIScreen mainScreen].bounds.size.width- btn.frame.size.width/2) {
                originPoint.x = [UIScreen mainScreen].bounds.size.width - btn.frame.size.width/2 - 10;
            }
            if (originPoint.y >= [UIScreen mainScreen].bounds.size.height- btn.frame.size.height/2) {
                originPoint.y = [UIScreen mainScreen].bounds.size.height- btn.frame.size.height/2 - 10;
            }
            btn.center = originPoint;
        }];
    }
}

5 、UIPinchGestureRecognizer 捏合手势
UIPinchGestureRecognizer 的属性 有 scale 和 velocity:
scale //是捏合的比例;
velocity //捏合的速度
代码实现

//捏合
- (void)createPinchGesture:(UIView *)view {
    //缩放手势
    UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc]
                                                        initWithTarget:self
                                                        action:@selector(handlePinch:)];
    
    [view addGestureRecognizer:pinchGestureRecognizer];
}
-(void)handlePinch:(UIPinchGestureRecognizer *)sender
{
//scale 缩放比例
//    sender.view.transform = CGAffineTransformMake(sender.scale, 0, 0, sender.scale, 0, 0);
//每次缩放以原来位置为标准
//    sender.view.transform = CGAffineTransformMakeScale(sender.scale, sender.scale);

//每次缩放以上一次为标准
sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale);
//重新设置缩放比例 1是正常缩放.小于1时是缩小(无论何种操作都是缩小),大于1时是放大(无论何种操作都是放大)
sender.scale = 1;
}

6、UIRotationGestureRecognizer 旋转手势
UIRotationGestureRecognizer属性
@property (nonatomic) CGFloat rotation;//旋转角度
@property (nonatomic,readonly) CGFloat velocity;//旋转速度 只读

代码

//旋转
- (void)createRotationGesture:(UIView *)view {
    UIRotationGestureRecognizer * rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
    [view addGestureRecognizer:rotationGesture];
}

- (void)rotationGesture :(UIRotationGestureRecognizer *) rotationGesture{
    //捏合手势两种改变方式
    //以原来的位置为标准
//    rotationGesture.view.transform = CGAffineTransformMakeRotation(rotationGesture.rotation);//rotation 是旋转角度
    //两个参数,以上位置为标准
    rotationGesture.view.transform = CGAffineTransformRotate(rotationGesture.view.transform, rotationGesture.rotation);
    //消除增量
    rotationGesture.rotation = 0.0;
}

对应demo下载地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值