Swift - 常用的扩展

前端之家收集整理的这篇文章主要介绍了Swift - 常用的扩展前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

UIScrollView-Extension

extension UIScrollView {
    var hh_insetT : CGFloat {

        get {
            return contentInset.top
        }

        set(newVal) {
            var inset = contentInset
            inset.top = newVal
            contentInset = inset
        }
    }

    var hh_insetB : CGFloat {

        get {
            return contentInset.bottom
        }

        set(newVal) {
            var inset = contentInset
            inset.bottom = newVal
            contentInset = inset
        }
    }

    var hh_insetL : CGFloat {

        get {
            return contentInset.left
        }

        set(newVal) {
            var inset = contentInset
            inset.left = newVal
            contentInset = inset
        }
    }

    var hh_insetR : CGFloat {

        get {
            return contentInset.right
        }

        set(newVal) {
            var inset = contentInset
            inset.right = newVal
            contentInset = inset
        }
    }

    var hh_offsetX : CGFloat {

        get {
            return contentOffset.x
        }

        set(newVal) {
            var offset = contentOffset
            offset.x = newVal
            contentOffset = offset
        }
    }
    var hh_offsetY : CGFloat {

        get {
            return contentOffset.y
        }

        set(newVal) {
            var offset = contentOffset
            offset.y = newVal
            contentOffset = offset
        }
    }
    var hh_contentW : CGFloat {

        get {
            return contentSize.width
        }

        set(newVal) {
            var size = contentSize
            size.width = newVal
            contentSize = size
        }
    }
    var hh_contentH : CGFloat {

        get {
            return contentSize.height
        }

        set(newVal) {
            var size = contentSize
            size.height = newVal
            contentSize = size
        }
    }
}

UIView-Extension

extension UIView {

    var hh_x : CGFloat {

        get {
            return frame.origin.x
        }

        set(newVal) {
            var tmpFrame : CGRect = frame
            tmpFrame.origin.x     = newVal
            frame                 = tmpFrame
        }
    }

    var hh_y : CGFloat {

        get {
            return frame.origin.y
        }

        set(newVal) {
            var tmpFrame : CGRect = frame
            tmpFrame.origin.y     = newVal
            frame                 = tmpFrame
        }
    }

    var hh_h : CGFloat {

        get {
            return frame.size.height
        }

        set(newVal) {
            var tmpFrame : CGRect = frame
            tmpFrame.size.height  = newVal
            frame                 = tmpFrame
        }
    }

    var hh_w : CGFloat {

        get {
            return frame.size.width
        }

        set(newVal) {
            var tmpFrame : CGRect = frame
            tmpFrame.size.width   = newVal
            frame                 = tmpFrame
        }
    }

    var hh_size : CGSize {

        get {
            return frame.size
        }

        set(newVal) {
            var tmpFrame : CGRect = frame
            tmpFrame.size         = newVal
            frame                 = tmpFrame
        }
    }

    var hh_origin : CGPoint {

        get {
            return frame.origin
        }

        set(newVal) {
            var tmpFrame : CGRect = frame
            tmpFrame.origin       = newVal
            frame                 = tmpFrame
        }
    }
}
原文链接:https://www.f2er.com/swift/322228.html

猜你在找的Swift相关文章