Guest User

Untitled

a guest
Jul 16th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. @IBOutlet var blurView: UIVisualEffectView!
  2.  
  3. var blurLayer : CALayer{
  4. return blurView.layer
  5. }
  6. override func viewDidLoad() {
  7. super.viewDidLoad()
  8. setUpLayer()
  9. // Do any additional setup after loading the view.
  10. }
  11.  
  12. func setUpLayer(){
  13. blurLayer.cornerRadius = 50
  14. }
  15.  
  16. @IBOutlet var blurView: UIVisualEffectView!
  17.  
  18. override func viewDidLoad() {
  19. super.viewDidLoad()
  20. blurView.layer.cornerRadius = 50
  21. // Do any additional setup after loading the view.
  22. }
  23.  
  24. override func viewDidLoad() {
  25. super.viewDidLoad()
  26. blurView.layer.cornerRadius = 50
  27. blurView.clipsToBounds = true
  28. }
  29.  
  30. class PSORoundedVisualEffectView : UIVisualEffectView{
  31.  
  32. override func layoutSubviews() {
  33. super.layoutSubviews()
  34. updateMaskLayer()
  35. }
  36.  
  37. func updateMaskLayer(){
  38. let shapeLayer = CAShapeLayer()
  39. shapeLayer.path = UIBezierPath(roundedRect: self.bounds, cornerRadius: 10).CGPath
  40. self.layer.mask = shapeLayer
  41. }
  42. }
  43.  
  44. private typealias ObjcRawUIVisualEffectViewSelCGRect =
  45. @convention(c) (UIVisualEffectView, Selector, CGRect) -> Void
  46.  
  47. private var cornerRadiusKey =
  48. "com.WeZZard.Waxing.UIVisualEffectView-CornerRadius.cornerRadius"
  49.  
  50. private var needsUpdateMaskLayerKey =
  51. "com.WeZZard.Waxing.UIVisualEffectView-CornerRadius.needsUpdateMaskLayer"
  52.  
  53. extension UIVisualEffectView {
  54. public var cornerRadius: CGFloat {
  55. get {
  56. if let storedValue = objc_getAssociatedObject(self,
  57. &cornerRadiusKey)
  58. as? CGFloat
  59. {
  60. return storedValue
  61. }
  62. return 0
  63. }
  64. set {
  65. if cornerRadius != newValue {
  66. objc_setAssociatedObject(self,
  67. &cornerRadiusKey,
  68. newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  69. setNeedsUpdateMaskLayer()
  70. }
  71. }
  72. }
  73.  
  74. private var needsUpdateMaskLayer: Bool {
  75. get {
  76. if let storedValue = objc_getAssociatedObject(self,
  77. &needsUpdateMaskLayerKey)
  78. as? Bool
  79. {
  80. return storedValue
  81. }
  82. return false
  83. }
  84. set {
  85. objc_setAssociatedObject(self,
  86. &needsUpdateMaskLayerKey,
  87. newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  88. }
  89. }
  90.  
  91.  
  92. public override class func initialize() {
  93. swizzle_setBounds()
  94. }
  95.  
  96. private class func swizzle_setBounds() {
  97. struct Static {
  98. static var token: dispatch_once_t = 0
  99. }
  100.  
  101. dispatch_once(&Static.token) {
  102. let selector: Selector = "setBounds:"
  103.  
  104. let method = class_getInstanceMethod(self, selector)
  105.  
  106. let imp_original = method_getImplementation(method)
  107.  
  108. before_setBounds = unsafeBitCast(imp_original,
  109. ObjcRawUIVisualEffectViewSelCGRect.self)
  110.  
  111. class_replaceMethod(self,
  112. selector,
  113. unsafeBitCast(after_setBounds, IMP.self),
  114. "@:{_struct=CGRect}")
  115. }
  116. }
  117.  
  118. private func setNeedsUpdateMaskLayer() {
  119. needsUpdateMaskLayer = true
  120. NSOperationQueue.mainQueue().addOperationWithBlock { [weak self] _ in
  121. self?.updateMaskLayerIfNeeded()
  122. }
  123. }
  124.  
  125. private func updateMaskLayerIfNeeded() {
  126. if needsUpdateMaskLayer {
  127. updateMaskLayer()
  128. needsUpdateMaskLayer = false
  129. }
  130. }
  131.  
  132. private func updateMaskLayer(){
  133. var filterViewFound = false
  134. for each in subviews {
  135. if each.dynamicType.description()
  136. .containsString("Filter")
  137. {
  138. filterViewFound = true
  139. let newPath = UIBezierPath(roundedRect: each.bounds,
  140. cornerRadius: self.cornerRadius)
  141. .CGPath
  142. if let existedMask = each.layer.mask
  143. as? CAShapeLayer
  144. {
  145. existedMask.path = newPath
  146. } else {
  147. let shapeLayer = CAShapeLayer()
  148. shapeLayer.path = newPath
  149. each.layer.mask = shapeLayer
  150. }
  151. } else {
  152. setNeedsUpdateMaskLayer()
  153. }
  154. }
  155. assert(filterViewFound == true, "Filter view was not found! Check your hacking!")
  156. }
  157. }
  158.  
  159. private var before_setBounds: ObjcRawUIVisualEffectViewSelCGRect = { _ in
  160. fatalError("No implementation found")
  161. }
  162.  
  163. private let after_setBounds: ObjcRawUIVisualEffectViewSelCGRect = {
  164. (aSelf, selector, bounds) -> Void in
  165.  
  166. let oldBounds = aSelf.bounds
  167.  
  168. before_setBounds(aSelf, selector, bounds)
  169.  
  170. if oldBounds.size != bounds.size {
  171. aSelf.setNeedsUpdateMaskLayer()
  172. }
  173. }
Add Comment
Please, Sign In to add comment