iOS 手势之左右滑动UISwipeGestureRecognizer

做个左右滑动手势.

 

1. 在"ViewController.h"中增加两个手势property.

 

@property (nonatomic, strong) UISwipeGestureRecognizer *leftSwipeGestureRecognizer;

@property (nonatomic, strong) UISwipeGestureRecognizer *rightSwipeGestureRecognizer;

并synthesize到"ViewController.m"文件中.

 

2.在"ViewController.m"文件中的"ViewDidLoad"方法中增加如下代码:

 

self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];

    self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];

     

    self.leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;

    self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;

     

    [self.view addGestureRecognizer:self.leftSwipeGestureRecognizer];

    [self.view addGestureRecognizer:self.rightSwipeGestureRecognizer];

 

3.并在"ViewController.m"中增加如下方法;

- (void)handleSwipes:(UISwipeGestureRecognizer *)sender

{

    if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {

        CGPoint labelPosition = CGPointMake(self.swipeLabel.frame.origin.x - 100.0, self.swipeLabel.frame.origin.y);

        self.swipeLabel.frame = CGRectMake( labelPosition.x , labelPosition.y , self.swipeLabel.frame.size.width, self.swipeLabel.frame.size.height);

        self.swipeLabel.text = @"往左边....";

    }

     

    if (sender.direction == UISwipeGestureRecognizerDirectionRight) {

        CGPoint labelPosition = CGPointMake(self.swipeLabel.frame.origin.x + 100.0, self.swipeLabel.frame.origin.y);

        self.swipeLabel.frame = CGRectMake( labelPosition.x , labelPosition.y , self.swipeLabel.frame.size.width, self.swipeLabel.frame.size.height);

        self.swipeLabel.text = @"往右边....";

    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值