Runtime 实现 动态添加属性
利用动态加载为对象添加一个 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 实现 动态添加属性的更多相关文章
- ios开发runtime学习四:动态添加属性
#import "ViewController.h" #import "Person.h" #import "NSObject+Property.h& ...
- Runtime(动态添加属性)
下面通过一个实例展示一下Runtime(动态添加属性)的用法 下面对运行时添加属性用到的策略参数进行补充: 这样看来,前面的NSString* name用的策略是retain nonatomic就知道 ...
- 我的Python学习笔记(四):动态添加属性和方法
一.动态语言与静态语言 1.1 动态语言 在运行时代码可以根据某些条件改变自身结构 可以在运行时引进新的函数.对象.甚至代码,可以删除已有的函数等其他结构上的变化 常见的动态语言:Object-C.C ...
- JS-给对象动态添加属性
var obj = {};//用来存放获取到所填写的信息 btn.onclick = function(){ var city = input_city.value; var num = input_ ...
- day_5.26python动态添加属性和方法
python动态添加属性和方法 既然给类添加⽅法,是使⽤ 类名.⽅法名 = xxxx ,那么给对象添加⼀个⽅法 也是类似的 对象.⽅法名 = xxx '''2018-5-26 13:40:09pyth ...
- JavaScript自定义事件,动态添加属性
根据事件的不同,可用的自定义方法也不同. document.createEvent('Event'); 实现主要有4个步骤: 1.创建事件. 2.初始化事件(三个参数:事件名,是否起泡,是否取消默认触 ...
- js对象动态添加属性,方法
1. 动态添加属性,方法 var object = new Object(); object.name = "name"; object.age = 19; >>> ...
- python 动态添加属性及方法及“__slots__的作用”
1.动态添加属性 class Person(object): def __init__(self, newName, newAge): self.name = newName self.age = n ...
- python动态添加属性和方法
---恢复内容开始--- python动态添加属性: class Person(object): def __init__(self,newName,newAge): self.name = newN ...
随机推荐
- 【Stirling Number I】
hdu 4372 Count the Buildings 推荐这位小哥的,我觉得人家说的灰常的好. 注意数据范围,n,f,b均在(0,2000]范围内,而第一斯特林数的数组范围却是s[2000+5][ ...
- 【思路、优化】UVa 11491 - Erasing and Winning
Juliano is a fan of the TV show Erasing and Winning, where participants are selected in a draw and r ...
- hdu 4417 划分树
思路:二分枚举区间第k大.用划分树查找是否符合要求的高度. #include<iostream> #include<algorithm> #include<cstdio& ...
- Leetcode 338. Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...
- li颜色特效
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> < ...
- ActiveMQ(5.10.0) - JNDI Support
1. Place the jndi.properties file on the classpath. java.naming.factory.initial = org.apache.activem ...
- [转]IOS, xib和storyboard的混用
1. 从xib的viewcontroll中启动storyboard 或者 从一个storyboard切换到另一个storyboard: [objc]– (IBAction)openStoryboard ...
- android中数据存储的contentprovider的使用方法
元数据接口 package com.example.contentproviderprojecrt; import android.net.Uri; import android.provider.B ...
- 【网络收集】数据库中字段类型对应C#中的数据类型
数据库中字段类型对应C#中的数据类型: 数据库 C#程序 int int32 text string bigint int64 binary System.Byte[] bit Boolean cha ...
- Android之触屏事件
方法一: 新建"MyView"类 package onTouchEvent; import android.content.Context; import android.grap ...