简体   繁体   English

触摸移动拖动按钮

[英]Drag button with touch move

I am working on a custom controller. 我正在使用自定义控制器。 I want to create a slider, to choose between more options. 我想创建一个滑块,以便在更多选项之间进行选择。 The problem is that when I touch the button I want to it doesn't call my touchesBegan method. 问题是,当我触摸按钮时,我不希望它调用我的touchesBegan方法。 But if I press in any other part of my view, it works. 但是,如果我按视图的其他任何部分,它都会起作用。 How can I get my button to move? 我如何才能移动按钮?

Thanks. 谢谢。

The UIButton is capturing the touch events. UIButton正在捕获触摸事件。 if you try this 如果你尝试这个

myButton.userInteractionEnabled = NO;

Then it will no longer consume the touch events but it will also no longer fire the onTouchUpInside event. 然后它将不再消耗触摸事件,但也将不再触发onTouchUpInside事件。 In this case you process all of the touches in the super view and position the slider button accordingly. 在这种情况下,您将处理超级视图中的所有触摸,并相应地放置滑块按钮。

It should drag just fine. 它应该拖动就好了。 This will work for any UIView. 这将适用于任何UIView。

touchesBegan will not work on a UIButton .. for that to work you will have to subclass UIButton and implement touchesBegan touchesBegan不能在UIButton上工作..为此,您必须继承UIButton并实现touchesBegan

refer this answer for more info https://stackoverflow.com/a/4863734/919545 请参阅此答案以获取更多信息https://stackoverflow.com/a/4863734/919545

Why don't you create a custom UIView and avoid using the UIButton class? 为什么不创建自定义UIView并避免使用UIButton类? It is simplier that subclassing UIButto or to set 子类化UIButto或设置更简单

myButton.userInteractionEnabled = NO;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM