在 isContinuous 为真时捕获 UISlider 中的最后一个值

capturing the last value in UISlider while isContinuous is true

我有一个以编程方式设置的 UISlider,其中 属性

slider.isContinuous = true

适合我玩的游戏。随着滑块的移动,我的用户界面发生了一些变化。 现在,我还想捕获滑块中最后更改的值作为离散值(与将 .isContinuous 设置为 false 时的行为相同)。有人找到方法了吗?

    @IBAction func sliderChanged(_ sender: UISlider, forEvent event: UIEvent) {
        if let touchEvent = event.allTouches?.first {
            switch touchEvent.phase {
            case .began:
                debugPrint("touch begin")
                debugPrint("current value: \(sender.value)")
            case .ended:
                debugPrint("touch end")
                debugPrint("current value: \(sender.value)")
                // record current value, save it in the view state
            default:
                debugPrint("touch continue")
            }
        }
    }

主要思想是在到达 .ended 的情况下保存值,并忽略其他不相关的情况。

更新:https://github.com/0x00Zhoujialei/DiscreteIsContinuous是我刚刚更新的demo,你可以运行这个demo,在控制台上查看结果。