Swift 的扩展和封装代码举例

2016-12-26  本文已影响208人  乡水情缘

Swift 的扩展和封装代码举例
1.扩展是向一个已有的类、结构体或枚举类型添加新的功能(在swift中扩展没有名字)。相当于Objective-C中Category(OC中可以有名字的,而且只能扩展类)。以下例子是扩展UIView,来获取UIView上的UIViewController。

extension UIView {  
  
    /// Get the controller that this view belongs to. It will return nil if there is no controller.  
    ///  
    /// - returns: UIViewController?  
    public func viewController() -> UIViewController? {  
        var next = self.next  
        while next != nil {  
            if let ret = next?.isKind(of: UIViewController.superclass()!) {  
                return next as? UIViewController  
            }  
              
            next = next?.next  
        }  
          
        return nil  
    }  
  
}  

2.定义frame的简写

extension UIView {
     var x_swift: CGFloat{
        get{
            return self.frame.origin.x
        }
        set{
            var r = self.frame
            r.origin.x = newValue
            self.frame = r
        }
    }
    

    
    public var y_swift: CGFloat{
        get{
            return self.frame.origin.y
        }
        set{
            var r = self.frame
            r.origin.y = newValue
            self.frame = r
        }
    }
    /// 右边界的x值
    public var rightX: CGFloat{
        get{
            return self.x + self.width
        }
        set{
            var r = self.frame
            r.origin.x = newValue - frame.size.width
            self.frame = r
        }
    }
    /// 下边界的y值
    public var bottomY: CGFloat{
        get{
            return self.y + self.height
        }
        set{
            var r = self.frame
            r.origin.y = newValue - frame.size.height
            self.frame = r
        }
    }
    
    public var centerX_swift : CGFloat{
        get{
            return self.center.x
        }
        set{
            self.center = CGPoint(x: newValue, y: self.center.y)
        }
    }
    
    public var centerY_swift : CGFloat{
        get{
            return self.center.y
        }
        set{
            self.center = CGPoint(x: self.center.x, y: newValue)
        }
    }
    
    public var width_swift: CGFloat{
        get{
            return self.frame.size.width
        }
        set{
            var r = self.frame
            r.size.width = newValue
            self.frame = r
        }
    }
    public var height_swift: CGFloat{
        get{
            return self.frame.size.height
        }
        set{
            var r = self.frame
            r.size.height = newValue
            self.frame = r
        }
    }
    
    
    public var origin_swift: CGPoint{
        get{
            return self.frame.origin
        }
        set{
            self.x = newValue.x
            self.y = newValue.y
        }
    }
    
    public var size_swift: CGSize{
        get{
            return self.frame.size
        }
        set{
            self.width = newValue.width
            self.height = newValue.height
        }
    }
}

3.也可以扩展方法来封装频繁重复的操作。
以下例子为创建固定格式的UIButton。

extension UIButton {  
      
    /// Create simple button  
    ///  
    /// - parameter imageName: image name, can set to be nil  
    /// - parameter title:     title name  
    ///  
    /// - returns: button  
    public func creatButton(imageName:String?, title:String) -> UIButton {  
        let button = UIButton()  
        if ((imageName?.characters.count) != nil) {  
            button.setImage(UIImage(named: imageName!), for: UIControlState.normal)  
        }  
        button.setTitle(title, for: UIControlState.normal)  
        button.titleLabel?.font = UIFont.systemFont(ofSize: 12)  
        button.setTitleColor(UIColor.darkGray, for: UIControlState.normal)  
        button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)  
        return button  
    }  
      
}  
上一篇下一篇

猜你喜欢

热点阅读