30.怎样在Swift中添加运行时属性?
和OC一样,Swift中也可以添加运行时属性。下面将提供一个完整的例子,演示如何给按钮点击事件添加运行时属性。
1.示例
import UIKit var s_GofButtonTouchDownKey = "s_GofButtonTouchDownKey";
var s_GofButtonTouchUpKey = "s_GofButtonTouchUpKey"; extension UIControl
{
/// 按钮TouchDown
var gof_touchDown: GofBtnClickBlock? {
get{
let touchDown = objc_getAssociatedObject(self, &s_GofButtonTouchDownKey);
let dealTouchDown = unsafeBitCast(touchDown, GofBtnClickBlock.self);
return dealTouchDown;
}
set(touchDown)
{
let dealTouchDown: AnyObject = unsafeBitCast(touchDown, AnyObject.self);
objc_setAssociatedObject(self, &s_GofButtonTouchDownKey, dealTouchDown, .OBJC_ASSOCIATION_COPY); self.removeTarget(self, action: #selector(onTouchDown(_:)), forControlEvents: .TouchDown);
self.addTarget(self, action: #selector(onTouchDown(_:)), forControlEvents: .TouchDown)
}
} /// 按钮TouchUpInside
var gof_touchUpInside: GofBtnClickBlock? {
get{
let touchUp = objc_getAssociatedObject(self, &s_GofButtonTouchUpKey);
let dealTouchUp = unsafeBitCast(touchUp, GofBtnClickBlock.self);
return dealTouchUp;
}
set(touchUp)
{
let dealTouchUp: AnyObject = unsafeBitCast(touchUp, AnyObject.self);
objc_setAssociatedObject(self, &s_GofButtonTouchUpKey, dealTouchUp, .OBJC_ASSOCIATION_COPY); self.removeTarget(self, action: #selector(onTouchUp(_:)), forControlEvents: .TouchUpInside);
self.addTarget(self, action: #selector(onTouchUp(_:)), forControlEvents: .TouchUpInside)
}
} /**
按钮TouchDown事件处理 - parameter btn: 按钮
*/
func onTouchDown(btn: UIButton) -> Void
{
let touchDown = self.gof_touchDown; if touchDown != nil
{
touchDown!(btn);
} } /**
按钮TouchUpInside事件处理 - parameter btn: 按钮
*/
func onTouchUp(btn: UIButton) -> Void
{
let touchUp = self.gof_touchUpInside; if touchUp != nil
{
touchUp!(btn);
}
}
}
2.示例说明
在Swift中,闭包不属于AnyObject,因此,需要做一个相互转换。如下所示:

使用方式如下:
extension UIButton
{
/**
创建UIButton控件 - parameter title: 按钮标题
- parameter superView: 按钮父视图
- parameter constraints: 约束
- parameter touchup: 按钮点击事件 - returns: UIButton控件
*/
static func gof_buttonWithTitle(title: String?, superView: UIView?, constraints: GofConstraintMaker?, touchup: GofBtnClickBlock?) -> UIButton
{
let button = UIButton(type: .Custom);
if title != nil
{
button.setTitle(title, forState: .Normal);
button.titleLabel?.font = kTitleFont;
} button.backgroundColor = kCColor; if superView != nil
{
superView!.addSubview(button); if constraints != nil
{
button.snp_makeConstraints(closure: { (make) in
constraints!(make);
})
}
} if touchup != nil
{
button.gof_touchUpInside = touchup!;
} return button;
}
}
30.怎样在Swift中添加运行时属性?的更多相关文章
- servers中添加server时,看不到运行环境的选择。
servers中添加server时,看不到运行环境的选择. 主要原因是tomcat目录中的配置文件格式不对.
- 在Amazon FreeRTOS V10中使用运行时统计信息
在MCU on Eclipse网站上看到Erich Styger在8月2日发的博文,一篇关于在Amazon FreeRTOS V10中使用运行时统计信息的文章,本人觉得很有启发,特将其翻译过来以备参考 ...
- Visual C++中对运行时库的支持
原文地址:http://blog.csdn.net/wqvbjhc/article/details/6612099 一.什么是C运行时库 1)C运行时库就是 C run-time library,是 ...
- Android Tips: 在给drawable中添加图片资源时,文件名必须全小写
在给drawable中添加图片资源时,文件名必须全小写
- Visual Studio 2008中添加运行按钮 转载
在Visual Studio 2008中添加运行按钮 默认情况下,VS2008中的工具栏上没有运行按钮,只有调试(Debug)按钮,可按照以下方法添加 1.点击菜单Tools(工具)->Cust ...
- [C++]C++中的运行时类型检测
Date:2014-1-3 Summary: 使用C++中的运行时类型检测.(文章重点在于记录本人的使用情况,并非深层讨论RTTI) Contents:写习惯C#的我,在C++依然存在哪些.NET的惯 ...
- 翻译:JVM虚拟机规范1.7中的运行时常量池部分(二)
本篇为JVM虚拟机规范1.7中的运行时常量池部分系列的第二篇. 4.4.4. The CONSTANT_Integer_info and CONSTANT_Float_info Structures ...
- Android中Activity运行时屏幕方向与显示方式详解
现在我们的手机一般都内置有方向感应器,手机屏幕会根据所处位置自动进行横竖屏切换(前提是未锁定屏幕方向).但有时我们的应用程序仅限在横屏或者竖屏状态下才可以运行,此时我们需要锁定该程序Activity运 ...
- HTML5Viewer中如何运行时绑定多数据源
很多报表控件提供HTML5Viewer 支持跨设备的报表系统,当然在很多情况下,一个系统可包含多个报表文件,这些报表的数据有可能均为运行时绑定数据源,那么在html5viewer中对一张报表通过重写W ...
随机推荐
- nodejs 改变全局前缀
npm的包安装分为本地安装(local).全局安装(global)两种,从敲的命令行来看,差别只是有没有-g而已,比如: 复制代码 代码如下: npm install grunt # 本地安装npm ...
- Mysql导入导出工具Mysqldump和Source命令用法详解
Mysql本身提供了命令行导出工具Mysqldump和Mysql Source导入命令进行SQL数据导入导出工作,通过Mysql命令行导出工具Mysqldump命令能够将Mysql数据导出为文本格式( ...
- AJAX异步调用
前台: function readygo(v) { $.ajax({ type: "post", ...
- xampp无法打开phpmyadmin解决方案
如果设置了apache的端口号(如8890),那么不可以用自带的admin按钮打开,而是要加上端口(如localhost:8890/phpmyadmin/)
- Web Developer可以做得更多
美国雅虎前端工程师Hedger Wang.这位原雅虎奇摩的第一位Web Developer,非常慷慨的与我们分享了他丰富的经验.现身说法,比空洞的理论更有感染力,我们发现现在遇到的很多问题也都是他曾经 ...
- MySQL · 性能优化· InnoDB buffer pool flush策略漫谈
MySQL · 性能优化· InnoDB buffer pool flush策略漫谈 背景 我们知道InnoDB使用buffer pool来缓存从磁盘读取到内存的数据页.buffer pool通常由数 ...
- 第二个UI脚本--Python+selenium之unittest+HTMLtestRunner及python的继承
前面有一篇对于常见元素的识别和操作的python自动化脚本,这一篇就接着聊下python的类继承,已经它的第三款unittest框架,和报告收集包HTMLtestRunner的应用. 还是直接上代码吧 ...
- JavaScript 性能优化 --By Google V8 Team Manager
原文:https://developers.google.com/v8/?hl=zh-CN Be Prepared before writing code[9:35] Understand how V ...
- C++C#时间转换
time_t是从1970年1月1日的格林尼治时间开始的,所以以下就是你要的结果System.DateTime time= new System.DateTime(1970, 1, 1).ToLocal ...
- js动画框架设计
当你不再依赖JQuery时,当你已经厌倦了引入js类库实现一些动画效果的方式,当你想实现一个简单而实用的动画框架......下面介绍下愚人设计的动画框架:支持动画缓动算法函数,如Linear.Cubi ...