123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- //
- // CLSliderView.swift
- // CLSlider
- //
- // Created by weclouds on 2020/2/17.
- // Copyright © 2020 weclouds. All rights reserved.
- //
- import UIKit
- class CLSliderView: UIView {
-
- var valueChange:((CGFloat)->Void)?
- var value: CGFloat? = 0{
- didSet{
- if self.value! > 1 {
- self.value = 1
- }
- if self.value! < 0 {
- self.value = 0
- }
- let x = value! * self.frame.size.width
- self.indicator.center = CGPoint(x: x, y: self.bounds.maxY / 2)
- }
- }
- var thumbImage:UIImage?{
- didSet{
- if let thumbImage = self.thumbImage {
- self.indicator.image = thumbImage
- }
- }
- }
-
- private lazy var indicator: UIImageView = {
- let indicatorView = UIImageView(frame: CGRect(x: 0, y: 0, width: 8, height: 30))
- // indicatorView.backgroundColor = UIColor.red
- // indicatorView.layer.backgroundColor = UIColor.init(red: 87/255.0, green: 63/255.0, blue: 63/255.0, alpha: 1.0).cgColor
- return indicatorView
- }()
-
- override init(frame: CGRect) {
- super.init(frame: frame)
- createUI()
- }
- func createUI() {
- //self.bounds.width
- self.backgroundColor = UIColor.clear
- let bgView = UIView(frame: CGRect(x: 0, y: 3.5, width: self.frame.width , height: self.bounds.height - 3.5 - 3))
- bgView.layer.cornerRadius = 4
- bgView.layer.masksToBounds = true
- addSubview(bgView)
- let bgLayer1 = CAGradientLayer()
- 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]
- bgLayer1.locations = [0, 1]
- bgLayer1.frame = bgView.bounds
- bgLayer1.startPoint = CGPoint(x: 0, y: 0)
- bgLayer1.endPoint = CGPoint(x: 1, y: 0)
- bgView.layer.addSublayer(bgLayer1)
- addSubview(indicator)
- }
-
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
- override func draw(_ rect: CGRect) {
- let x = value! * self.frame.size.width
- self.indicator.center = CGPoint(x: x, y: self.bounds.maxY / 2)
- }
-
- override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
- let touch = touches.first
- indcatorViewWithTouch(touch!)
- }
- override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
- let touch = touches.first
- indcatorViewWithTouch(touch!)
- }
- func indcatorViewWithTouch(_ touch:UITouch) {
- let p = touch.location(in: self)
- if p.x > 0 && p.x < self.bounds.size.width {
- self.indicator.center = CGPoint(x: p.x, y: self.indicator.center.y)
- self.value = self.indicator.center.x / self.bounds.size.width
- if let block = self.valueChange {
- block(self.value!)
- }
- }
- }
- }
|