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

IOS笔记051-手势使用

时间:2015-06-18 09:21:02      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

UIGestureRecognizer

利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势

    UITapGestureRecognizer(敲击)

    UIPinchGestureRecognizer(捏合,用于缩放)

    UIPanGestureRecognizer(拖拽)

    UISwipeGestureRecognizer(轻扫)

    UIRotationGestureRecognizer(旋转)

    UILongPressGestureRecognizer(长按)

使用方法

    // 创建点按手势

    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];

    // imageView添加手势,默认imageView不响应事件

    // 设置imageView 响应事件

    self.imageView.userInteractionEnabled = YES;

    [self.imageView addGestureRecognizer:gesture];

响应手势事件,可以在里面进行形变

    // 响应手势事件

    - (void)tap:(UITapGestureRecognizer *)tap

    {

        NSLog(@"%s",__func__);

        // 旋转

        self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI_4);

    }

手势的状态

    typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {

        // 没有触摸事件发生,所有手势识别的默认状态

        UIGestureRecognizerStatePossible,

        // 一个手势已经开始但尚未改变或者完成时

        UIGestureRecognizerStateBegan,

        // 手势状态改变

        UIGestureRecognizerStateChanged,

        // 手势完成

        UIGestureRecognizerStateEnded,

        // 手势取消,恢复至Possible状态

        UIGestureRecognizerStateCancelled,

        // 手势失败,恢复至Possible状态

        UIGestureRecognizerStateFailed,

        // 识别到手势识别

        UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded

    };

手势的一些代理方法 

    #pragma mark -  手势代理方法

    // 是否开始手势

    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer

    {

        return YES;

    }

    //是否允许同时支持多个手势,默认是不支持多个手势

    // 返回yes表示支持多个手势

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

    {

        return YES;

    }

    // 是否允许接收手指的触摸点

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

    {

    //    NSLog(@"%@",touch);

        return YES;

    }

逐个实现每个手势

1、点按 UITapGestureRecognizer 

    #pragma mark - 各种手势

    // UITapGestureRecognizer

    // 点按

    - (void)tapImage

    {

        // 创建点按手势

        UITapGestureRecognizer *gestur = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(tap:)];

        // imageView添加手势,默认imageView不响应事件

        gestur.delegate = self;

        gestur.numberOfTapsRequired = 2; // 点击次数

        [self.imageView addGestureRecognizer:gestur];

    }

    // 响应点按手势事件

    - (void)tap:(UITapGestureRecognizer *)tap

    {

        // 连续点按两次就更换显示图片

        if (tap.numberOfTapsRequired == 2) {

            

            self.imageView.image = [UIImage imageNamed:@"qianbao"];

        }

    }

2、捏合 可以使用两个手指进行缩放 UIPinchGestureRecognizer 

    //UIPinchGestureRecognizer

    // 捏合,可以缩放

    - (void)pinchImage

    {

        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizeralloc] initWithTarget:selfaction:@selector(pinch:)];

       //pinch.delegate = self;

        

        [self.imageViewaddGestureRecognizer:pinch];

    }

    - (void)pinch:(UIPinchGestureRecognizer *)pinch

    {

        self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale,pinch.scale);

        // 复位

        pinch.scale = 1;

    }

3、长按 UILongPressGestureRecognizer

    // UILongPressGestureRecognizer

    // 长按,默认会触发两次,开始时调用一次,结束时调用一次,可以根据手势状态

    - (void)longPressImage

    {

        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(longPress:)];

        [self.imageView addGestureRecognizer:longPress];

    }

 

    - (void)longPress:(UILongPressGestureRecognizer *)longP

    {

        // 判断手势状态,只在手势开始的时候执行。

        if(longP.state == UIGestureRecognizerStateBegan)

        {

            self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI);

        }

    }

4、清扫 轻轻向某个方向滑动 

    // UISwipeGestureRecognizer

    // 清扫手势,默认向右

    - (void)swipeImage

    {

        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizeralloc] initWithTarget:selfaction:@selector(swipe:)];

        [self.imageViewaddGestureRecognizer:swipe];

        

        // 如果以后想要一个控件支持多个方向的轻扫,必须创建多个轻扫手势,一个轻扫手势只支持一个方向

        // 默认轻扫的方向是往右

        UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizeralloc] initWithTarget:selfaction:@selector(swipe:)];

        swipeUp.direction = UISwipeGestureRecognizerDirectionUp;

        [self.imageView addGestureRecognizer:swipeUp];

    }

    - (void)swipe:(UISwipeGestureRecognizer *)swipe

    {

        // 输出状态

        NSLog(@"%s\n%ld",__func__,swipe.direction);

    }

5、旋转 UIRotationGestureRecognizer

    // UIRotationGestureRecognizer

    // 旋转手势

    - (void)rotateImage

    {

        UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];

        [self.imageView addGestureRecognizer:rotate];

    }

    - (void)rotate:(UIRotationGestureRecognizer *)rotate

    {

        self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotate.rotation);

        // 复位

        rotate.rotation = 0;

    }

6、拖拽  UIPanGestureRecognizer

    // 拖拽

    - (void)panImage

    {

        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc] initWithTarget:selfaction:@selector(pan:)];

        

        [self.imageViewaddGestureRecognizer:pan];

    }

    - (void)pan:(UIPanGestureRecognizer *)pan

    {

        // 获取手势移动点

        CGPoint curP = [pan translationInView:self.imageView];

        

        self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, curP.x, curP.y);

        // 复位,一定要复位

        [pan setTranslation:CGPointZeroinView:self.imageView];

    }

 在视图加载时进行调用即可 

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view, typically from a nib.

        // 设置imageView 响应事件

        self.imageView.userInteractionEnabled = YES;

        // 点按图形

        [self tapImage];

        // 长按

        [selflongPressImage];

        // 清扫

        [self swipeImage];

        // 旋转

        [self rotateImage];

        // 缩放

        [self pinchImage];

        // 拖拽

    //    [self panImage];

    }

 简单效果如下

  技术分享

 

IOS笔记051-手势使用

标签:

原文地址:http://www.cnblogs.com/songliquan/p/4584863.html

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