UIView添加支持代码块的手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTap)];
[aView addGestureRecognizer:tap];
[tap release];
以上是很简单的给一个UIView 添加 单击手势的方法.
下面我们来改进他支持代码快
先建立一个类别
@interface UIView (XY) -(void) addTapGestureWithTarget:(id)target action:(SEL)action;
-(void) addTapGestureWithBlock:(void(^)(void))aBlock; -(void) removeTapGesture; @end
代码块方法,这里要注意的是,我们得给UIView,添加一个block属性,然后在dealloc的时候释放掉.
但是在类别里重写dealloc会覆盖原本的方法,所以我们要先劫持dealloc方法
-(void) addTapGestureWithBlock:(void(^)(void))aBlock{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTap)];
[self addGestureRecognizer:tap];
[tap release];
objc_setAssociatedObject(self, UIView_key_tapBlock, aBlock, OBJC_ASSOCIATION_COPY);
XY_swizzleInstanceMethod([self class], @selector(dealloc), @selector(UIView_dealloc));
}
点击的时候响应的方法
-(void)actionTap{
void (^aBlock)(void) = objc_getAssociatedObject(self, UIView_key_tapBlock);
if (aBlock) aBlock();
}
当UIview 被释放的时候, 清空block, 然后执行原本dealloc方法
-(void) UIView_dealloc{
objc_removeAssociatedObjects(self);
XY_swizzleInstanceMethod([self class], @selector(UIView_dealloc), @selector(dealloc));
[self dealloc];
}
移魂大法,
static void XY_swizzleInstanceMethod(Class c, SEL original, SEL replacement) {
Method a = class_getInstanceMethod(c, original);
Method b = class_getInstanceMethod(c, replacement);
if (class_addMethod(c, original, method_getImplementation(b), method_getTypeEncoding(b)))
{
class_replaceMethod(c, replacement, method_getImplementation(a), method_getTypeEncoding(a));
}
else
{
method_exchangeImplementations(a, b);
}
}
UIView添加支持代码块的手势的更多相关文章
- xcode添加快捷代码块
添加快捷代码块 在开发过程中,一些常用的代码段,我们可以设置成快捷代码段来快速实现代码的编写. 如上图中的属性的设置,它们都有相同重复的代码@property (nonatomic, strong), ...
- Hexo next博客添加折叠块功能添加折叠代码块
前言 有大段的东西想要放上去,但又不想占据大量的位置.折叠是最好的选择.下面在Hexo的主题上定制添加折叠功能. 本文基于Hexo Next的主题修改.其他主题应该也差不多. 在main.js中添加折 ...
- pycharm如何添加固定代码块
1. file -- settings -- 搜索框输入live,找到 Live Templates 2. 选择你要添加到哪个语言中去,打开python组,并点击右上角 “+”,选择 1.Live T ...
- Xcode10 代码块(Code Snippet)添加和删除
https://blog.csdn.net/lg767201403/article/details/82761448 Code Snippets 使用 https://blog.csdn.net/lv ...
- 代码块(Block)回调一般阐述
本章教程主要对代码块回调模式进行讲解,已经分析其他回调的各种优缺点和适合的使用场景. 代码块机制 Block变量类型 Block代码封装及调用 Block变量对普通变量作用域的影响 Block回调接口 ...
- IOS开发之----代码块的使用(二)
iOS4引入了一个新特性,支持代码块的使用,这将从根本上改变你的编程方式.代码块是对C语言的一个扩展,因此在Objective-C中完全支持.如果你学过Ruby,Python或Lisp编程语言,那么你 ...
- VS 添加自定义--代码块 实现一秒创建方法
创建一个方法 你是不是不可避免需要敲以下至少6行代码 现在教你一个方法 实现一秒创建完整方法 首先按照代码块规则创建代码块文件 代码块意义,是什么? 请参考: https://docs.microso ...
- 给UIView添加手势
对于不能addTarget的UI对象,添加手势为他们带来了“福音”,以为UIView添加手势为例,揭开手势的面目. 1,创建一个view先, UIView * jrView=[[UIViewalloc ...
- 62.Xcode 添加代码块
1. Xcode创建一个新项目,打开一个.h或者.m文件 2.我举例以设置属性为例 #import <UIKit/UIKit.h> @interface ViewController : ...
随机推荐
- jquery1.7.2的源码分析(五)$.support
$.support 的英文注释很详细的介绍的这里,就稍微的写了下 Query.support = (function() { var support, all, a, select, opt, inp ...
- 手游架构-REST架构
REST架构风格是全新的针对Web应用的开发风格,是当今世界最成功的互联网超媒体分布式系统架构,它使得人们真正理解了Http协议本来面貌.随着 REST架构成为主流技术,一种全新的互联网网络应用开发的 ...
- Java基础知识强化35:String类之String的其他功能
1. String类的其他功能: (1)替换功能: String replace(char old, char new) String replace(String old,String new) ( ...
- CentOS yum Fatal Error 处理一例
环境说明 [root@thatsit ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@thatsit ~]# uname - ...
- python challenge 待续中
网址:http://www.pythonchallenge.com/解答好文:http://story.iteye.com/blog/730466 0:2^38 reduce(lambda x,y:x ...
- EF中的自动追踪与代理
自动追踪 EF框架会自动追踪实体的变化(通过比较实体的当前值与原始值). 默认情况下,以下方法会自动触发实体变化的追踪 DbSet.Find DbSet.Local DbSet.Remove DbSe ...
- ORA-02095: specified initialization parameter cannot be modified
输入命令:alter system set utl_file_dir='/home/oracle/logmnr' scope=spfile; 报错: 出错原因:没有用spfile文件启动数据库 解决办 ...
- MVC校验特性
1.前端引入3个脚本 ①jq脚本 ②jQuery.Validate.js ③jquery.validate.unobtrusive.js(异步验证) 2.后端加特性 在表对应的Mod ...
- C#list泛型集合
//创建list泛型集合 List<int> ilist = new List<int>(); ilist.Add(); ilist.Add(); ilist.AddRange ...
- AVL树相关操作
#include <iostream> using namespace std; //AVL树的节点 template<typename T> class TreeNode { ...