Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. override init(frame: CGRect) {
  2. super.init(frame: frame)
  3. sharedInit()
  4. }
  5.  
  6. required init?(coder aDecoder: NSCoder) {
  7. super.init(coder: aDecoder)
  8. sharedInit()
  9. }
  10.  
  11. override func prepareForInterfaceBuilder() {
  12. sharedInit()
  13. }
  14.  
  15. func sharedInit() {
  16. refreshCorners(value: cornerRadius)
  17. refreshShadowColor(color: shadowColor)
  18. refreshShadowOpacity(alpha: shadowAlpha)
  19. refreshShadowOffset(size: shadowOffset)
  20. refreshShadowRadius(blur: shadowBlur)
  21. refreshShadowPath(spread: shadowSpread)
  22. }
  23.  
  24. func refreshCorners(value: CGFloat) {
  25. layer.cornerRadius = value
  26. }
  27.  
  28. @IBInspectable var cornerRadius: CGFloat = 40 {
  29. didSet {
  30. refreshCorners(value: cornerRadius)
  31. }
  32. }
  33.  
  34. func refreshShadowColor(color: UIColor) {
  35. layer.shadowColor = color.cgColor
  36. }
  37.  
  38. @IBInspectable var shadowColor: UIColor = .black {
  39. didSet {
  40. refreshShadowColor(color: shadowColor)
  41. }
  42. }
  43.  
  44. func refreshShadowOpacity(alpha: Float) {
  45. layer.shadowOpacity = alpha
  46. }
  47.  
  48. @IBInspectable var shadowAlpha: Float = 0.16 {
  49. didSet {
  50. refreshShadowOpacity(alpha: shadowAlpha)
  51. }
  52. }
  53.  
  54. func refreshShadowOffset(size: CGSize) {
  55. layer.shadowOffset = size
  56. }
  57.  
  58. @IBInspectable var shadowOffset: CGSize = CGSize(width: 0, height: 0) {
  59. didSet {
  60. refreshShadowOffset(size: shadowOffset)
  61. }
  62. }
  63.  
  64. func refreshShadowRadius(blur: CGFloat) {
  65. layer.shadowRadius = blur / 2.0
  66. }
  67.  
  68. @IBInspectable var shadowBlur: CGFloat = 30 {
  69. didSet {
  70. refreshShadowRadius(blur: shadowBlur)
  71. }
  72. }
  73.  
  74. func refreshShadowPath(spread: CGFloat) {
  75. if spread == 0 {
  76. layer.shadowPath = nil
  77. } else {
  78. let dx = -spread
  79. let rect = layer.bounds.insetBy(dx: dx, dy: dx)
  80. layer.shadowPath = UIBezierPath(rect: rect).cgPath
  81. }
  82. }
  83.  
  84. @IBInspectable var shadowSpread: CGFloat = 0 {
  85. didSet {
  86. refreshShadowPath(spread: shadowSpread)
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement