ios 检查用户是否在连续的 UISlider 上完成滑动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8792702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:12:09  来源:igfitidea点击:

Check if user finished sliding on a continuous UISlider?

objective-cioscocoa-touch

提问by winsmith

In my app, I have a couple of UISlider instances to change various values. The values are displayed right next to the slider, as well as rendered in a 3d space in another visible part of the app.

在我的应用程序中,我有几个 UISlider 实例来更改各种值。这些值显示在滑块旁边,并在应用程序的另一个可见部分的 3d 空间中呈现。

The 3d part includes some rather heavy calculations, and right now it doesn't seem possible to update it live as the slider changes. That would imply that I'd have to set the slider's continuousproperty to NO, therefore only getting updates when the slider has finished changing.

3d 部分包括一些相当繁重的计算,现在似乎不可能随着滑块的变化实时更新它。这意味着我必须将滑块的continuous属性设置为NO,因此只有在滑块完成更改时才会获得更新。

I'd prefer to have the displayedvalue update live, however. Is there a way to have a slider that is continuous (so I can update my value-label in real time) and still sends some kind of message once the user has finished interacting with it? My gut feeling right now is to subclass UISlider and override the touchesEnded:method. Is that feasible?

但是,我更喜欢实时更新显示的值。有没有办法让滑块是连续的(这样我就可以实时更新我的​​值标签)并且在用户完成与其交互后仍然发送某种消息?我现在的直觉是继承 UISlider 并覆盖该touchesEnded:方法。那可行吗?

回答by Jasarien

You can do this with simple target/actions.

您可以使用简单的目标/操作来做到这一点。

Set a target and action for the UIControlEventValueChangedevent, and then another target and action for the UIControlEventTouchUpInsideevent. With the continuousproperty set to YES, the value changed event will fire as the slider changes value, while the touch up inside event will only fire when the user releases the control.

UIControlEventValueChanged事件设置目标和操作,然后为事件设置另一个目标和操作UIControlEventTouchUpInside。将continuous属性设置为YES,值更改事件将在滑块更改值时触发,而 touch up inside 事件仅在用户释放控件时触发。

回答by Stephen J

I just had to do this, so I looked up touch properties, and used the full IBAction header.

我只需要这样做,所以我查找了触摸属性,并使用了完整的 IBAction 标头。

This should be a viable alternative for people who want some extra control, though Jas's is definitely easier on the code side.

对于想要一些额外控制的人来说,这应该是一个可行的替代方案,尽管在代码方面 Jas 肯定更容易。

- (IBAction)itemSlider:(UISlider *)itemSlider withEvent:(UIEvent*)e;
{
    UITouch * touch = [e.allTouches anyObject];

    if( touch.phase != UITouchPhaseMoved && touch.phase != UITouchPhaseBegan)
    {
       //The user hasn't ended using the slider yet.
    }

}

:D

:D

回答by Agustin

Also note you should connect the UIControlEventTouchUpOutsideevent as well in case the user drags his finger out of the control before lifting it.

另请注意,您还应该连接UIControlEventTouchUpOutside事件,以防用户在抬起手指之前将其拖出控件。

回答by derpoliuk

In Swift 3:

在 Swift 3 中:

@IBAction func sliderValueChanged(_ slider: UISlider, _ event: UIEvent) {
    guard let touch = event.allTouches?.first, touch.phase != .ended else {
        // ended
        return
    }
    // not ended yet
}