|
| 1 | +// |
| 2 | +// CALayer+SwiftUIKit.swift |
| 3 | +// SwiftUIKit |
| 4 | +// |
| 5 | +// Created by Oskar on 08/03/2020. |
| 6 | +// |
| 7 | + |
| 8 | +import UIKit |
| 9 | + |
| 10 | +@available(iOS 9.0, *) |
| 11 | +public extension UIView { |
| 12 | + |
| 13 | + /// Change layer's background color |
| 14 | + /// - Parameter color: You can use `UIColor.colorName.cgColor` to pass UIColor value |
| 15 | + @discardableResult |
| 16 | + func layer(backgroundColor color: CGColor?) -> Self { |
| 17 | + layer.backgroundColor = color |
| 18 | + |
| 19 | + return self |
| 20 | + } |
| 21 | + |
| 22 | + /// Change layer's background color |
| 23 | + @discardableResult |
| 24 | + func layer(backgroundColor color: UIColor?) -> Self { |
| 25 | + layer.backgroundColor = color?.cgColor |
| 26 | + |
| 27 | + return self |
| 28 | + } |
| 29 | + |
| 30 | + /// Change layer's content's gravity. |
| 31 | + /// - Parameter gravity: Will be used as layer's gravity. |
| 32 | + @discardableResult |
| 33 | + func layer(contentsGravity: CALayerContentsGravity) -> Self { |
| 34 | + layer.contentsGravity = contentsGravity |
| 35 | + |
| 36 | + return self |
| 37 | + } |
| 38 | + |
| 39 | + /// Change layer's corner radius. |
| 40 | + /// - Parameter radius: value, defines corner radius. |
| 41 | + @discardableResult |
| 42 | + func layer(cornerRadius: Float) -> Self { |
| 43 | + layer.cornerRadius = CGFloat(cornerRadius) |
| 44 | + |
| 45 | + return self |
| 46 | + } |
| 47 | + |
| 48 | + /// Change layer's border color. |
| 49 | + /// - Parameter color: use `UIColor.colorName.cgColor` to pass UIColor value. |
| 50 | + @discardableResult |
| 51 | + func layer(borderColor: CGColor?) -> Self { |
| 52 | + layer.borderColor = borderColor |
| 53 | + |
| 54 | + return self |
| 55 | + } |
| 56 | + |
| 57 | + /// Change layer's border color. |
| 58 | + @discardableResult |
| 59 | + func layer(borderColor: UIColor?) -> Self { |
| 60 | + layer.borderColor = borderColor?.cgColor |
| 61 | + |
| 62 | + return self |
| 63 | + } |
| 64 | + |
| 65 | + /// Change layer's border width. |
| 66 | + /// - Parameter width: Will be used as width value. |
| 67 | + @discardableResult |
| 68 | + func layer(borderWidth: Float) -> Self { |
| 69 | + layer.borderWidth = CGFloat(borderWidth) |
| 70 | + return self |
| 71 | + } |
| 72 | + |
| 73 | + /// Change layer's opacity |
| 74 | + /// - Parameter value: Will be used as opacity value. |
| 75 | + @discardableResult |
| 76 | + func layer(opacity: Float) -> Self { |
| 77 | + layer.opacity = opacity |
| 78 | + |
| 79 | + return self |
| 80 | + } |
| 81 | + |
| 82 | + /// Change layer's isHidden value |
| 83 | + @discardableResult |
| 84 | + func layer(isHidden: Bool) -> Self { |
| 85 | + layer.isHidden = isHidden |
| 86 | + |
| 87 | + return self |
| 88 | + } |
| 89 | + |
| 90 | + /// Set masks to bounds value. |
| 91 | + @discardableResult |
| 92 | + func layer(masksToBounds: Bool) -> Self { |
| 93 | + layer.masksToBounds = masksToBounds |
| 94 | + |
| 95 | + return self |
| 96 | + } |
| 97 | + |
| 98 | + /// Set layer's mask. |
| 99 | + /// - Parameter layer: Allows to set existing type or create new one inside closure. |
| 100 | + @discardableResult |
| 101 | + func layer(mask: @autoclosure () -> CALayer?) -> Self { |
| 102 | + self.layer.mask = mask() |
| 103 | + |
| 104 | + return self |
| 105 | + } |
| 106 | + |
| 107 | + /// Set `isDoubleSided` parameter. |
| 108 | + @discardableResult |
| 109 | + func layer(isDoubleSided: Bool) -> Self { |
| 110 | + layer.isDoubleSided = isDoubleSided |
| 111 | + |
| 112 | + return self |
| 113 | + } |
| 114 | + |
| 115 | + /// Set masked corners value. |
| 116 | + /// - Parameter corners: You can pass exisiting value or create it inside closure |
| 117 | + @available(iOS 11.0, *) |
| 118 | + @discardableResult |
| 119 | + func layer(maskedCorners: @autoclosure () -> CACornerMask) -> Self { |
| 120 | + layer.maskedCorners = maskedCorners() |
| 121 | + |
| 122 | + return self |
| 123 | + } |
| 124 | + |
| 125 | + /// Set layer's shadow opacity. |
| 126 | + /// - Parameter opacity: |
| 127 | + @discardableResult |
| 128 | + func layer(shadowOpacity: Float) -> Self { |
| 129 | + layer.shadowOpacity = shadowOpacity |
| 130 | + |
| 131 | + return self |
| 132 | + } |
| 133 | + |
| 134 | + /// Change layer's shadow color. |
| 135 | + /// - Parameter color: Pass `UIColor.colorName.cgColor` to pass UIColor value |
| 136 | + @discardableResult |
| 137 | + func layer(shadowColor: CGColor?) -> Self { |
| 138 | + layer.shadowColor = shadowColor |
| 139 | + |
| 140 | + return self |
| 141 | + } |
| 142 | + |
| 143 | + /// Change layer's shadow color. |
| 144 | + @discardableResult |
| 145 | + func layer(shadowColor: UIColor?) -> Self { |
| 146 | + layer.shadowColor = shadowColor?.cgColor |
| 147 | + |
| 148 | + return self |
| 149 | + } |
| 150 | + |
| 151 | + /// Set layer's shadow radius. Takes `Float` and converts it to `CGFloat` for programmer's convenience |
| 152 | + /// - Parameter radius: |
| 153 | + @discardableResult |
| 154 | + func layer(shadowRadius: Float) -> Self { |
| 155 | + layer.shadowRadius = CGFloat(shadowRadius) |
| 156 | + |
| 157 | + return self |
| 158 | + } |
| 159 | + |
| 160 | + /// Changes layer's shadowOffset to given in parameter |
| 161 | + /// - Parameter offset: offset value, can be calculated using closure inside or just add ready one. |
| 162 | + @discardableResult |
| 163 | + func layer(shadowOffset: @autoclosure () -> CGSize) -> Self { |
| 164 | + layer.shadowOffset = shadowOffset() |
| 165 | + |
| 166 | + return self |
| 167 | + } |
| 168 | + |
| 169 | + /// Set shadow path by passing existing object or use curly braces to create own one. |
| 170 | + /// - Parameter path: |
| 171 | + @discardableResult |
| 172 | + func layer(shadowPath: @autoclosure () -> CGPath?) -> Self { |
| 173 | + layer.shadowPath = shadowPath() |
| 174 | + |
| 175 | + return self |
| 176 | + } |
| 177 | + |
| 178 | + /// Set layer's `allowsEdgeAntialiasing`. |
| 179 | + @discardableResult |
| 180 | + func layer(allowsEdgeAntialiasing: Bool) -> Self { |
| 181 | + layer.allowsEdgeAntialiasing = allowsEdgeAntialiasing |
| 182 | + |
| 183 | + return self |
| 184 | + } |
| 185 | + |
| 186 | + /// Set layer's `allowsGroupOpacity`. |
| 187 | + @discardableResult |
| 188 | + func layer(allowsGroupOpacity: Bool) -> Self { |
| 189 | + layer.allowsGroupOpacity = allowsGroupOpacity |
| 190 | + |
| 191 | + return self |
| 192 | + } |
| 193 | + |
| 194 | + /// Set layer's style. |
| 195 | + /// - Parameter style: You can pass exisiting value or create new one inside closure. |
| 196 | + @discardableResult |
| 197 | + func layer(style: @autoclosure () -> [AnyHashable: Any]?) -> Self { |
| 198 | + layer.style = style() |
| 199 | + |
| 200 | + return self |
| 201 | + } |
| 202 | + |
| 203 | + /// Set layer's filters. |
| 204 | + /// - Parameter filters: You can pass exisiting value or create new one inside closure. |
| 205 | + @discardableResult |
| 206 | + func layer(filters: @autoclosure () -> [Any]?) -> Self { |
| 207 | + layer.filters = filters() |
| 208 | + |
| 209 | + return self |
| 210 | + } |
| 211 | + |
| 212 | + /// Set layer's `isOpaque` Boolean. |
| 213 | + @discardableResult |
| 214 | + func layer(isOpaque: Bool) -> Self { |
| 215 | + layer.isOpaque = isOpaque |
| 216 | + |
| 217 | + return self |
| 218 | + } |
| 219 | + |
| 220 | + /// Set layer's `drawsAsynchronously` |
| 221 | + @discardableResult |
| 222 | + func layer(drawsAsynchronously: Bool) -> Self { |
| 223 | + layer.drawsAsynchronously = drawsAsynchronously |
| 224 | + |
| 225 | + return self |
| 226 | + } |
| 227 | + |
| 228 | + /// Add animation to a layer. |
| 229 | + /// - Parameters: |
| 230 | + /// - key: Key which allows to identify given animation. |
| 231 | + /// - animation: You can pass existing value or create it inside closure. |
| 232 | + @discardableResult |
| 233 | + func layer(addAnimation animation: CAAnimation, forKey key: String?) -> Self { |
| 234 | + layer.add(animation, forKey: key) |
| 235 | + |
| 236 | + return self |
| 237 | + } |
| 238 | + |
| 239 | + /// Remove layer's animation specified by given key. |
| 240 | + /// - Parameter key: Key that's assigned to animation. |
| 241 | + @discardableResult |
| 242 | + func layer(removeAnimationForKey key: String) -> Self { |
| 243 | + layer.removeAnimation(forKey: key) |
| 244 | + |
| 245 | + return self |
| 246 | + } |
| 247 | + |
| 248 | + /// Remove all existing layer's animations. |
| 249 | + @discardableResult |
| 250 | + func removeAllAnimationsFromLayer() -> Self { |
| 251 | + layer.removeAllAnimations() |
| 252 | + |
| 253 | + return self |
| 254 | + } |
| 255 | + |
| 256 | + /// Modify the object's layer |
| 257 | + /// - Parameters: |
| 258 | + /// - closure: A trailing closure that receives itself.layer inside the closue |
| 259 | + @discardableResult |
| 260 | + func layer(_ closure: (CALayer) -> Void) -> Self { |
| 261 | + closure(layer) |
| 262 | + |
| 263 | + return self |
| 264 | + } |
| 265 | +} |
0 commit comments