码迷,mamicode.com
首页 > 移动开发 > 详细

IOS 手势之左右滑动

时间:2015-05-04 18:11:11      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:ios开发   ios   手势   

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

?
1
2
@property (nonatomic, strong) UISwipeGestureRecognizer *leftSwipeGestureRecognizer;
@property (nonatomic, strong) UISwipeGestureRecognizer *rightSwipeGestureRecognizer;
并synthesize到"ViewController.m"文件中.


技术分享

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


?
1
2
3
4
5
6
7
8
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"中增加如下方法;


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (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 = @"尼玛的, 你在往右边跑啊....";

IOS 手势之左右滑动

标签:ios开发   ios   手势   

原文地址:http://blog.csdn.net/yangchen9931/article/details/45482629

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!