ios – The way to make this design component?

0
12
ios – The way to make this design component?


I believe it could be simpler to make your individual utilizing a UIStackView and UIPanGestureRecognizer as an alternative of making an attempt to inherit a UISlider.

Here is what I got here up with:

Result

CustomSliderView.swift:

import UIKit

ultimate class CustomSliderView: UIView {
    
    // MARK: - Properties
    
    var progress: Int = 0 {
        didSet {
            progress = max(0, min(progress, sectionCount))
            updateProgress()
        }
    }
    
    non-public let sectionCount = 10
    non-public var sectionHeight: CGFloat = 0
    
    non-public var startProgress: Int = 0
    non-public var startTouchY: CGFloat = 0
    
    // MARK: - Views
    
    non-public let stackView = UIStackView()
    non-public let filledView = UIView()
    
    // MARK: - Init
    
    override init(body: CGRect) {
        tremendous.init(body: body)
        
        commonInit()
    }
    
    required init?(coder: NSCoder) {
        tremendous.init(coder: coder)
        
        commonInit()
    }
    
    non-public func commonInit() {
        setupView()
        setupStackView()
        setupFilledView()
        addGestureRecognizer(UIPanGestureRecognizer(goal: self, motion: #selector(handlePan(_:))))
    }
    
    // MARK: - Format
    
    override func layoutSubviews() {
        tremendous.layoutSubviews()
        sectionHeight = bounds.top / CGFloat(sectionCount)
        updateProgress()
    }
    
    non-public func setupView() {
        backgroundColor = .clear
        layer.cornerRadius = 10
        layer.masksToBounds = true
    }
    
    non-public func setupStackView() {
        stackView.axis = .vertical
        stackView.distribution = .fillEqually
        stackView.spacing = 1
        stackView.layer.cornerRadius = 10
        stackView.clipsToBounds = true
        
        addSubview(stackView)
        
        stackView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            stackView.leadingAnchor.constraint(equalTo: leadingAnchor),
            stackView.trailingAnchor.constraint(equalTo: trailingAnchor),
            stackView.topAnchor.constraint(equalTo: topAnchor),
            stackView.bottomAnchor.constraint(equalTo: bottomAnchor)
        ])
        
        for _ in 0..

Utilization:

view.addSubview(customSlider)
customSlider.translatesAutoresizingMaskIntoConstraints = false
        
NSLayoutConstraint.activate([
    customSlider.centerXAnchor.constraint(equalTo: view.centerXAnchor),
    customSlider.centerYAnchor.constraint(equalTo: view.centerYAnchor),
    customSlider.widthAnchor.constraint(equalToConstant: 60),
    customSlider.heightAnchor.constraint(equalToConstant: 200)
])

LEAVE A REPLY

Please enter your comment!
Please enter your name here