UIView with shadow, rounded corners and custom drawRect

前端 未结 16 1388
南旧
南旧 2020-12-02 05:22

I have to create a custom UIView that will have round corners, a border, a shadow and its drawRect() method is overridden to provide custom drawing

16条回答
  •  一生所求
    2020-12-02 06:00

    I use this extension to UIView:

    Import UIKit
    
    extension UIView {
    
        /// A property that accesses the backing layer's opacity.
        @IBInspectable
        open var opacity: Float {
            get {
                return layer.opacity
            }
            set(value) {
                layer.opacity = value
            }
        }
    
        /// A property that accesses the backing layer's shadow
        @IBInspectable
        open var shadowColor: UIColor? {
            get {
                guard let v = layer.shadowColor else {
                    return nil
                }
    
                return UIColor(cgColor: v)
            }
            set(value) {
                layer.shadowColor = value?.cgColor
            }
        }
    
        /// A property that accesses the backing layer's shadowOffset.
        @IBInspectable
        open var shadowOffset: CGSize {
            get {
                return layer.shadowOffset
            }
            set(value) {
                layer.shadowOffset = value
            }
        }
    
        /// A property that accesses the backing layer's shadowOpacity.
        @IBInspectable
        open var shadowOpacity: Float {
            get {
                return layer.shadowOpacity
            }
            set(value) {
                layer.shadowOpacity = value
            }
        }
    
        /// A property that accesses the backing layer's shadowRadius.
        @IBInspectable
        open var shadowRadius: CGFloat {
            get {
                return layer.shadowRadius
            }
            set(value) {
                layer.shadowRadius = value
            }
        }
    
        /// A property that accesses the backing layer's shadowPath.
        @IBInspectable
        open var shadowPath: CGPath? {
            get {
                return layer.shadowPath
            }
            set(value) {
                layer.shadowPath = value
            }
        }
    
    
        /// A property that accesses the layer.cornerRadius.
        @IBInspectable
        open var cornerRadius: CGFloat {
            get {
                return layer.cornerRadius
            }
            set(value) {
                layer.cornerRadius = value
            }
        }
    
    
        /// A property that accesses the layer.borderWith.
        @IBInspectable
        open var borderWidth: CGFloat {
            get {
                return layer.borderWidth
            }
            set(value) {
                layer.borderWidth = value
            }
        }
    
        /// A property that accesses the layer.borderColor property.
        @IBInspectable
        open var borderColor: UIColor? {
            get {
                guard let v = layer.borderColor else {
                    return nil
                }
                return UIColor(cgColor: v)
            }
            set(value) {
                layer.borderColor = value?.cgColor
            }
        }
    }
    

提交回复
热议问题