How to dynamically change UIButton title during Touch and Hold - iphone

I am following this answer to change title of my UIButton. To sum up, here is what I have to do:
When user touches down, title of the button starts changing.
While touch is held, it should change every 1 second, and user
should be able to see it.
When user touches up (stops touching), button title should be fixed to the last value held.
I handle 1 using setTitle:forState:
I handle 2 using setTitle:forState:. This keeps happening as far as the timer keeps firing following user's touch, every 1 second.
I handle 3 using titleForState. However here I don't get the value set in 2.
Sometimes when I get correct value, there is still one problem: After touch is over, I still see in UI the same old title on that custom UIButton, not the one that I read using titleForState. (for that matter, even UIButton.textLabel.text gives wrong value)
1 - Am I following the right approach for touch and hold? (I mean using the timer approach depicted to set and read UIButton titles)
2 - If yes, what should I change in my code to read correct UIButton title value?
EDIT:
I got rid of the issue I get in 3 above. The reason for the issue was: I wasn't supplying same UIControlState combination while reading and writing. I was assuming that if you supply UIControlStateNormal | UIControlStateHighlighted for writing title and then use UIControlStateNormal for reading it back, it should give back the currect title value, and vice versa. Unfortunately that's not the case. I changed my app logic so that I only have to use one of these states at both times.
However the main issue still remains (2 above) - how to show title while the UIButton is still in highlighted state. Title is completely invisible while the touch is held.

how to show title while the UIButton is still in highlighted state. Title is completely invisible while the touch is held.
Is your button configured such that the title disappears during a touch event, without your code changing the title text? That is with no code changing the title does it still disappear?
I think your button needs to be redrawn, try calling [button setNeedsDisplay] after changing the title text.

Related

Swift: How Do I Stop A Pan Gesture from Cancelling When a Timer Fires?

This question is brief and I don't believe it requires showing code, but I'd like to learn why my app is doing this.
In my ViewController, I have a countdown Timer that fires every second and updates a label in the view with seconds -= 1 (for example). Everything's working perfectly as it should... no big deal.
But, I also have a UIButton in this same view that I can drag around. The problem occurs every time my Timer() fires (every second)... My PanGesture (dragging the UIButton) is cancelled and the UIButton is dropped.
Is there a certain property that I need to set on either the UIButton or the Timer to prevent this from happening?
Many thanks for your advice!
Even though I haven't been able to discover the reasoning behind it, here's what's I've learned:
If you are in the middle of a UIPanGesture (dragging a UIView around) and you try to update the text on a label (ie. update the UI), your UIPanGesture will cancel and .ended will be called. This was happening for me every second when my timer was updating the text on the label.
In my application, the label showing the countdown value is originally hidden (alpha = 0) in my view even when I am performing the text updates on it, and my UIButton to drag around is only visible when my countdown label isn't.
So my workaround was to only update the text on the label when it's actually visible. That way, my UIPanGesture isn't affected while I can drag it around, but when the label becomes visible and my UIButton isn't, then I can update the text on the label as desired without worrying about dragging the UIButton around.
I know it's a very custom fix that works for me, but if anyone knows what causes the issue, please do share :)

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.

UIButton states

I am making multiple custom buttons that look much like this:
It is a simple button with either the green or gray in the "indicator view". What I need some explanation for is: In interfacebuilder there are four states a button can have; Normal, Highlighted, Selected and Disabled. When I provide images for everything except disabled I thought that normal would be when no touches were made on the button, highlighted is while you hold your finger on it and selected would be when after you release finger.
However I do not think thats right now. I use the touch-up-inside event. Is it correct that I need to set the selected/highlighted etc property on the button?
Thank you for your time.
You might want to set to selected and not highlighted.
Highlight is darkening the button for a fraction when touching the UIButton. UIButton can modify your image automaticaly so usually you don't need to provide a highlight image.
Disabled is when it is disabled.
Selected is when it is selected. You can invert the select flag on touch up inside event to make a state button.
[button setSelected:![button isSelected]];
Yes, you need to respond to the touch up inside by setting the button to highlighted.
Btw, it's "disabled" not deselected, but it doesn't sound like you need that state.

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.

Display an image on screen after button is pressed in Cocoa

I was wondering what I should use to display an image on screen every time the user presses a button. I am using Objective-C/CocoaTouch on the iPod Touch. I would like to pull these images from an array I have set up and place them on the screen when the button is triggered. I feel dumb asking but any one that can point me in the right direction would be great. I think I could figure out the rest from there.
Thanks.
I'm sure there are many ways of doing this, but one way would be to define a UIImageView on the screen, and make it hidden. When the user presses the button, you can set the source of the UIImageView to the image from your array, and set the hidden property to NO.
I think it's as simple as something like this:
[myImageView setImage:[imageArray objectAtIndex:theIndex]];
In your button's action method. You can set theIndex before or afterwards to get a different image from your array every time the action method gets called.