Osheep

时光不回头,当下最重要。

pan手势监听对view的上下左右滑动,利用关联对象在block中触发view的点击事件(附手势大全)

内容目录:

  1. 在block中触发view的点击事件
  2. 利用pan手势,监听对view的上下左右滑动
  3. 各种手势的简单实现
  4. 解决手势冲突

一、在block中触发view的点击事件

首先创建一个UIView的分类,下面是头文件中的代码。

/****************UIView+WHAddTap.h**********************/

#import <UIKit/UIKit.h>

// 定义点击view的block
typedef void(^TapActionBlock)(UITapGestureRecognizer *tapGesture);

@interface UIView (WHAddTap)

// 点击view时触发此方法
- (void)wh_addTapActionWithBlock:(TapActionBlock)block;

@end

下面是具体实现代码,需要用到runtime的关联对象,所以别忘了引入头文件#import <objc/runtime.h>

/****************UIView+WHAddTap.m**********************/

#import "UIView+WHAddTap.h"
#import <objc/runtime.h>

@implementation UIView (WHAddTap)

- (void)wh_addTapActionWithBlock:(TapActionBlock)block {
    // 关联对象获取手势tap,_cmd是key值,代表此方法名
    UITapGestureRecognizer *tap = objc_getAssociatedObject(self, _cmd);
    if (!tap) {
        // 首次点击获取不到关联对象,创建手势并绑定触发方法
        tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionForTap:)];
        // 添加手势
        [self addGestureRecognizer:tap];
        // 运用runtime,set手势tap,这样下次点击的时候 外面的tap就存在了
        objc_setAssociatedObject(self, @selector(wh_addTapActionWithBlock:), tap, OBJC_ASSOCIATION_RETAIN);
    }
     // key是手势触发的方法名,关联block
    objc_setAssociatedObject(self, @selector(actionForTap:), block, OBJC_ASSOCIATION_COPY);
}

// tap手势的实现
- (void)actionForTap:(UITapGestureRecognizer *)tap {
    if (tap.state == UIGestureRecognizerStateRecognized) {
        // _cmd就是此方法名,利用这个key拿到上面的方法关联的block
        TapActionBlock block = objc_getAssociatedObject(self, _cmd);
        if (block) {
            block(tap);
        }
    }
}

@end

应用如下

// 在需要使用的地方引入头文件
#import "UIView+WHAddTap.h"

// 直接用view对象调用分类中的方法,在block中写代码
[view wh_addTapActionWithBlock:^(UITapGestureRecognizer *tapGesture) {

    NSLog(@"点击了这个view");
}];

二、利用pan手势,监听对view的上下左右滑动

简单暴力,直接上代码

    // 给view添加pan手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(directionPan:)];
    [view addGestureRecognizer:pan];

// 监听上下左右滑动的方法
- (void)directionPan:(UIPanGestureRecognizer *)pan {
    CGPoint movePoint = [pan translationInView:pan.view];
    [pan setTranslation:CGPointZero inView:pan.view];
    
    CGFloat absX = fabs(movePoint.x);
    CGFloat absY = fabs(movePoint.y);
    
    if (absX > absY ) {
        if (movePoint.x<0) {
            NSLog(@"向左滑动");
           
        }else{
            NSLog(@"向右滑动");
       
        }

    } else if (absY > absX) {
        if (movePoint.y<0) {
            NSLog(@"向上滑动");

        }else{
            NSLog(@"向下滑动");
     
        }
    }
}

三、各种手势的简单实现

    // 1. 缩放手势pinch
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    [view addGestureRecognizer:pinch];
    
// 缩放手势pinch触发方法
- (void)pinch:(UIPinchGestureRecognizer *)pinch {
    self.tempView.transform = CGAffineTransformScale(self.tempView.transform, pinch.scale, pinch.scale);
    pinch.scale = 1;
}

/************************************************************/

    // 2. 点击手势tap
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    tap.numberOfTapsRequired = 1;    // 点击多少次才能触发方法
    tap.numberOfTouchesRequired = 1; // 几根手指点击
    [view addGestureRecognizer:tap];
   
// 点击手势tap触发方法 
- (void)tap:(UITapGestureRecognizer *)tap {
    NSLog(@"%@", tap.view);
}

/************************************************************/

    // 3. 长按手势longPress
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    longPress.minimumPressDuration = 2;  // 长按2秒触发监听方法
    longPress.allowableMovement = 5;  // 这个范围内移动, 还是可以触发方法
    [view addGestureRecognizer:longPress];
    
// 长按手势longPress触发方法
- (void)longPress:(UILongPressGestureRecognizer *)longPress {
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"在这里执行长按后的代码");
    }
}

/************************************************************/

    // 4. 轻扫手势swipe
    // 向左
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [view addGestureRecognizer:swipeLeft];
    // 向右
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [view addGestureRecognizer:swipeRight];
    // 向上
    UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
    [view addGestureRecognizer:swipeUp];
    // 向上
    UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
    [view addGestureRecognizer:swipeDown];
    
// 轻扫手势swipe触发方法
- (void)swipe:(UISwipeGestureRecognizer *)swipe {
    if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"向右Swipe");
        
    } else if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"向左Swipe");
        
    } else if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {
        NSLog(@"向上Swipe");
        
    } else if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
        NSLog(@"向下Swipe");
    }
}

/************************************************************/

    // 5. 移动手势pan
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [view addGestureRecognizer:pan];
    
// 移动手势pan触发方法
- (void)pan:(UIPanGestureRecognizer *)pan {
    CGPoint movePoint = [pan translationInView:pan.view];
    _tempView.transform = CGAffineTransformTranslate(_tempView.transform, movePoint.x, movePoint.y);
    [pan setTranslation:CGPointZero inView:pan.view];
}


/************************************************************/

    // 6. 旋转手势Rotation
    UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
    rotate.delegate = self;
    [view addGestureRecognizer:rotate];

// 旋转手势Rotation触发方法
- (void)rotate:(UIRotationGestureRecognizer *)rotate {
    _tempView.transform = CGAffineTransformRotate(_tempView.transform, rotate.rotation);
    rotate.rotation = 0;
}

四、解决手势冲突

// 首先遵守UIGestureRecognizerDelegate
@interface ViewController () <UIGestureRecognizerDelegate>

// 实现如下方法,return YES 解决手势冲突问题
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

后记

在block中触发view的点击事件,主要运用的是关联对象技巧,比较实用。
推荐简单又好用的分类集合:WHKit
Github:https://github.com/remember17


转载请注明出处:http://www.jianshu.com/p/4ff12ccfc3c9

点赞