Cocoa Touch - Handling touches that get interrupted or lost - iphone

I have a couple places in my app where UI elements reset themselves when a touch ends on them. For example, hiding a dashed outline, sliding a view back to the default position.
The problem is that on occasion the app loses track of the touches. One example is if I slide the view upward and cause a UIAlertView to show, the view doesn't slide back, because the reset code is in touches ended. The touch ends during the time the UIAlertView is active, and the view doesn't reset. The same example works for hiding the dashed outlines.
My question is, where/how can I handle the reseting of these custom UI elements so that when a touch ends without being noticed it will still reset. TouchesEnded isn't always doing it for me.

Have you try touchesCancelled? I think it is called when the touch get interrupted.
EDIT: If this doesn't work then maybe you can manually add code that cancel the touch when you initiate the alert.

Related

button highlight state and gestures

I've been struggling with this for a while already and some
help would be useful.
Imagine I have a UIButton, which starts in highlighted/selected state.
If a user taps it, then highlighted/selected state changes.
I've implemented this and it works fine. Problems start
for example if user taps inside the button region,
does not release her/his finger, and moves mouse
outside the button area -- at this time my Button
would usually lose highlighted/selected state.
Anyway, I have solved this issue too, by overriding UIControlEventTouchUpOutside
and making button keep the state it had before...
But now another problem comes in, similar to the above,
if a user taps the button, does not release his/her
finger, and moves the finger to the right say (horizontally,
which also makes my dialog for instance go to the right),
then I again lose "selected/highlighted" state....
I believe again some kind of gesture similar to - but different -
than UIControlEventTouchUpOutside is being called which
removes my selected state...
Do you know what can be going in here? Any advice appreciated.
ps. I have fixed all the issues by just setting different images
for normal/selected states using the interface builder.
For the highlighted effect you can use the: setHighlighted of UIButton class.
When you start touching the button set the highlighted property of button to YES.
when you start touch:
yourButton.highlighted = YES;
When you stop touch:
yourButton.highlighted = NO;
I have fixed all the issues by just setting different images for normal/selected states using the interface builder.

How to cancel UITapGestureRecognizer?

I noticed that the single tap happens when the touch dragging is performed quickly.
I want to cancel the single tap if the user moves the touch.
How can I do this?
I noticed that the single tap happens when the touch dragging is performed quickly.
My suggestion is to do nothing. Assuming you have everything set up properly then what you are going to end up doing is creating an app specific behavior for dragging simply to accommodate your personal preference. This could confuse your customers and possibly prevent your app from getting approved.
If the problem you are experience cannot be duplicated in other apps then I would guess that you are doing something incorrectly with respect to the responder chain, and you should focus on fixing that first.
I'm going to assume that you don't want to cancel the tap gesture itself (once the tap gesture has fired, I don't think you can cancel the gesture, it's already done!), but rather the code fired from it.
What you could do is create a timer, and delay the action created by the tap gesture by a second or two. You then have that time interval to cancel the action.

ccTouchesMoved and selector:#selector(ButtonTapped:)

Ill try and keep this as simple as possible.
I have a guy which you drag around the screen
I have a shoot button which fires bullets
To drag the guy around the screen im using ccTouchesMoved, basically get the users finger movement and set the guy to that place. Done.
I have a button which is a CCMenuItemImage and if it is tapped then is called a selector to run a method. The method simply fires some bullets. Done.
Now my problem is i cant have these two things work at the same time. I would like the player to be able to drag around the guy and shoot at the same time. Im assuming the button is not being tapped becuase the user is touching and dragging around the player.
How can i fix this? Still be able to drag around with one finger and press the shoot button with the other and they both work together?
You might want to implement your fire button using a sprite. You can handle ccTouchesBegan and ccTouchesEnded to check for a press within the bounds of your button, to swap it with a pressed state (on begin) or to implement the fire action (on end).
In your ccTouchesMoved handler, you can iterate over the set of touches and determine if the touch that moved is the one over the button, or the touch that should change the character position.

UIScrollView with embedded UIWebView not scrolling after holding

I have a UIWebView which is embedded in a UIScrollView. The webView is resized so that the scroll view manages all the scrolling (I need control over the scrolling).
In the webView I have disabled userSelection via '-webkit-user-select: none;'
Everything is working fine except one annoying detail. When I hold down my finger on the content before starting to scroll for about a second the scrollView won't scroll. My best guess is, that it has something to do with userSelection. The time is about the same it usually takes for the copy/paste/magnifying-thing to appear which usually disables scrolling as well.
I am running out of ideas on how to solve this. Every help would be greatly appreciated!
Thanks!
EDIT: Another aspect of the problem is, that the non-scrolling actually triggers JS-Eventhandler (click, mousedown, mouseup) inside my webView which leads to surprising app behavior. The user puts her finger down, waits, scrolls, nothing happens, removes her finger and this is perceived as a click, which feels wrong from a users perspective.
I would guess what is happening is that after that short duration, the scrollview is no longer interpreting the touch as being on it's view and instead passes the touch down to it's content views.
Have you tried delaying the content touches for the scrollview? This will essentially tell the scrollview to delay taking action on the touch event and instead to briefly monitor the touch and if the touch moves then it recognizes it as a swipe gesture for scrolling. If it doesn't move, it will eventually pass the touch along to it's subviews.
scrollView.delaysContentTouches = YES;
I think even then, there is a standard delay time before the scrollview will pass the touch events along the responder chain. If you hold for too long, it's going to naturally perceive it as being a press down event rather than a scroll event.
This question is not relevant anymore. As of iOS 5.0 the UIWebView is based on a real UIScrollView and also exposes that UIScrollView via a property. Use that instead.
And don't mess with UIWebViews embedded in UIScrollViews anymore. The documentation explicitly advises against that.
Relevant Documentation

UIButton subclass continuously highlighted after an popping up and UIAlertView

I am subclassing a UIButton and detecting finger movements, if the user swipes his finger in a certain way I will pop up an UIAlerView.
All good, except that after dismissing the UIAlertView... when the user next touches the UIButton the button goes to it's highlighted state and gets stuck there, continuously highlighted, even when no finger touching it.
Pressing the button again the UIButton begins to behave normally (only highlights when touched).
So I can only presume that an alert during a swipe, screws up the process of events, the touchesEnded never firing perhaps?
If anyone has any ideas on how to 'reset' the button after the swipe so that it behaves as it should, I would be grateful.
Are you resetting your state in touchesCancelled?