Last active
April 12, 2020 11:32
-
-
Save mosluce/424849d3dda83dcf06cc57094c70013f to your computer and use it in GitHub Desktop.
Protocol Oriented 結合 UIView 及 IBDesignable+IBInspectable 練習
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // BorderDesignable.swift | |
| // Gotyou | |
| // | |
| // Created by 默司 on 2016/11/28. | |
| // Copyright © 2016年 默司. All rights reserved. | |
| // | |
| import Foundation | |
| import UIKit | |
| protocol BorderDesignable { | |
| var borderWidth: CGFloat { get set } | |
| var borderColor: UIColor? { get set } | |
| var cornerRadius: CGFloat { get set } | |
| func updateBorder() | |
| } | |
| extension BorderDesignable where Self: UIView { | |
| func updateBorder() { | |
| self.layer.borderWidth = borderWidth | |
| self.layer.borderColor = borderColor?.cgColor | |
| self.layer.cornerRadius = cornerRadius | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // DesignableView.swift | |
| // Gotyou | |
| // | |
| // Created by 默司 on 2016/11/28. | |
| // Copyright © 2016年 默司. All rights reserved. | |
| // | |
| import UIKit | |
| @IBDesignable class DesignableView: UIView, ShadowDesignable, BorderDesignable { | |
| @IBInspectable var shadowColor: UIColor = UIColor.black { | |
| didSet { | |
| self.layer.shadowColor = shadowColor.cgColor | |
| } | |
| } | |
| @IBInspectable var shadowOffset: CGSize = CGSize.zero { | |
| didSet { | |
| self.layer.shadowOffset = shadowOffset | |
| } | |
| } | |
| @IBInspectable var shadowOpacity: Float = 0 { | |
| didSet { | |
| self.layer.shadowOpacity = shadowOpacity | |
| } | |
| } | |
| @IBInspectable var shadowRadius: CGFloat = 0 { | |
| didSet { | |
| self.layer.shadowRadius = shadowRadius | |
| } | |
| } | |
| @IBInspectable var borderWidth: CGFloat = 0 { | |
| didSet { | |
| self.layer.borderWidth = borderWidth | |
| } | |
| } | |
| @IBInspectable var borderColor: UIColor? = UIColor.black { | |
| didSet { | |
| self.layer.borderColor = borderColor?.cgColor | |
| } | |
| } | |
| @IBInspectable var cornerRadius: CGFloat = 0 { | |
| didSet { | |
| self.layer.cornerRadius = cornerRadius | |
| } | |
| } | |
| override func prepareForInterfaceBuilder() { | |
| super.prepareForInterfaceBuilder() | |
| if self.borderWidth > 0 { | |
| updateBorder() | |
| } | |
| if self.shadowOpacity > 0 { | |
| drawShadow() | |
| } | |
| } | |
| override func awakeFromNib() { | |
| super.awakeFromNib() | |
| if self.borderWidth > 0 { | |
| updateBorder() | |
| } | |
| if self.shadowOpacity > 0 { | |
| drawShadow() | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // FlatDesignable.swift | |
| // Gotyou | |
| // | |
| // Created by 默司 on 2016/11/29. | |
| // Copyright © 2016年 默司. All rights reserved. | |
| // | |
| import Foundation | |
| import UIKit | |
| enum FlatState { | |
| case normal | |
| case highlighted | |
| case disabled | |
| } | |
| protocol FlatDesignable: BorderDesignable, ShadowDesignable { | |
| var isFilled: Bool { set get } | |
| var color: UIColor { set get } | |
| var highlightColor: UIColor { set get } | |
| var disabledColor: UIColor { set get } | |
| var flatState: FlatState { set get } | |
| func updateFlat() | |
| func updateFlatColors() | |
| } | |
| extension FlatDesignable where Self: UIButton { | |
| func updateFlat() { | |
| if self.state == .disabled { | |
| self.flatState = .disabled | |
| } else { | |
| if self.state == .highlighted { | |
| self.flatState = .highlighted | |
| } else { | |
| self.flatState = .normal | |
| } | |
| } | |
| self.borderWidth = self.borderWidth <= 0 ? 2 : self.borderWidth | |
| self.cornerRadius = min(self.bounds.height, self.bounds.width) / 2 | |
| self.updateBorder() | |
| } | |
| func updateFlatColors() { | |
| switch self.flatState { | |
| case .normal: | |
| self.borderColor = self.color | |
| self.backgroundColor = self.isFilled ? self.color : UIColor.clear | |
| self.setTitleColor(self.isFilled ? UIColor.white : self.color, for: .normal) | |
| break | |
| case .highlighted: | |
| self.borderColor = self.highlightColor | |
| self.backgroundColor = self.isFilled ? self.highlightColor : UIColor.clear | |
| self.setTitleColor(self.isFilled ? UIColor.white : self.highlightColor, for: .normal) | |
| break | |
| case .disabled: | |
| self.borderColor = self.disabledColor | |
| self.backgroundColor = self.isFilled ? self.disabledColor : UIColor.clear | |
| self.setTitleColor(self.isFilled ? UIColor.white : self.disabledColor, for: .normal) | |
| break | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // ShadowDesignable.swift | |
| // Gotyou | |
| // | |
| // Created by 默司 on 2016/11/28. | |
| // Copyright © 2016年 默司. All rights reserved. | |
| // | |
| import Foundation | |
| import UIKit | |
| protocol ShadowDesignable { | |
| var shadowColor: UIColor { get set } | |
| var shadowOffset: CGSize { get set } | |
| var shadowRadius: CGFloat { get set } | |
| var shadowOpacity: Float { get set } | |
| func drawShadow() | |
| } | |
| extension ShadowDesignable where Self: UIView { | |
| func drawShadow() { | |
| let rect = self.bounds | |
| self.layer.shadowOpacity = shadowOpacity | |
| self.layer.shadowRadius = shadowRadius | |
| self.layer.shadowColor = shadowColor.cgColor | |
| self.layer.shadowOffset = shadowOffset | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment