GradientButton.swift 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //
  2. // GradientButton.swift
  3. // PhysicalWallPaper
  4. //
  5. // Created by nkl on 2024/11/8.
  6. //
  7. import Foundation
  8. import UIKit
  9. public class GradientButton: UIButton {
  10. var colors: [UIColor]?
  11. var from: CGPoint = CGPoint(x: 0, y: 0)
  12. var to: CGPoint = CGPoint(x: 1, y: 0)
  13. var index: Int?
  14. public required init(colors: [UIColor]? = nil,
  15. from: CGPoint = CGPoint(x: 0, y: 0),
  16. to: CGPoint = CGPoint(x: 1, y: 0),
  17. index: Int? = nil) {
  18. self.colors = colors
  19. self.from = from
  20. self.to = to
  21. self.index = index
  22. super.init(frame: .zero)
  23. }
  24. required init?(coder: NSCoder) {
  25. super.init(coder: coder)
  26. }
  27. public override func layoutSubviews() {
  28. super.layoutSubviews()
  29. if let colors = colors {
  30. setGradient(colors: colors, from: from, to: to, index: index)
  31. }
  32. }
  33. public func set(colors: [UIColor]? = nil,
  34. from: CGPoint = CGPoint(x: 0, y: 0),
  35. to: CGPoint = CGPoint(x: 1, y: 0),
  36. index: Int? = nil) {
  37. self.colors = colors
  38. self.from = from
  39. self.to = to
  40. self.index = index
  41. }
  42. }