简体   繁体   中英

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. 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. 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. 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.

touchesBegan will not work on a UIButton .. for that to work you will have to subclass UIButton and implement touchesBegan

refer this answer for more info https://stackoverflow.com/a/4863734/919545

Why don't you create a custom UIView and avoid using the UIButton class? It is simplier that subclassing UIButto or to set

myButton.userInteractionEnabled = NO;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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