项目中有很多地方需要添加点击事件,重复代码很多,所以做了一个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:\n%@",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:\n%@",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;

}

给View 添加手势,点击无反应 如何给View添加点击事件,手势方法的更多相关文章

  1. wordpress在撰写新文章界面的显示选项按钮点击无反应的解决办法

    原文链接:wordpress在撰写新文章界面的显示选项按钮点击无反应的解决办法 最近升级wordpress之后,发现在文章编辑界面的添加新媒体和可视化按钮点击无反应,如下:  然后就在网上找解决办法, ...

  2. 解决IDEA的maven项目 添加依赖后Reimport无反应

    解决IDEA的maven项目 添加依赖后Reimport无反应   如果重启项目和编译器都不管用的话, 找到项目在硬盘上的位置  把该项目的.idea文件夹和xxx.iml文件删除    打开IDEA ...

  3. wordpress模块无法拖拽/显示选项点击无反应

    问题:wordpress模块无法拖拽/显示选项点击无反应,还有编辑器的全屏什么的都用不了,按F12查看了console,提示很多jQuery is not defined... 解决方法:把wp-in ...

  4. UItableViewCell上的button点击无响应的办法

    由于IOS7中添加了滑动后出现编辑按钮的操作,所以使用scrollView来处理,UITableViewCellScrollView有对触摸的相应处理,导致按钮的点击效果被屏蔽了,但是点击事件还是在的 ...

  5. Android ListView的item背景色设置以及item点击无响应等相关问题

    Android ListView的item背景色设置以及item点击无响应等相关问题 在Android开发中,listview控件是非常常用的控件,在大多数情况下,大家都会改掉listview的ite ...

  6. 支付宝钱包手势密码破解实战(root过的手机可直接绕过手势密码)

    /* 本文章由 莫灰灰 编写,转载请注明出处. 作者:莫灰灰    邮箱: minzhenfei@163.com */ 背景 随着移动互联网的普及以及手机屏幕越做越大等特点,在移动设备上购物.消费已是 ...

  7. [转载]支付宝钱包手势密码破解实战(root过的手机可直接绕过手势密码)

    /* *转自http://blog.csdn.net/hu3167343/article/details/36418063 *本文章由 莫灰灰 编写,转载请注明出处. *作者:莫灰灰    邮箱: m ...

  8. View Controller Programming Guide for iOS---(五)---Resource Management in View Controllers

    Resource Management in View Controllers View controllers are an essential part of managing your app’ ...

  9. View Controller Programming Guide for iOS---(四)---Creating Custom Content View Controllers

    Creating Custom Content View Controllers 创建自定义内容视图控制器 Custom content view controllers are the heart ...

随机推荐

  1. 斜率优化dp学习

    用了一堂半的课才彻底搞懂.其他神犇写的博客或多或少有点小bug,所以orzzz不才斗胆重新写一个. 里面大量穿用其他神犇的原话,就不逐一标明出处了. 引用资料 Accept的博客 MathonL的博客 ...

  2. Caffe cpu版本 Linux配置命令及搭建

    Caffee 安装过程 1.安装依赖包 $ sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-de ...

  3. linux下增加useradd提示existing lock file /etc/subgid.lock without a PID

    # useradd git -g git useradd: existing lock file /etc/subgid.lock without a PID useradd: cannot lock ...

  4. wamp升级php7

    原文:http://blog.csdn.net/cheng6251/article/details/50730441 1.下载php7   http://windows.PHP.net/downloa ...

  5. Memcached stats items 命令

    Memcached stats items 命令用于显示各个 slab 中 item 的数目和存储时长(最后一次访问距离现在的秒数). 语法: stats items 命令的基本语法格式如下: sta ...

  6. KVM与XEN虚拟化环境究竟有何不同

    虚拟化的概念在近些年收到了很大程度上的普及,求其原因很简单:虚拟化能够最大程度利用资源,为企业节约成本.目前市面较受欢迎的虚拟架构主要有KVM.XEN和VMware,其中,KVM和XEN都是免费开源的 ...

  7. LeetCode 525. Contiguous Array

    525. Contiguous Array Add to List Description Submission Solutions Total Accepted: 2476 Total Submis ...

  8. 重新学习MySQL数据库1:无废话MySQL入门

    重新学习Mysql数据库1:无废话MySQL入门 开始使用 我下面所有的SQL语句是基于MySQL 5.6+运行. MySQL 为关系型数据库(Relational Database Manageme ...

  9. 17.并发容器之ThreadLocal

    1. ThreadLocal的简介 在多线程编程中通常解决线程安全的问题我们会利用synchronzed或者lock控制线程对临界区资源的同步顺序从而解决线程安全的问题,但是这种加锁的方式会让未获取到 ...

  10. POJ 2891 中国剩余定理的非互质形式

    中国剩余定理的非互质形式 任意n个表达式一对对处理,故只需处理两个表达式. x = a(mod m) x = b(mod n) km+a = b (mod n) km = (a-b)(mod n) 利 ...