I am creating UIButtons programmatically and set their targets as well. Most of the time TouchDown-TouchDragInside-TouchUpInside chain seems to work properly but if I perform this chain of events fast (about 2-3 times per second) method bound to TouchUpInside is sometimes not firing.
From my understanding UIEvents will always fire even if they don't fire immediately. Is this a known issue that I can't seem to find anything about? What can I do about it, besides touching things slower?
I think you create button can switch and click.if so ,the two gesture will also do not handle well, you can add swipe gesture on the button , the it work well;Hope can help you.
Related
I have a super view that has a UITapGestureRecognizer on it. It allows touches within the view because there are clickable items within the view.
When these items are clicked on, I want to take a specific action, not the generic one that covers the entire superview. Unfortunately in my TouchDown event of my child control I don't know how to stop the event here. I know I could create a kludge flag, but this seems like the wrong way to go.
Any advice?
James
OK I got a solution. Totally my problem. I was playing around with trying to get all touches to work and at one point I had set cancelTouchesInView = true on the UITapGestureRecognizer superview. While this didn't stop the other touches from happening, for whatever reason the touches carried through to the superview as well. I understand that this explanation probably makes no sense, but that's what did it. Still trying to wrap my head around how iOS does touch.
I have a custom class here that consists of a UIButton inside of a UIView.
The goal of this class is to allow users to have a greater area (being the UIView) to select the button than just the buttons frame itself.
Now when a user taps on the view I want the buttons highlighted image to show... But the problem is, it does not.
I've read many possible solutions to this issue For Example:
Calling: [btnObject sendActionsForControlEvents:UIControlEventTouchUpInside]
This however did not change the buttons highlight.
I also tried just settings the button.highlighted = YES;
But that did not work either.
I have the images for different states properly setup (Normal and Highlighted), I'm sure of that.
I also have the gestureRecognizer working properly as the functionality is great except for the lack of highlight.
Does anybody know if I'm missing any thing special that needs to be done in order to pull off this seemingly very simple task? Surely it's been done many times.
Thank you
You were on the right track. -[UIButton setHighlighted:] is just a flag. What you need to do is call setNeedsDisplay on that button right after you change the highlighted property.
I solved my problem a little while ago and I'm not sure if Kevin Low's answer would've worked too, it very well might have.
But for some reason, a UITapGesture doesn't work well with highlighting buttons as a view transitions (That might be because I didn't call setNeedsDisplay). The gesture that ended up working was the UILongPressGesture with a 0.0 sec for minimum duration.
I'm making a calculator app and am supplying my own keypad with UIButtons. I have a delete key and everything works except that the user has to keep pressing the delete key over and over again if they want to delete all.
I was wondering if there is a way to delete everything when the button is held for more than 2 seconds.
The simplest way of implementing this would be attaching a long press gesture recognizer to your [Delete] button.
Xcode lets you attach long press gesture recognizer in the interface builder. Add it to your button, configure the duration of long press, and connect the handler to IBOutlet in the same way that you connect other UI events.
If you would rather do it in code, this answer shows you how.
Use your own timer function to handle this
-(IBAction)buttonHit {
//here start timer that fires for every 2 seconds and handle deletion method in that
}
-(IBAction)buttonReleased {
//Stop timer...
}
In your subclassed UIButton, you might want to watch the "touchesBegan: withEvent:" UIResponder method and if it passes a certain threshold of time, then start deleting like crazy (that is, until the "touchesEnded: withEvent" method gets called).
I have often noticed that UIView animations are often not smooth during the first becomeFirstResponder event when the keyboard appears for the first time. I am referring to animations that occur with the keyboard animation, such as manually scrolling the UIView to make a textField visible. The animation is always smooth after the first time it is executed.
Is there a technical reason why this would be the case? I was thinking that there might be some lazy loading or optimization that happens with UIView animations on the first run, then gets stored in cache for reuse. Are there lessons learned around this? If this is not clear to this audience, I can try to recreate the issue in a test project.
While this does not answer the question WHY this happens, it explains how to fix it.
Why are iOS animations slow the first time they are run?
Basically, you need to do animations on "DID" events rather than "WILL" or "SHOULD". The system performs it's animations during the "will/should" events, so apparently there is some colluding happening. This does not explain why the behavior is inconsistent between the first run and all other runs.
I thought, as may some of you, that I should put the animation in the "textFieldWillBeginEditing" because I wanted the animation to run concurrently with the keyboard animation. Luckily, putting the animation code in "DID" actually still ensures that the animation happens concurrently. Fantastic.
If anyone still has an explanation of the inconsistency between the first and latter runs, I'll still hold his question open and award you with an upvote and question answer. Thanks!
I've created a custom button because I wanted to be able to compose it of multiple different images. It actually subclasses UIControl instead of UIButton. This led to the issue of highlighting the images while it was being tapped.
So, I followed the advice in this question by creating a category on UIImage to emulate the highlighting of a standard UIButton: How to implement highlighting on UIImage like UIButton does when tapped?
I'm triggering the image tinting based on the UIControlEventTouchDown, UIControlEventTouchUpInside, and UIControlEventTouchUpOutside events.
This mostly works, except the timing is a bit off. With a standard UIButton, no matter how long or short the user taps down, the highlighting always happens, but with my implementation, if the user taps very quickly (which is most times), the highlighting doesn't happen.
I'm assuming this may be because the screen isn't getting re-drawn between the time the user taps down and up, but I'm not totally sure.
What I've tried:
Calling setNeedsDisplay right after swapping out the image - doesn't help
Over-riding touchesBegan and touchesEnded and putting the image swapping code there - doesn't help
Executing the image swapping code asynchronously inside a dispatch_async call - doesn't help
At this point, the only other thing I can think of is to set up a timer that manually fires off the image change after a slight delay if it detects that the user hasn't pressed down for longer than a certain time period.
This feels wrong, and I'm wondering if there's a better way to achieve this. Is there some other event I should be over-riding?