Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. //
  2. // RoundedView.swift
  3. // Raayek
  4. //
  5. // Created by mohamed saeed on 1/29/18.
  6. // Copyright © 2018 mohamed saeed. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class RoundedView: UIView {
  12.  
  13.  
  14. // Only override draw() if you perform custom drawing.
  15. // An empty implementation adversely affects performance during animation.
  16. /* override func draw(_ rect: CGRect) {
  17. // Drawing code
  18. layer.cornerRadius = 20.0
  19. }*/
  20. @IBInspectable var isCornererRaduisRTH:Bool = false
  21. @IBInspectable var raduisPresentRTH : CGFloat = 0.1
  22. @IBInspectable var radius :CGFloat = 10
  23. @IBInspectable var topLeft:Bool = true
  24. @IBInspectable var bottomLeft : Bool = true
  25. @IBInspectable var bottomRight: Bool = true
  26. @IBInspectable var topRight : Bool = true
  27.  
  28. fileprivate func updateCalculations() {
  29. var rad : CGFloat = 0
  30. if (isCornererRaduisRTH){
  31. rad = frame.height * raduisPresentRTH
  32. }
  33. else {
  34. rad = radius
  35. }
  36.  
  37. var cornerArrays = UIRectCorner()
  38. if L102Language.currentAppleLanguage() == "ar" {
  39. if topLeft && !topRight {
  40. topRight = true
  41. topLeft = false
  42. }
  43. if bottomLeft && !bottomRight {
  44. bottomRight = true
  45. bottomLeft = false
  46. }
  47. }
  48. if topLeft {
  49. cornerArrays.insert(.topLeft)
  50. }
  51. if topRight {
  52. cornerArrays.insert(.topRight)
  53. }
  54. if bottomRight {
  55. cornerArrays.insert(.bottomRight)
  56. }
  57. if bottomLeft {
  58. cornerArrays.insert(.bottomLeft)
  59. }
  60. roundCorners( cornerArrays, radius: rad )
  61. }
  62.  
  63. override func awakeFromNib() {
  64. super.awakeFromNib()
  65. updateCalculations()
  66. //clipsToBounds = true
  67. //layer.masksToBounds = true
  68.  
  69. }
  70. func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
  71. let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
  72. let mask = CAShapeLayer()
  73. mask.path = path.cgPath
  74. self.layer.mask = mask
  75. }
  76. override func layoutSubviews() {
  77. super.layoutSubviews()
  78. updateCalculations()
  79. }
  80.  
  81. @IBInspectable var shadowOffset: CGSize{
  82. get{
  83. return self.layer.shadowOffset
  84. }
  85. set{
  86. if L102Language.currentAppleLanguage() == "ar" {
  87. var adjustedVal = newValue
  88. adjustedVal.width = -adjustedVal.width
  89. self.layer.shadowOffset = adjustedVal
  90. }
  91. }
  92. }
  93.  
  94. @IBInspectable var shadowColor: UIColor{
  95. get{
  96. return UIColor(cgColor: self.layer.shadowColor!)
  97. }
  98. set{
  99. self.layer.shadowColor = newValue.cgColor
  100. }
  101. }
  102.  
  103. @IBInspectable var shadowRadius: CGFloat{
  104. get{
  105. return self.layer.shadowRadius
  106. }
  107. set{
  108. self.layer.shadowRadius = newValue
  109. }
  110. }
  111.  
  112. @IBInspectable var shadowOpacity: Float{
  113. get{
  114. return self.layer.shadowOpacity
  115. }
  116. set{
  117. self.layer.shadowOpacity = newValue
  118. }
  119. }
  120. }
  121.  
  122.  
  123. class BorderView: RoundedView
  124. {
  125. @IBInspectable var borderColor : UIColor = UIColor.white
  126. @IBInspectable var borderWidth : CGFloat = 1.0
  127. override func awakeFromNib() {
  128. super.awakeFromNib()
  129. backgroundColor = UIColor.clear
  130. layer.borderColor = borderColor.cgColor
  131. layer.borderWidth = borderWidth
  132. clipsToBounds = true
  133. }
  134. }
  135.  
  136.  
  137. class GradiantView : UIView{
  138.  
  139. @IBInspectable var borderRadiusPresentage : CGFloat = 0.5
  140. @IBInspectable var color1:UIColor = UIColor.white
  141. @IBInspectable var color2:UIColor = UIColor.blue
  142. @IBInspectable var startPoint:CGPoint = CGPoint.zero
  143. @IBInspectable var endpoint:CGPoint = CGPoint(x:1 , y:0)
  144. @IBInspectable var gradiantInsectBy:CGPoint = CGPoint(x:1 , y:1)
  145. let gradient = CAGradientLayer()
  146. override func awakeFromNib() {
  147. super.awakeFromNib()
  148.  
  149. layer.cornerRadius = borderRadiusPresentage * frame.height
  150. layer.borderWidth = 0
  151. backgroundColor = UIColor.clear
  152. gradient.frame = bounds.insetBy(dx: gradiantInsectBy.x, dy: gradiantInsectBy.y)
  153. gradient.colors = [color1.cgColor, color2.cgColor]
  154. gradient.cornerRadius = layer.cornerRadius
  155. gradient.startPoint = startPoint
  156. gradient.endPoint = endpoint
  157. gradient.borderWidth = 0
  158. layer.insertSublayer(gradient, at: 0)
  159. }
  160. override func layoutSubviews() {
  161. super.layoutSubviews()
  162. gradient.frame = bounds.insetBy(dx: 1, dy: 1)
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement