博客> pan手势监听对view的上下左右滑动,利用关联对象在block中触发view的点击事件(附手势大全)
pan手势监听对view的上下左右滑动,利用关联对象在block中触发view的点击事件(附手势大全)
2018-07-21 14:14 评论:0 阅读:260 r17
手势冲突 iOS手势 iOS点击事件

内容目录:

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

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

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

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

#import <UIKit>

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

@interface UIView (WHAddTap)

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

@end

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

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

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

@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> absX) {
        if (movePoint.y<0 xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed>

// 实现如下方法,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

收藏
0
sina weixin mail 回到顶部