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?
Related
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.
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.
I have a ScrollView which shows photos. When touching the screen, a UIToolBar pops up, with several UIBarButtonItem buttons, like Previous, Next, Play... If the users doesn't do anything for 5 seconds, the toolbar disappears again.
It's all very similar to the Apple Photos app.
Everything works as it should, but there's one thing I'm struggling with: I cannot get touches for if the user pressed the buttons, nor can I check on the highlight status of the button.
So there's a problem if the user keeps a button pressed for a few seconds... the program will assume nothing has happened, and remove the toolbar after the 5 seconds have passed.
The Photos app does not have this problem. Even better: when you keep the Next or Previous button pressed or longer than a second, it already executes the "action" associated with the UIBarButtonItem !
In short, I want to do one of these:
- to know whether a UIBarButtonItem is pressed
- to know whether a UIBarButtonItem is highlighted
- or else just know whether there's any press anywhere going on
You could try attaching a custom subclass of UIGestureRecognizer, which tells you when the users interacts with the view to the toolbar.
Alternatively, you could try subclassing UIToolbar and change its -[touchesBegan:withEvent:] and -[touchesEnded:withEvent:] methods to tell you when the users starts/stops interacting with anything on the toolbar.
The latter is probably simpler.
I have a row of say 10 UIButtons along the bottom of the iPhone screen. The user can TouchUpInside each of these. I'd also like to allow the user to drag their finger along the row, deselecting the previous button that was touched when the finger is over the next. This kind of behaviour is seen on iPhone's keyboard.
The obvious thing to try is UIControlEventDragOutside or UIControlEventDragExit to trigger the appropriate actions however these only work when the touch event is sufficiently far away from the button. As my buttons must sit flush against each other this is not good enough for me.
Suggestions?
I have a UIButton subview inside of a UITableViewCell.
When this button is touched, the user must hold the button for about a half second for the button's image to change to the UIControlStateHighlighted image.
This means that if the user just taps the button as is usually the case, the highlighted state is never shown.
Why does this occur and how can I fix it?
I just encountered this problem and saw that this issue hadn't been closed. After screwing around for a while I found a fix for it.
Now you can fix this by turning off delaysContentTouches or unchecking the "Delays content touches" box on the tableview.
The only negative side effect is that the user won't be able to tap down on a button and initiate a scrolling gesture. However, if the user tries to scroll starting from anywhere that doesn't itself accept touches, the behavior should be the same as before.
The problem is that your UIButton is inside a UITableView. This means that the table view has to determine whether your tap is going to be a swipe or if it's just a tap intended for the button. The table view has to delay sending a message to the UIButton until it knows that the user doesn't intend to swipe and therefore scroll the view instead of pressing the button.
If you don't need a table view, get rid of the UITableView.
Up for David Hodge's answer.
I just want to add a way to remove that "only negative side effect", already described by David: if you start scrolling inside a UIcontrol in a UIScrollView with delayContentTouches=NO, scrolling doesn't work.
SOLUTION
Subclass UIScrollView (or UITableView as the original question) and override:
-(BOOL) touchesShouldCancelInContentView:(UIView *)view {
return YES;
}
Your UIControls inside UIScrollView/UITableView will change their state immediately on tap and the scrollviews will be able to scroll even if the touch starts on some UIControl. Works like a charm.
I just change the image from within the target action method:
[sender setBackgroundImage:[UIImage imageNamed:#"highlighted-image.png"] forState:UIControlStateNormal];
It changes the background image instantly.
Edit: completely re-written following a misunderstanding of the question
One way of thinking of a UIButton is as a shorthand way of setting up an area of the screen that can respond to various instantaneous touch events the response it makes is defined by UIControl's Target-Action system for delivering messages to other objects.
UIControlEventTouchDown sounds like the one you need to respond to. It will be triggered as soon as someone touches inside your button - this is what the "Contact Info" button in SMS does.
UIButton* myButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect];
// SEt up title, frame etc
[myButton addTarget:self action:#selector(myButtonWasPressed) forControlEvents: UIControlEventTouchDown];
[myMainView addSubView:myButton];
Will send a -(void)myButtonWasPressed message to the object this code runs from (ideally you view controller). In myButtonWasPressed you can then add a new view or take any action you like. The SMS app pushes a view controller to display the contact info using a navigation controller.
If this still doesn't solve your problem, you're going to have to post some code in order to get more insight into what's going wrong.