xcode 检测 UISlider 上的触摸?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10971154/
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
Detecting touches on a UISlider?
提问by Andrew Martin
I have a UISlider on screen, and I need to be able to detect when the user stops touching it. (so I can fade some elements away).
我在屏幕上有一个 UISlider,我需要能够检测到用户何时停止触摸它。(所以我可以淡化一些元素)。
I have tried using:
我试过使用:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
but this did not work when ending touches on a slider.
但这在结束触摸滑块时不起作用。
回答by
You can detect when a touch ends using two control events; try
您可以使用两个控制事件检测触摸何时结束;尝试
[slider addTarget:self action:@selector(touchEnded:)
forControlEvents:UIControlEventTouchUpInside];
or
或者
[slider addTarget:self action:@selector(touchEnded:)
forControlEvents:UIControlEventTouchUpOutside];
If you want to detect both types of the touchesEnd
event, use
如果要检测这两种类型的touchesEnd
事件,请使用
[slider addTarget:self action:@selector(touchEnded:)
forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
回答by Evan Mulawski
Instead of using touchesEnded:
(which shouldn't be used for this purpose anyway), attach an action to the UISlider
's UIControlEventValueChanged
event and set the continuous
property of the UISlider
to NO
, so the event will fire when the user finishes selecting a value.
不是使用touchesEnded:
(无论如何都不应该用于此目的),而是将操作附加到UISlider
的UIControlEventValueChanged
事件并将 的continuous
属性设置UISlider
为NO
,因此当用户完成选择值时将触发该事件。
mySlider.continuous = NO;
[mySlider addTarget:self
action:@selector(myMethodThatFadesObjects)
forControlEvents:UIControlEventValueChanged];
回答by Adrian
I couldn't get anything to capture both the start and end of the touches, but upon RTFD-ing, I came up with something that will do both.
我无法获得任何东西来捕捉触摸的开始和结束,但是在 RTFD 中,我想出了一些可以同时做到的东西。
@IBAction func sliderAction(_ sender: UISlider, forEvent event: UIEvent) {
if let touchEvent = event.allTouches?.first {
switch touchEvent.phase {
case .began:
print("touches began")
sliderTouchBegan()
case .ended:
print("touches ended")
sliderTouchEnded()
default:
delegate?.sliderValueUpdated(sender.value)
}
}
}
sliderTouchBegan()
and sliderTouchEnded()
are just methods I wrote that handle animations when the touch begins and when it ends. If it's not a begin or end, it's a default
and the slider value updates.
sliderTouchBegan()
并且sliderTouchEnded()
只是我编写的处理触摸开始和结束时的动画的方法。如果它不是开始或结束,则是 adefault
并且滑块值会更新。