Tap on UISlider to Set the Value

前端 未结 6 405
無奈伤痛
無奈伤痛 2021-02-01 19:20

I created a Slider (operating as control of the video, like YouTube has at the bottom) and set the maximum (duration) and minimum values. And then used SeekToTime t

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-01 20:09

    I like this approach

    extension UISlider {
        public func addTapGesture() {
            let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
            addGestureRecognizer(tap)
        }
    
        @objc private func handleTap(_ sender: UITapGestureRecognizer) {
            let location = sender.location(in: self)
            let percent = minimumValue + Float(location.x / bounds.width) * maximumValue
            setValue(percent, animated: true)
            sendActions(for: .valueChanged)
        }
    }
    

    And later just call

    let slider = UISlider()
    slider.addTapGesture()
    

提交回复
热议问题