zoukankan      html  css  js  c++  java
  • 给View 添加手势,点击无反应 如何给View添加点击事件,手势方法

    项目中有很多地方需要添加点击事件,重复代码很多,所以做了一个UIView的分类,专门做点击事件使用.
    项目地址:
    UIView-Tap

    代码很简单,主要有一点就是注意分类不能直接添加属性,需要用到运行时相关内容.

    代码如下:

    \UIView+Tap.h文件
    @interface UIView (Tap)
    - (void)addTapBlock:(void(^)(id obj))tapAction;
    @end
    
    \UIView+Tap.m文件
    #import <objc/runtime.h>
    
    static const void* tagValue = &tagValue;
    
    @interface UIView ()
    @property (nonatomic, copy) void(^tapAction)(id);
    @end
    
    @implementation UIView (Tap)
    - (void)tap{
        if (self.tapAction) {
            self.tapAction(self);
        }
    }
    - (void)addTapBlock:(void(^)(id obj))tapAction{
        self.tapAction = tapAction;
        if (![self gestureRecognizers]) {
            self.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
            [self addGestureRecognizer:tap];
        }
    }
    
    -(void)setTapAction:(void (^)(id))tapAction {
        objc_setAssociatedObject(self, tagValue, tapAction, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    -(void (^)(id))tapAction {
        return objc_getAssociatedObject(self, tagValue);
    }
    @end

    正如大家所见,如果要接收点击事件,必须userInteractionEnabled设置为YES,所以不管怎么只要确认要给视图添加点击事件,都会被设置为userInteractionEnabled = YES

    简单实用: 

    \UIView+Tap.h文件
    UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    redView.backgroundColor = [UIColor redColor];
    [redView addTapBlock:^(UIView* obj) {
        NSLog(@"redView%@",obj.backgroundColor);
    }];
    [self.view addSubview:redView];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 250, 100, 100)];
    imageView.image = [UIImage imageNamed:@"icon"];
    [imageView addTapBlock:^(UIImageView* obj) {
        NSLog(@"imageView:
    %@",obj.image);
    }];
    [self.view addSubview:imageView];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(150, 400, 100, 100)];
    label.text = @"这是label,点击这里...";
    [label addTapBlock:^(UILabel* obj) {
        NSLog(@"label:
    %@",obj.text);
    }];
    [self.view addSubview:label];


    有时候需要做出响应 比如在cell中 冲突

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

    //    

    //    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {

    //        return NO;

    //    }

    //    return YES;

    //    

    //}

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

    {

        // 过滤掉UIButton,也可以是其他类型

        if ( [touch.view isKindOfClass:[UIView class]])

        {

            return NO;

        }

        

        return YES;

    }

  • 相关阅读:
    Cocos2d-x3.0网络通信学习(一)
    Coding 初级教程(一)——用GitHub的GUI客户端对Coding的项目进行管理
    glob模式
    Cocos2d-x.3.0开发环境搭建
    Cocos2d-x.3.0开发环境搭建之—— 极简式环境搭建
    绘制图形与3D增强技巧(五)----多边形图元的使用及其他
    绘制图形与3D增强技巧(四)----多边形图元及其点画模式
    绘制图形与3D增强技巧(三)----三角形图元TRANGLE
    绘制图形与3D增强技巧(二)----直线图元之点画
    绘制图形与3D增强技巧(二)----直线图元
  • 原文地址:https://www.cnblogs.com/tangyuanby2/p/6587065.html
Copyright © 2011-2022 走看看