TAG

RSS订阅

收藏本站

设为首页

当前位置:主页 > 移动开发 > IOS开发 >

IOS 手势之左右滑动UISwipeGestureRecognizer

发布时间:2014-11-28 15:02 类别:IOS开发

做个左右滑动手势.

 

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 = @"尼玛的, 你在往右边跑啊....";

    }

}

 

4. 保存, 编译, 运行....

猜你会喜欢....

Copyright © 2015 www.wahenzan.com 哇!很赞 版权所有 浙ICP备14030256号-1 Power by DedeCms

浙公网安备 33010602005986号

声明:本站所有文章除标明原创外,均来自网络转载,版权归原作者所有,如果有侵犯到您的权益,请联系本站删除 网站管理员:758763728

360网站安全检测平台