IOS添加滑动手势

添加滑动手势有两种方法
方法一:

添加手势不分方向, 左 右 上 下滑动都会调用方法

UIPanGestureRecognizer *removeSelfView = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(removeSelfView:)];
    [self addGestureRecognizer:removeSelfView];

- (void)removeSelfView:(UIPanGestureRecognizer *)gesture
{
    NSLog(@"滑动方法调用了");
    [self removeFromSuperview];
}

方法二:
添加可以区分方向的手势 特定滑动方向时  才会调用相应的操作

UISwipeGestureRecognizer *left = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipes:)];
    left.direction=UISwipeGestureRecognizerDirectionLeft;
    [self addGestureRecognizer:left];
    
    UISwipeGestureRecognizer *right = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipes:)];
    right.direction=UISwipeGestureRecognizerDirectionRight;
    [self addGestureRecognizer:right];
    
    UISwipeGestureRecognizer *up = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipes:)];
    up.direction=UISwipeGestureRecognizerDirectionUp;
    [self addGestureRecognizer:up];
    
    UISwipeGestureRecognizer *down = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipes:)];
    down.direction=UISwipeGestureRecognizerDirectionLeft;
    [self addGestureRecognizer:down];

-(void)handleSwipes:(UISwipeGestureRecognizer *)recognizer
{
    if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft){
        NSLog(@"向左边滑动了!!!!!!");
    }
    if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"向右边滑动了!!!!!!");
    }
    if (recognizer.direction == UISwipeGestureRecognizerDirectionUp) {
        NSLog(@"向上边滑动了!!!!!!");
    }
    if (recognizer.direction == UISwipeGestureRecognizerDirectionDown) {
        NSLog(@"向下边滑动了!!!!!!");
    }

}


使用手势很简单,分为三步:
    1    创建手势识别器对象实例。创建时,指定一个回调方法,当手势开始,改变、或结束时,执行回调方法。
    2    设置手势识别器对象实例的相关属性(可选部分)
    3    添加到需要识别的 View 中。每个手势只对应一个 View,当屏幕触摸在 View 的边界内时,如果手势和预定的一样,那就会执行回调方法。
PS:一个手势只能对应一个 View,但是一个 View 可以有多个手势。建议在真机上测试这些手势,模拟器操作不太方便,可能导致认为手势失效的情况。(模拟器测试捏合和旋转手势时,按住 option 键,再用触摸板或鼠标操作)




  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值