开发者

iOS: inconsistent behaviour with UIButton's UIControlEventTouchUpOutside event

开发者 https://www.devze.com 2023-03-21 20:44 出处:网络
I want to get dual functionality from a UIButton. When the user touches down on the button,I want it to immediately trigger behaviour A

I want to get dual functionality from a UIButton.

When the user touches down on the button, I want it to immediately trigger behaviour A

But if the user then drags his finger outside of the button before releasing it, I would like to also trigger behaviour B upon release.

So,

        [presetButton开发者_高级运维 addTarget: self 
                         action: @selector( presetButtonHit: )
               forControlEvents: UIControlEventTouchDown ];  

        [presetButton addTarget: self 
                         action: @selector( presetButtonSwipe: )
               forControlEvents: UIControlEventTouchUpOutside ];  

As a test case I will simply log 'A' or 'B' from the respective action methods.

A always gets hit correctly.

However, B is sporadic. I can methodically finger down, drag well outside of the button, wait a second and release. And it usually wont' fire. Alternatively if I do a quick flick, it is more likely to fire. But still 50-50.

All I have is a UIView with a background image and six buttons.

What can I do to track this one down?

I would rather get to the bottom of it than mess around manually tracking touches up and down; but if I have to do that, is there any clean way? From what I can see the it is going to be a royal PITA, and I'm going to have to figure out whether touches up/down gets hit before my button's touch up/down gets hit, set flags, etc.


This is a duplicate of Strange UIButton behavior: Is that normal? but I recommend keeping it because the question title is well worded.

When my finger goes down on the button, the image darkens

as I drag my finger away, the moment I pass an invisible circumference a clear centimetre and more beyond the button's frame, the image returns to its original colour

this is the typical behaviour for ' cancelling a touch by moving the finger far enough away before releasing '

if you release the finger inside this zone, it is going to fire the touch up inside. sim for outside.

looks as though Apple didn't anticipate anyone using these methods for getting a dual functionality from UIButton.

Now, is there any simple way to catch the XY touch coordinates from within the action. the action just receives the ID of the button that was pressed.

But here is an elegant way:

http://sree.cc/iphone/handling-touche-events-for-uibuttons-in-iphone

UIControl has methods for detecting drag start / move / stop. You can override these and get the screen coordinates.

0

精彩评论

暂无评论...
验证码 换一张
取 消