button highlight state and gestures - iphone

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.

Related

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.

Cocoa Touch - Handling touches that get interrupted or lost

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.

How do I properly clean up a UIButton

I am having issues with UIButton with the iPhone SDK. I've determined that when I have a UIButton held down when the parent is removed (and the uibutton subsequently removed), I get some strange behavior. In my case, the app stops receiving any input whatsoever, as if the cleaned up held-down button has hijacked the input system somehow.
Does anyone know if there is an appropriate way to clean up the UIButton that would release what I'm guessing is the framework thinking the released button is still held down?
Update: Adding detail to my issue
Basically, the simplified situation is that I have a root view with two characters (actors, not letter characters) and a number of buttons, among other views. The user can use the buttons to affect direction of movement, and when the characters are close to each other, the scene moves forward immediately into another mode, i.e. the buttons slide off, the view cleans up, and I construct a new one.
What this necessitates is the ability to immediately move on and clean up even if the button is still held down. I figured I could pull some tricks to delay cleaning the button up, but I would be surprised if there isn't simply some way to properly clean up the button for removal regardless of what state it is currently in. Hope that clarifies.
It might help if you would explain what behavior you are expecting. If you are calling -removeFromSuperview while holding down the view you want to remove (or its parent), I would suggest you instead hide the view and set some flag you can look for when the user finishes holding that will at that point call -removeFromSuperview.
It turned out the problem wasn't a button being held down but rather a call to set the center coordinate of a mapview with animation on, i.e.
[mMapView setCenterCoordinate:mCoordinates animated:YES];
If the view was cleaned up while the animation was still moving, the app would no longer respond to user input. I'm not certain why this is the case, still, but setting the animated flag to NO ensured the problem didn't arise.

iphone - forcing button to acknowledge touch programmatically

When you touch a UIButton it hides for a fraction of second and then it executes its action. This fast "blink" is the feedback the user needs to know that the button has been clicked.
In the project I am doing, I need to select the button programmatically, as if the user had clicked it. In other words, the same behavior has the button had been clicked by the user... a fast blink and execution of its action.
Is this possible to do?
thanks for any help.
The change in the appearance of the button is effected by setting the button's highlighted property. The property is automatically set to YES when the user touches down on the button, and back to NO when she releases.
The highlighted property is writable, so you can set it YES yourself to simulate a touch down. You'll probably want to use +[NSTimer scheduledTimerWithTimeInterval:invocation:repeats:] to set it back to NO after a short interval.
It is pretty simple, and probably there is a better solution.
First, use images to your button, and when you have to fire the button, you just change the button's image in the normal state to the pressed image, and after that, replace it back to the original. You can simply do it with a timer.

Drag & sweep with Cocoa on iPhone

I'm about to start a new iPhone app that requires a certain functionality but I'm not sure if it's doable. I'm willing to research but first I just wanted to know if I should at least consider it or not.
I haven't seen this in an app before (which is my main concern, even though I haven't seen too many apps since I don't own an iPhone), but an example would be the iPhone shortcuts panels: you can hold on an app, and then drag it to another panel, sweeping while still dragging it. But this is the core app, is it possible to reproduce something similar within a normal app?
I only need to be sure it can be done before I start digging, I don't need code examples or anything, but if you have some exact resources that you consider helpful, that would be appreciated.
Thanks!
Yes. If you have your custom UIView subclass instance inside a UIScrollView, your view controller just needs to set the UIScrollView to delay content touches and not allow it to cancel touch events.
[scrollView setCanCancelContentTouches:NO];
[scrollView setDelaysContentTouches:YES];
When the user taps and holds in the custom view, the event goes to that custom view, which can process the touch events to drag an item around, but if the user quickly swipes, it scrolls the view.
The "panel" view that you're referring to appears to be a UIPageControl view — although, perhaps, the specific incarnation of this view that Apple uses for the iPhone's home page may be customized.
Instances of generic UIView views that you might touch-and-drag will receive touch events. By overriding methods in the view, these events can be processed and passed to the page control, in order to tell it to "sweep" between pages.
If I wanted to do what you're asking about, that's how I might approach it. It seems doable to me, in any case.
Start with this: Swip from one view to the next view
Try using a UIButton that tracks the time since the state of the button changed to "highlighted". You may need to do this in order to track the dragging and move the button around:
Observing pinch multi-touch gestures in a UITableView
Check to see if the button starts overlapping one side of the screen while being dragged. If s certain amount of time elapses since the button first started overlapping the edge and then manipulate the UIScrollView so that it switches to the next page on the corresponding side of the screen
You may need to use NSTimer to keep track of how long the button is held down, etc.
In any case there's no reason why this couldn't work.
If UIButton doesn't work then perhaps try a custom subclass of UIControl (which tracks the same touch down actions etc.). If that doesn't work then use the window event intercept thing to track everything.