码迷,mamicode.com
首页 > 其他好文 > 详细

手势,轻拍(tapGesture)+轻扫(swipeGesture)

时间:2015-03-01 01:42:48      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:tapgesture、swipegesture

       在iOS开发时我们经常用到手势,手势有很多种,先介绍两种一个轻拍(UITapGestureRecognizer)还有一个轻扫(UISwipeGestureRecognizer)。


一,轻拍(UITapGestureRecognizer)


        我们给图片或者一个视图添加一个点击事件,一般都用UITapGestureRecognizer,因为手势有个属性view,可以获取当前手势下的视图,运用起来非常灵活,所以我比较习惯用轻拍手势,有时候我们会纠结该用轻拍手势还是按钮,其实两者之间做选择最好的方法就是我需不需要获得我们点击的视图,获取该视图的信息或者对该视图的某些属性(比如frame、layer(一般修改layer下的transform))进行修改,如果需要我们用手势更灵活,如果不需要我们用按钮就行。其实不管需不需要两者都可以,只是在需要的情况下用按钮的话,我们得设置tag值(本人觉得比用手势繁琐)也能得到一样的效果。下面简单说一下用法。


- (void)addImageViewAndAddTapGesture

{

    // 获取图片路径

    NSString *path =[[NSBundle mainBundle] pathForResource:@"image"ofType:@"jpg"];

    // 获得图片视图

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:path]];

    // 设置 frame,大小和坐标

    [imageView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    // 用户交互开启,默认是关闭,在关闭状态下图片不能点击

    [imageView setUserInteractionEnabled:YES];

    // 新建轻拍手势,初始化方法中设置 target action

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];

    // imageView添加手势

    [imageView addGestureRecognizer:tapGesture];

    // release

    [tapGesture release];

    // 添加图片

    [self.view addSubview:imageView];

    [imageView release];

}


轻拍图片后会执行 tapGestureAction:

可以添加我们想要的功能

- (void)tapGestureAction:(UITapGestureRecognizer *)tapGesture

{

    // 获取当前手势下的视图(返回值是UIView,所以需要强制类型转换)

    UIImageView *imageView = (UIImageView *)tapGesture.view;

    // 可以操作imageView,而且可以修改imageView的一些属性

}


二,轻扫(UISwipeGestureRecognizer)


- (void)addImageViewAndAddSwipeGesture

{

    // 获取图片路径

    NSString *path =[[NSBundle mainBundle] pathForResource:@"image"ofType:@"jpg"];

    // 获得图片视图

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:path]];

    // 设置 frame,大小和坐标

    [imageView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    // 用户交互开启,默认是关闭,在关闭状态下图片不能点击

    [imageView setUserInteractionEnabled:YES];

    // 新建轻扫手势,初始化方法中设置 target action

    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];

    // 需要设置轻扫的方向,一个轻扫手势只能设置一个,如果需要多个方向需要新建多个轻扫手势,这个问题一会儿说

    swipeGesture.direction = UISwipeGestureRecognizerDirectionDown;     // 向下方向

//    swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;     // 向左方向

//    swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;    // 向右方向

//    swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;       // 向上方向

    // imageView添加手势

    [imageView addGestureRecognizer:swipeGesture];

    // release

    [swipeGesture release];

    // 添加图片

    [self.view addSubview:imageView];

    [imageView release];

}


向下轻扫时执行swipeGestureAction:

- (void)swipeGestureAction:(UISwipeGestureRecognizer *)swipeGesture

{

    // 获取当前手势下的视图(返回值是UIView,所以需要强制类型转换)

    UIImageView *imageView = (UIImageView *)swipeGesture.view;

    // 可以操作imageView,而且可以修改imageView的一些属性

}


接下来说一下添加多个方向的手势:


- (void)addImageViewAndAddSwipeGesture

{

    NSString *path =[[NSBundle mainBundle] pathForResource:@"image"ofType:@"jpg"];

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:path]];

    [imageView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    [imageView setUserInteractionEnabled:YES];

    

    // 向下方向

    UISwipeGestureRecognizer *swipeGestureDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];

    swipeGestureDown.direction = UISwipeGestureRecognizerDirectionDown;     // 向下方向

    [imageView addGestureRecognizer:swipeGestureDown];

    [swipeGestureDown release];

    

    // 向左方向

    UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];

    swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft;     // 向左方向

    [imageView addGestureRecognizer:swipeGestureLeft];

    [swipeGestureLeft release];

    

    // 向右方向

    UISwipeGestureRecognizer *swipeGestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];

    swipeGestureRight.direction = UISwipeGestureRecognizerDirectionRight;     // 向右方向

    [imageView addGestureRecognizer:swipeGestureRight];

    [swipeGestureRight release];

    

    // 向上方向

    UISwipeGestureRecognizer *swipeGestureUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];

    swipeGestureUp.direction = UISwipeGestureRecognizerDirectionUp;     // 向上方向

    

    [imageView addGestureRecognizer:swipeGestureUp];

    [swipeGestureUp release];

    // 添加图片

    [self.view addSubview:imageView];

    [imageView release];

}


- (void)swipeGestureAction:(UISwipeGestureRecognizer *)swipeGesture

{

    UIImageView *imageView = (UIImageView *)swipeGesture.view;

    // 不管触发的是哪个方向的手势都走这一个方法,所以需要通过判断给指定的方向添加不同的动作

    if (swipeGesture.direction == UISwipeGestureRecognizerDirectionDown) {

        NSLog(@"down");

    } if (swipeGesture.direction == UISwipeGestureRecognizerDirectionLeft) {

        NSLog(@"left");

    } if (swipeGesture.direction == UISwipeGestureRecognizerDirectionRight) {

        NSLog(@"right");

    } if (swipeGesture.direction == UISwipeGestureRecognizerDirectionUp) {

        NSLog(@"up");

    }

}

以上是tapGesture和swipeGesture



本文出自 “封寒瑾” 博客,请务必保留此出处http://21lovetong.blog.51cto.com/9944813/1616027

手势,轻拍(tapGesture)+轻扫(swipeGesture)

标签:tapgesture、swipegesture

原文地址:http://21lovetong.blog.51cto.com/9944813/1616027

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