一些提高开发效率的 Category
最近工作陆续生产了一些方便开发的工具类,尽管最终没被收入使用,但不妨碍个人使用,故特此开一篇博文,也记录一些自己踩的坑。
UIGestureRecognizer+Block
简单来说,你可以这样使用 UIGestureRecognizer:
[self.view addGestureRecognizer:[UITapGestureRecognizer gestureRecognizerWithActionBlock:^(id gestureRecognizer) {
//...
}]];
不再需要繁琐地使用 selector 反射,也解决了代码分离的问题。 实现代码如下:
static const int target_key;
@implementation UIGestureRecognizer (Block) +(instancetype)nvm_gestureRecognizerWithActionBlock:(NVMGestureBlock)block {
return [[self alloc]initWithActionBlock:block];
} - (instancetype)initWithActionBlock:(NVMGestureBlock)block {
self = [self init];
[self addActionBlock:block];
[self addTarget:self action:@selector(invoke:)];
return self;
} - (void)addActionBlock:(NVMGestureBlock)block {
if (block) {
objc_setAssociatedObject(self, &target_key, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
} - (void)invoke:(id)sender {
NVMGestureBlock block = objc_getAssociatedObject(self, &target_key);
if (block) {
block(sender);
}
} @end
原理就是把 block 动态地绑成 UIGestureRecognizer 的一个变量,invoke 的时候再调用这个 block。
开发过程中遇到了两个坑。
- 我一开始在类方法里面进行了动态绑定,错误代码如下:
//以下是错误代码:
+ (instancetype)initWithActionBlock:(KYGestureBlock)block {
return [[self alloc] initWithTarget: [self _gestureRecognizerBlockTarget:block] selector:@selector(invoke:)];
} + (_KYGestureRecognizerBlockTarget *)_gestureRecognizerBlockTarget:(KYGestureBlock)block{
_KYGestureRecognizerBlockTarget *target = objc_getAssociatedObject(self, &target_key);
if (!target) {
target = [[_KYGestureRecognizerBlockTarget alloc]initWithBlock:block];
objc_setAssociatedObject(self, &target_key, target, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return target;
}
这样导致的结果就是,变量被绑到了这个类对象上,因为在类方法里面 self 指的是这个类对象,而类对象是常驻内存直到程序退出才释放的,这也就导致了这个类上始终绑着第一次的 target,之后无论怎样都不会改变。如果恰好你在 block 中有强制持有了 block 外的其他对象,那就会导致这些对象都不会释放,造成内存泄露。在实例方法中动态绑定即可解决。
- 如果不使用动态绑定,使用如下的代码会产生怎样的结果?
//错误代码
+ (instancetype)initWithActionBlock:(KYGestureBlock)block {
return [[self alloc] initWithTarget: [[_KYGestureRecognizerBlockTarget alloc]initWithBlock:block] selector:@selector(invoke:)];
}
结果就是出了这个作用域 target 对象释放。通过查阅文档,我在《OC 编程概念》的 Target-Action 一节中,看到苹果有提到这么一句: Control objects do not (and should not) retain their targets. 按照惯例,如果有特殊情况,苹果会特别提醒(比如 NSTimer 的描述中就写到 target The timer maintains a strong reference to this object until it (the timer) is invalidated. )。所以 UIGestureRecognizer 是不会对 target 强引用。一旦出了作用域,target 对象就释放了。所以,需要使用动态绑定强制持有。
UIView+ExtendTouchRect
一行代码扩大视图点击区域:
self.button.touchExtendInset = UIEdgeInsetsMake(-, -, -, -)
实现代码如下:
void Swizzle(Class c, SEL orig, SEL new) {
Method origMethod = class_getInstanceMethod(c, orig);
Method newMethod = class_getInstanceMethod(c, new);
if (class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))){
class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
} else {
method_exchangeImplementations(origMethod, newMethod);
}
}
@implementation UIView (ExtendTouchRect)
+ (void)load {
Swizzle(self, @selector(pointInside:withEvent:), @selector(myPointInside:withEvent:));
}
- (BOOL)myPointInside:(CGPoint)point withEvent:(UIEvent *)event {
if (UIEdgeInsetsEqualToEdgeInsets(self.touchExtendInset, UIEdgeInsetsZero) || self.hidden ||
([self isKindOfClass:UIControl.class] && !((UIControl *)self).enabled)) {
return [self myPointInside:point withEvent:event]; // original implementation
}
CGRect hitFrame = UIEdgeInsetsInsetRect(self.bounds, self.touchExtendInset);
hitFrame.size.width = MAX(hitFrame.size.width, ); // don't allow negative sizes
hitFrame.size.height = MAX(hitFrame.size.height, );
return CGRectContainsPoint(hitFrame, point);
}
static char touchExtendInsetKey;
- (void)setTouchExtendInset:(UIEdgeInsets)touchExtendInset {
objc_setAssociatedObject(self, &touchExtendInsetKey, [NSValue valueWithUIEdgeInsets:touchExtendInset],
OBJC_ASSOCIATION_RETAIN);
}
- (UIEdgeInsets)touchExtendInset {
return [objc_getAssociatedObject(self, &touchExtendInsetKey) UIEdgeInsetsValue];
}
@end
实现思路就是替换 pointInside:withEvent: 或者 hitTest:withEvent: 方法。顺便再复习一下响应链传递机制:当手指触摸屏幕,UIWindow 从最底层开始向上分发事件,每到一个视图,先调用 hitTest:withEvent: ,在 hitTest:withEvent: 里调用 pointInside:withEvent: 判断触摸点是否在当前区域,如果在,遍历它的子视图递归调用 hitTest:withEvent:。画成二叉树图就是一个反向深度优先遍历,一旦找到第一个最深的包含触摸点的后裔就停止遍历。

一些提高开发效率的 Category的更多相关文章
- 如何利用 Visual Studio 自带工具提高开发效率
Visual Stuido 是一款强大的Windows 平台集成开发工具,你是否好好地利用了它呢? 显示行号 有些时候(比如错误定位)的时候,显示行号将有利于我们进行快速定位. 如何显示 1. 工具 ...
- 成吨提高开发效率:Intellij Shortcuts精简子集与思维模式
在线精简cheatsheet备查表:intellij.linesh.twGithub项目:intellij-mac-frequent-keymap Intellij的快捷键多而繁杂,从官方推荐的key ...
- 善用VS中的Code Snippet来提高开发效率
http://www.cnblogs.com/anderslly/archive/2009/02/16/vs2008-code-snippets.html http://www.cnblogs.com ...
- 实用手册:130+ 提高开发效率的 vim 常用命令
Vim 是从 vi 发展出来的一个文本编辑器.代码补完.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用.和 Emacs 并列成为类 Unix 系统用户最喜欢的编辑器.这里收录了130+程 ...
- 提高开发效率的 Eclipse 实用操作
工欲善其事,必先利其器.对于程序员来说,Eclipse便是其中的一个“器”.本文会从Eclipse快捷键和实用技巧这两个篇章展开介绍.Eclipse快捷键用熟后,不用鼠标,便可进行编程开发,避免鼠标分 ...
- 10 款提高开发效率的 jQuery/CSS3 组件
前端开发是一项十分繁琐而又耗体力的工作,如何更有效率的开发我们的应用,很多人会选择适当地使用一些jQuery插件.今天就要给大家分享10款可以提高开发效率的jQuery/CSS3组件.部分插件可以下载 ...
- 能够提高开发效率的Eclipse实用操作
工欲善其事,必先利其器.对于程序员来说,Eclipse便是其中的一个“器”.本文会从Eclipse快捷键和实用技巧这两个篇章展开介绍.Eclipse快捷键用熟后,不用鼠标,便可进行编程开发,避免鼠标分 ...
- tomcat免重启随意更改java代码 提高开发效率
转载:http://developer.51cto.com/art/201012/241243.htm 做为了一个java开发人员,总是为因为要增加一个类,或是增加删除一个方法,甚至修改一个小处代码而 ...
- 能够提高开发效率的 Eclipse 实用操作
工欲善其事,必先利其器.对于程序员来说,Eclipse便是其中的一个“器”.本文会从Eclipse快捷键和实用技巧这两个篇章展开介绍.Eclipse快捷键用熟后,不用鼠标,便可进行编程开发,避免鼠标分 ...
随机推荐
- JS正则表达式验证账号、手机号、电话、邮箱、货币
验证帐号是否合法验证规则:字母.数字.下划线组成,字母开头,4-16位. function checkUser(str){ var re = /^[a-zA-z]\w{3,15}$/; if(re.t ...
- 从零开始,在windows上用nodejs搭建一个静态文件服务器
从零开始,在windows上用nodejs搭建一个静态文件服务器 首先安装nodejs: 新建一个node文件夹 下载node.exe到该文件夹 下载npm然后解压到该文件夹 现在node文件夹是这样 ...
- UIView -> image & 本地时间获取
//UIView 转换为图片 UIGraphicsBeginImageContext(self.rootsView.bounds.size); [_rootsView.layer renderInCo ...
- 自定义UITableViewCell时, 使用autoLayout, 无法很好的做到屏幕适配
解决方法: 重写cell的setFrame方法即可 - (void)setFrame:(CGRect)frame { frame.size.width = self.window.frame.size ...
- JS格式化数字金额用逗号隔开保留两位小数
JS格式化金额,正则方式修改. /** * 格式化金额 * @param {[type]} v [要转换的数字] * @param {[type]} len [小数点位数,默认2位] * @param ...
- 用 javascript 判断 IE 版本号
原文地址: http://julying.com/blog/determine-the-version-number-of-ie-with-javascript/ var _IE = (functio ...
- grails-MappingException: Could not determine Type
在用grails的时候遇到这个问题,反复调试了很久,没有进展,同时敲了几个命令后好了,真纠结,这是框架的问题吗? 问题: Caused by MappingException: Could not d ...
- jquery获取浏览器的高度和宽度
<script type="text/javascript"> $(document).ready(function() { alert($(window).heigh ...
- JavaScript 类型判断的那些事
先准备几个变量 var a = "abcde."; var b = 222; var c= [1,2,3]; // 或者 new Array() var d = new Date( ...
- 合并JS和CSS
1.先来看看,不用合并js的情况,传输量大 1.1新建一个 [基本]的mvc项目 然后新建一个控制器HomeController,因为js会在很多视图中用到,所以此时我们添加一个视图带布局页Index ...