CLSliderView.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // CLSliderView.swift
  3. // CLSlider
  4. //
  5. // Created by weclouds on 2020/2/17.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. class CLSliderView: UIView {
  10. var valueChange:((CGFloat)->Void)?
  11. var value: CGFloat? = 0{
  12. didSet{
  13. if self.value! > 1 {
  14. self.value = 1
  15. }
  16. if self.value! < 0 {
  17. self.value = 0
  18. }
  19. let x = value! * self.frame.size.width
  20. self.indicator.center = CGPoint(x: x, y: self.bounds.maxY / 2)
  21. }
  22. }
  23. var thumbImage:UIImage?{
  24. didSet{
  25. if let thumbImage = self.thumbImage {
  26. self.indicator.image = thumbImage
  27. }
  28. }
  29. }
  30. private lazy var indicator: UIImageView = {
  31. let indicatorView = UIImageView(frame: CGRect(x: 0, y: 0, width: 8, height: 30))
  32. // indicatorView.backgroundColor = UIColor.red
  33. // indicatorView.layer.backgroundColor = UIColor.init(red: 87/255.0, green: 63/255.0, blue: 63/255.0, alpha: 1.0).cgColor
  34. return indicatorView
  35. }()
  36. override init(frame: CGRect) {
  37. super.init(frame: frame)
  38. createUI()
  39. }
  40. func createUI() {
  41. //self.bounds.width
  42. self.backgroundColor = UIColor.clear
  43. let bgView = UIView(frame: CGRect(x: 0, y: 3.5, width: self.frame.width , height: self.bounds.height - 3.5 - 3))
  44. bgView.layer.cornerRadius = 4
  45. bgView.layer.masksToBounds = true
  46. addSubview(bgView)
  47. let bgLayer1 = CAGradientLayer()
  48. bgLayer1.colors = [UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1).cgColor, UIColor(red: 87/255.0, green: 63/255.0, blue: 149/255.0, alpha: 1).cgColor]
  49. bgLayer1.locations = [0, 1]
  50. bgLayer1.frame = bgView.bounds
  51. bgLayer1.startPoint = CGPoint(x: 0, y: 0)
  52. bgLayer1.endPoint = CGPoint(x: 1, y: 0)
  53. bgView.layer.addSublayer(bgLayer1)
  54. addSubview(indicator)
  55. }
  56. required init?(coder: NSCoder) {
  57. fatalError("init(coder:) has not been implemented")
  58. }
  59. override func draw(_ rect: CGRect) {
  60. let x = value! * self.frame.size.width
  61. self.indicator.center = CGPoint(x: x, y: self.bounds.maxY / 2)
  62. }
  63. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  64. let touch = touches.first
  65. indcatorViewWithTouch(touch!)
  66. }
  67. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
  68. let touch = touches.first
  69. indcatorViewWithTouch(touch!)
  70. }
  71. func indcatorViewWithTouch(_ touch:UITouch) {
  72. let p = touch.location(in: self)
  73. if p.x > 0 && p.x < self.bounds.size.width {
  74. self.indicator.center = CGPoint(x: p.x, y: self.indicator.center.y)
  75. self.value = self.indicator.center.x / self.bounds.size.width
  76. if let block = self.valueChange {
  77. block(self.value!)
  78. }
  79. }
  80. }
  81. }