利用动态加载为对象添加一个 block 点击属性;

.h 文件

 #import <UIKit/UIKit.h>

 @interface UIView (Tap)
/**
* 动态添加手势
*/
- (void)setTapActionWithBlock:(void (^)(void))block ;
@end

.m 文件

 #import "UIView+Tap.h"
#import <objc/runtime.h>
/**
* 动态添加手势
*/
static const char *ActionHandlerTapGestureKey; @implementation UIView (Tap) - (void)setTapActionWithBlock:(void (^)(void))block { self.userInteractionEnabled = YES; UITapGestureRecognizer *gesture = objc_getAssociatedObject(self, &ActionHandlerTapGestureKey); if (!gesture) {
gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleActionForTapGesture:)];
[self addGestureRecognizer:gesture];
objc_setAssociatedObject(self, &ActionHandlerTapGestureKey, gesture, OBJC_ASSOCIATION_RETAIN);
} objc_setAssociatedObject(self, &ActionHandlerTapGestureKey, block, OBJC_ASSOCIATION_COPY);
} - (void)handleActionForTapGesture:(UITapGestureRecognizer *)gesture {
if (gesture.state == UIGestureRecognizerStateRecognized) {
void(^action)(void) = objc_getAssociatedObject(self, &ActionHandlerTapGestureKey);
if (action) {
action();
}
}
}
@end

Runtime 实现 动态添加属性的更多相关文章

  1. ios开发runtime学习四:动态添加属性

    #import "ViewController.h" #import "Person.h" #import "NSObject+Property.h& ...

  2. Runtime(动态添加属性)

    下面通过一个实例展示一下Runtime(动态添加属性)的用法 下面对运行时添加属性用到的策略参数进行补充: 这样看来,前面的NSString* name用的策略是retain nonatomic就知道 ...

  3. 我的Python学习笔记(四):动态添加属性和方法

    一.动态语言与静态语言 1.1 动态语言 在运行时代码可以根据某些条件改变自身结构 可以在运行时引进新的函数.对象.甚至代码,可以删除已有的函数等其他结构上的变化 常见的动态语言:Object-C.C ...

  4. JS-给对象动态添加属性

    var obj = {};//用来存放获取到所填写的信息 btn.onclick = function(){ var city = input_city.value; var num = input_ ...

  5. day_5.26python动态添加属性和方法

    python动态添加属性和方法 既然给类添加⽅法,是使⽤ 类名.⽅法名 = xxxx ,那么给对象添加⼀个⽅法 也是类似的 对象.⽅法名 = xxx '''2018-5-26 13:40:09pyth ...

  6. JavaScript自定义事件,动态添加属性

    根据事件的不同,可用的自定义方法也不同. document.createEvent('Event'); 实现主要有4个步骤: 1.创建事件. 2.初始化事件(三个参数:事件名,是否起泡,是否取消默认触 ...

  7. js对象动态添加属性,方法

    1. 动态添加属性,方法 var object = new Object(); object.name = "name"; object.age = 19; >>> ...

  8. python 动态添加属性及方法及“__slots__的作用”

    1.动态添加属性 class Person(object): def __init__(self, newName, newAge): self.name = newName self.age = n ...

  9. python动态添加属性和方法

    ---恢复内容开始--- python动态添加属性: class Person(object): def __init__(self,newName,newAge): self.name = newN ...

随机推荐

  1. ubuntu下安装Sublime Text并支持中文输入

    Sublime Text还是文本编辑器中比较不错的,就是他的文件对比有些差劲吧,还有中文输入需要打补丁,不知道开发者是怎么想的... 当然,这个软件是收费的,但是不买也能一直的使用,在我天朝就这点好处 ...

  2. Android中更新视图的函数onDraw()和dispatchdraw()函数的区别

    Android的view组件显示主要经过mesure, layout和draw这三个过程.在mesure阶段里调用mesure(int widthSpec, int heightSpec)方法,这个方 ...

  3. Jersey(1.19.1) - Client API, Overview of the API

    To utilize the client API it is first necessary to create an instance of a Client, for example: Clie ...

  4. Redis 命令 - Sets

    SADD key member [member ...] Add one or more members to a set 127.0.0.1:6379> SADD foo hello (int ...

  5. 关于easyui的一些小知识点(1)

    让layout布局自动适应浏览器宽度只需要加上fit="true"属性.

  6. Sql Server Profiler跟踪死锁

    Sql Server Profiler一个很重要的应用场景就是监视分析死锁. 下面通过一个例子来学习Profiler是如何监视死锁的. 1. 创建测试数据模拟死锁, 创建表Table_A和Table_ ...

  7. Cocos2d-x实例:设置背景音乐与音效-设置场景实现

    设置场景(Setting),Setting.h文件代码如下: #ifndef __Setting_SCENE_H__ #define __Setting_SCENE_H__ #include &quo ...

  8. C#中Excel的导入和导出的几种基本方式

    在上一篇(http://www.cnblogs.com/fengchengjushi/p/3369386.html)介绍过,Excel也是数据持久化的一种实现方式.在C#中.我们常常会与Excel文件 ...

  9. angular的post请求,SpringMVC后台接收不到参数值的解决方案

    这是我后台SpringMVC控制器接收isform参数的方法,只是简单的打出它的值: @RequestMapping(method = RequestMethod.POST) @ResponseBod ...

  10. CKedit在线编辑器

    在线编辑器         在实现所见即得的编辑效果. FCK            是开发者的名字的缩写 CKEditor        功能很完善的,具有,在线编辑与图片上传JS插件 UEdit  ...