UIGestureRecognizer 从头开始​​制作 UISlider

UIGestureRecognizer to make UISlider from scratch

嗨,

我正在尝试基于 UIGestureRecognizer 从头开始​​实现自定义 UISlider,因为我必须使拇指视图成为具有简洁设计的自定义视图。 UIGestureRecognizer 的使用是技术负责人的要求。

到目前为止我所拥有的是我进行测试的样本:

func setupSlider() {        
    let panGesture = UIPanGestureRecognizer(target: self, action: #selector(dragged(gestureRecognizer:)))
    sliderView.isUserInteractionEnabled = true
    sliderView.addGestureRecognizer(panGesture)
}

@objc func dragged(gestureRecognizer: UIPanGestureRecognizer) {
    if gestureRecognizer.state == UIGestureRecognizer.State.began || gestureRecognizer.state == UIGestureRecognizer.State.changed {
        let translation = gestureRecognizer.translation(in: self.sliderContainer)
        gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)
        gestureRecognizer.setTranslation(CGPoint(x: 0, y: 0), in: self.view)
    }
}

在这一点上我有两个问题:

  1. 将滑块视图保持在容器中间
  2. 能够根据区间的最小值和最大值得到滑块的“当前值”

你能帮忙提点建议吗?

亲切的问候。

从技术上讲,这不是您问题的答案,但是....

I'm trying to implement a custom UISlider from scratch based on UIGestureRecognizer because I have to make the thumb view a custom one with a clean design.

您知道可以自定义缩略图吗?

例如这篇文章: https://zeitschlag.net/customizing-uislider/
这是API:setThumbImage(_:for:)

The UIGestureRecognizer usage is a request from technical lead.

如果您毕竟可以使用 UISlider 获得结果,我相信您的领导会欢迎它。