• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

30.怎样在Swift中添加运行时属性?

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

  和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;
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
AppleSwift编程发布时间:2022-07-13
下一篇:
masonryoc版(snapkitswift版)发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap