Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. class GradientButton: UIButton {
  2. let gradient: CAGradientLayer = CAGradientLayer()
  3. internal override init(frame: CGRect) {
  4. super.init(frame: frame)
  5. }
  6. internal required init?(coder aDecoder: NSCoder) {
  7. super.init(coder: aDecoder)
  8. }
  9. required init(_ colors: [UIColor], locations: [NSNumber]?) {
  10. super.init(frame: .zero)
  11. applyGradient(colors,locations:locations)
  12. }
  13. func applyGradient(_ colors: [UIColor], locations: [NSNumber]?) {
  14. gradient.colors = colors
  15. gradient.locations = locations
  16. gradient.startPoint = CGPoint(x: 0.0, y: 1.0)
  17. gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
  18. layer.insertSublayer(gradient, at: 0)
  19. }
  20. override func layoutSublayers(of layer: CALayer) {
  21. super.layoutSublayers(of: layer)
  22. gradient.frame = self.bounds
  23. }
  24. }
  25.  
  26. let button = GradientButton([.red,.blue], locations: nil)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement