ios iPhone:如何检测滑块拖动的结束?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9390298/
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
iPhone : How to detect the end of slider drag?
提问by DroidHeaven
How to detect the event when the user has ended the drag of a slider pointer?
当用户结束拖动滑块指针时如何检测事件?
回答by Rok Jarc
If you don't need any data inbetween drag, than you should simply set:
如果您在拖动之间不需要任何数据,那么您应该简单地设置:
[mySlider setContinuous: NO];
This way you will receive valueChanged
event only when the user stops moving the slider.
这样您valueChanged
只会在用户停止移动滑块时收到事件。
Swift 5version:
斯威夫特 5版本:
mySlider.isContinuous = false
回答by pinch
You can add an action that takes twoparameters, sender and an event, for UIControlEventValueChanged:
您可以为 UIControlEventValueChanged添加一个带有两个参数的操作,发送者和一个事件:
[slider addTarget:self action:@selector(onSliderValChanged:forEvent:) forControlEvents:UIControlEventValueChanged]
Then check the phase of the touch object in your handler:
然后检查处理程序中触摸对象的阶段:
- (void)onSliderValChanged:(UISlider*)slider forEvent:(UIEvent*)event {
UITouch *touchEvent = [[event allTouches] anyObject];
switch (touchEvent.phase) {
case UITouchPhaseBegan:
// handle drag began
break;
case UITouchPhaseMoved:
// handle drag moved
break;
case UITouchPhaseEnded:
// handle drag ended
break;
default:
break;
}
}
Swift 4 & 5
斯威夫特 4 & 5
slider.addTarget(self, action: #selector(onSliderValChanged(slider:event:)), for: .valueChanged)
@objc func onSliderValChanged(slider: UISlider, event: UIEvent) {
if let touchEvent = event.allTouches?.first {
switch touchEvent.phase {
case .began:
// handle drag began
case .moved:
// handle drag moved
case .ended:
// handle drag ended
default:
break
}
}
}
Note in Interface Builder when adding an action you also have the option to add both sender and event parameters to the action.
请注意,在 Interface Builder 中添加操作时,您还可以选择将发送者和事件参数添加到操作中。
回答by thomas
I use the "Touch Up Inside" and "Touch up outside" notifications.
我使用“内部修饰”和“外部修饰”通知。
Interface Builder:
界面生成器:
Connect both notifications in the Interface Builder to your receiving method. The method could look like this:
将 Interface Builder 中的两个通知连接到您的接收方法。该方法可能如下所示:
- (IBAction)lengthSliderDidEndSliding:(id)sender {
NSLog(@"Slider did end sliding... Do your stuff here");
}
In code:
在代码中:
If you want to wire it programatically you would have something like this in your viewWillAppear (or wherever it fits you) call:
如果您想以编程方式连接它,您将在 viewWillAppear(或任何适合您的地方)调用中包含以下内容:
[_mySlider addTarget:self
action:@selector(sliderDidEndSliding:)
forControlEvents:(UIControlEventTouchUpInside | UIControlEventTouchUpOutside)];
The receiving method would look like this:
接收方法如下所示:
- (void)sliderDidEndSliding:(NSNotification *)notification {
NSLog(@"Slider did end sliding... Do your stuff here");
}
回答by rob mayoff
Since UISlider
is a subclass of UIControl
, you can set a target and action for its UIControlEventTouchUpInside
.
由于UISlider
是 的子类UIControl
,您可以为其设置目标和操作UIControlEventTouchUpInside
。
If you want to do it in code, it looks like this:
如果你想在代码中做到这一点,它看起来像这样:
[self.slider addTarget:self action:@selector(dragEndedForSlider:)
forControlEvents:UIControlEventTouchUpInside];
That will send you a dragEndedForSlider:
message when the touch ends.
这将dragEndedForSlider:
在触摸结束时向您发送一条消息。
If you want to do it in your nib, you can control-click your slider to get its connections menu, and then drag from the “Touch Up Inside” socket to the target object.
如果你想在你的笔尖上做这件事,你可以按住 Control 键点击你的滑块来获得它的连接菜单,然后从“Touch Up Inside”插座拖到目标对象。
You should also add a target and action for UIControlEventTouchUpOutside
and for UIControlEventTouchCancel
.
您还应该为UIControlEventTouchUpOutside
和 为添加目标和操作UIControlEventTouchCancel
。
回答by Guy Daher
Swift 5 and Swift 4
斯威夫特 5 和斯威夫特 4
Add target for
touchUpInside
andtouchUpOutside
events. You can do it either programatically or from storyboard. This is how you do it programaticallyslider.addTarget(self, action: #selector(sliderDidEndSliding), for: [.touchUpInside, .touchUpOutside])
Write your function to handle the end of slider drag
func sliderDidEndSliding() { print("end sliding") }
为
touchUpInside
和touchUpOutside
事件添加目标。您可以通过编程或故事板来完成。这就是您以编程方式执行的方式slider.addTarget(self, action: #selector(sliderDidEndSliding), for: [.touchUpInside, .touchUpOutside])
编写函数来处理滑块拖动的结束
func sliderDidEndSliding() { print("end sliding") }
回答by Mudit Bajpai
You can use:
您可以使用:
- (void)addTarget:(id)target action:(SEL)action
forControlEvents:(UIControlEvents)controlEvents
to detect when the touchDown and touchUp events occur in UISlider
检测 UISlider 中的 touchDown 和 touchUp 事件何时发生
回答by scorpiozj
I think you need the control event UIControlEventTouchUpInside
.
我认为你需要控制的事件UIControlEventTouchUpInside
。
回答by Andy
Swift 3 answer
斯威夫特 3 个回答
I had same problem and eventually got this to work, so maybe it will help somebody. Connect your_Slider to 'Value Changed' in storyboard and when slider moved "Slider Touched" actioned and when let go "Slider Released".
我遇到了同样的问题,最终让它起作用,所以也许它会帮助某人。将 your_Slider 连接到故事板中的“Value Changed”,当滑块移动时“Slider Touched”动作,松开时“Slider Released”。
@IBAction func your_Slider(_ sender: UISlider) {
if (yourSlider.isTracking) {
print("Slider Touched")
} else {
print("Slider Released")
}
}
回答by Jamie Birch
Swift 3.1
斯威夫特 3.1
Sometimes you will want the slider's drag events to fire continuously, but have the option of executing different code depending on whether the slider is still being dragged or has just finished being dragged.
有时您会希望滑块的拖动事件持续触发,但可以根据滑块是仍在拖动还是刚刚完成拖动来选择执行不同的代码。
To do this, I've expanded on Andy's answer above that makes use of the slider's isTracking
property. I needed to record two states: sliderHasFinishedTracking
and sliderLastStoredValue
.
为此,我扩展了上面使用滑块isTracking
属性的安迪的答案。我需要记录两个状态:sliderHasFinishedTracking
和sliderLastStoredValue
。
My code correctly:
我的代码正确:
doesn't fire an action upon starting drag
fires the action if the user only nudges the slider with a single tap (meaning that
isTracking
remainsfalse
)
开始拖动时不触发动作
如果用户仅通过一次轻点轻推滑块(意味着
isTracking
仍然存在false
),则触发该操作
class SliderExample: UIViewController {
var slider: UISlider = UISlider()
var sliderHasFinishedTracking: Bool = true
var sliderLastStoredValue: Float!
override func loadView() {
super.loadView()
slider.minimumValue = 100.0
slider.maximumValue = 200.0
slider.isContinuous = true
slider.value = 100.0
self.sliderLastStoredValue = slider.value
slider.addTarget(self, action: #selector(respondToSlideEvents), for: .valueChanged)
// add your slider to the view however you like
}
func respondToSlideEvents(sender: UISlider) {
let currentValue: Float = Float(sender.value)
print("Event fired. Current value for slider: \(currentValue)%.")
if(slider.isTracking) {
self.sliderHasFinishedTracking = false
print("Action while the slider is being dragged ?")
} else {
if(self.sliderHasFinishedTracking && currentValue == self.sliderLastStoredValue){
print("Slider has finished tracking and its value hasn't changed, " +
"yet event was fired anyway. ") // No need to take action.
} else {
self.sliderHasFinishedTracking = true
print("Action upon slider drag/tap finishing ??")
}
}
self.sliderLastStoredValue = currentValue
}
}
回答by pansora abhay
In Swift 4 you can use this function
在 Swift 4 中你可以使用这个函数
1 Frist step:- Adding the target in slider
1 第一步:- 在滑块中添加目标
self.distanceSlider.addTarget(self, action: #selector(self.sliderDidEndSliding(notification:)), for: ([.touchUpInside,.touchUpOutside]))
2 Second step:- Create a function
2 第二步:- 创建一个函数
@objc func sliderDidEndSliding(notification: NSNotification)
{
print("Hello-->\(distanceSlider.value)")
}