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 ...
随机推荐
- 一天一点MySQL复习——获取数据库系统时间、变量赋值、变量比较
一.SQL获取系统时间 mysql> select now() from dual; +---------------------+ | now() | +------------------- ...
- android中的style部分属性值介绍
转自:http://blog.sina.com.cn/s/blog_70c759fd01013phv.html Android平台定义的主题样式: android:theme="@andro ...
- T-SQL:SQL Server-SQL语句大全经典
ylbtech-SQL Server-Doc-Help:SQL Server-SQL语句大全经典 SQL Server 流程控制中的 While 语句. 1,SQL语句大全经典返回顶部 .说明:创建数 ...
- Aptana 插件 for Eclipse 4.4
http://download.aptana.com/studio3/plugin/install Aptana Update Site This site is designed to be use ...
- 查找指定目录下的文件 .xml
pre{ line-height:1; color:#9f1d66; background-color:#cfe4e4; font-size:16px;}.sysFunc{color:#5d57ff; ...
- linux c多线程编程范例
#include <stdio.h> #include <pthread.h> #include <unistd.h> #include <stdlib.h& ...
- iOS 获取通讯录权限的时机
建议将获取通讯录权限的代码放到 -(void)viewDidAppear:(BOOL)animated 或 -(void)viewWillAppear:(BOOL)animated 假如放在 view ...
- HDU-3872 Dragon Ball 线段树+DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3872 题意:有n个龙珠按顺序放在一列,每个龙珠有一个type和一个权值,要求你把这n个龙珠分成k个段, ...
- 基于jquery的表格动态创建,自动绑定,自动获取值
最近刚加入GUT项目,学习了很多其他同事写的代码,感觉受益匪浅. 在GUT项目中,经常会碰到这样一个问题:动态生成表格,包括从数据库中读取数据,并绑定在表格中,以及从在页面上通过jQuery新增删除表 ...
- 关于在 mac上配置pytesseract的相关问题
因为踩了两个小时坑 特别是在配置依赖tesseract-ORC识别库时候的问题 特别麻烦 一定要用brewhome 一定要用brewhome 一定要用brewhome 重要的事情说三遍. 刚开始我在网 ...