Swift iOS sometimes not detecting touches - ios

I am creating an iOS keyboard extension with UIInputViewController, and using UIButton for each key. I have two issues:
Sometimes buttons don't detect touches.
Touches at the edges of the screen are recognized with a delay.
I assume first one happens when I type too fast, or when touch area is too big (bigger than the button). I tried using different methods of detecting touch (button target, touchesBegan, touchesEnded, UITapGestureRecognizer) and all have the same issue.
I assume second happens because of the iOS edge false touch rejection. I tried changing preferredScreenEdgesDeferringSystemGestures but it had no effect.
Please help.

Turns out I was playing a quick animation on the UIButton press, so I had to set .allowUserInteraction in the animation options.
I used UILongPressGestureRecognizer with minimumPressDuration = 0 which, for some reason, is always instant even at the edges.

Related

How to delay start touch event of a UISlider in iOS?

In my application, there is a UISlider which is only slidable after a long-press (let's say, after 0.1 seconds of the press). To prevent from the slider to interact immediately, I set mySlider.isUserInteractionEnabled = false as default. However, since .isUserInteractionEnabled will disable all the functions, including the long-press gesture, I use another transparent UIButton of the same size as the slider on the top of it to detect long-presses, and set mySlider.isUserInteractionEnabled = true in the callback action. This works fine, except that the UIButton must be released first and then the user tap the slider again to slide it instead of drag the slider directly within the same touch. This make the slider very inconvenient to use. Therefore, I would like to know if either:
There is a better way other than mine to make UISlider start sliding only after certain delay? I'm quite surprised that I couldn't find any solution on this. I thought this would be a common need for a slider to be locked at a certain point.
or
Following my solution, is there any way that I could start dragging the slider thumb within the same touch?
Thanks a lot for any kind of answer.
Added: My solution looks like the following: in the TableViewCell, there is a label, a slider, and an invisible button with the same size as the slider (The grey background area). I set slider attribute isUserInteractionEnabled == false, and then add gesture recogniser to manipulate the slider (such as single tap, double tap for my custom functions, and long-press gesture which enable the slider). After the button is long-pressed, the isUserInteractionEnabled of the slider will set to true until the thumb goes to the new value.
Basically I only want to disable the slider to respond too quickly for short taps, the other features of the slider would remain the same. I suppose there may be better ways to achieve this, the so far using an transparent button is the only way I could think of.
You can disable user interaction on slider and then add a gesture recogniser over it (Not exactly sure about user interaction though). Maybe do something like this
let longPress = UILongPressGestureRecognizer(target: self.slider, action: Selector("longPressAndSlide:"))
longPress.minimumPressDuration = 0
self.addGestureRecognizer(longPress)
In longPressAndSlide you can calculate the direction and delta of movement and set the value of slider accordingly.
This has been answer multiple times actually. just a better search would help you more.
Update
Check answers under - How to enable "tap and slide" in a UISlider?
Latest Update
As the comments suggest, we can add a button on top of the slider and add longPress gesture over it. The handling can be done in the handler Action for the gesture.

How to make UIView stop receiving touch events?

I'm working on an app where the user is expected to rapidly touch and swipe across multiple UIViews, each of which is supposed to do an action once the user's finger has reached it. I've got a lot of views and so the typical thing to do, where I'd iterate over each view to see if a touch is inside of its bounds, is a no-go - there's just too much lag. Is there any other way to get touch events from one view to another (that is beside the first one)? I thought maybe there is some way to cancel the touch event, but I've searched and so far have come up empty.
One of the big problems I have is that if I implement my touch handling in my view controller, touchesBegan only fires for the first touch - if the user touches something and then, without moving the first finger, taps on something else, that tap is not recorded in either touchesBegan or touchesMoved. But if I implement my touch handling in the UIViews themselves, once a view registers a touch, if the user does not lift their finger up and moves it, the views around the first view do not register the touch. Only if the user lifts his finger and then puts it back down will the surrounding views register the touch.
So my question is, lets say I have two views side by side, my touch handling code is implemented in the views, and I put my finger down on view 1. I then slide my finger over to view 2 - what do I need to do to make view 2 register that touch, which started in view 1 and never "ended"?
Set userInteractionEnabled property of UIView to NO.
view.userInteractionEnabled = NO;
UIView has the following property:
#property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled
Ok, I figured out what was going on. Thing is, I have my views as subviews of a scrollview, which is itself a subview of my main view. With scrollEnabled = NO, I could touch my subviews - but apparently the scrollview was only forwarding me the initial touch event, and all subsequent touches were part of that initial event. Because of that, I had many weird problems such as touching two views one after the other, both would select and highlight, but if I took the first finger off the screen both views would de-select. This was not the desired behavior.
So what I did is I subclassed the scrollview and overrode the touch handling methods to send the events to its first responder, which is its superview, which is the view where I'm doing my touch handling. Now it works!

UISystemGateGestureRecognizer and delayed taps near bottom of screen

What are the standard UISystemGestureGateGestureRecognizers installed on the top level UIView of an iOS app for?
My app consists of two views - one fills the top half of the screen, the other is a custom keyboard and fills the bottom half. I found that taps on the space bar didn't always work and after some investigation found that the timing of tap events in the bottom 20 pixels or so was different to the rest of the view. For most of the view the period between touchesBegan/Ended was about 100ms, where as for the space bar it was 1-2ms. (My app is an emulator and this is too fast for it to detect the key press).
After some more digging I found the main UIView of the application (ie: my main view's superview) has 2 UISystemGestureGateGestureRecognizer's installed. By removing them in ViewDidAppear the bottom of the screen is no longer affected. (Presumably these are cancelling the touch press events to my keyboard hence the faster timing).
These system recognizers are present on at least iOS 5 through 7 and on both iPad and iPhone. I thought they may be related to swipe from top/bottom but this functionality still works with them removed.
So I have a fix, but I'd like to understand more about what's going on here - in particular what I might be breaking by removing these.
This delayed touches bothered me too.
Just as an addition to what's said before,
here's a simple fix:
override func viewDidAppear(_ animated: Bool) {
let window = view.window!
let gr0 = window.gestureRecognizers![0] as UIGestureRecognizer
let gr1 = window.gestureRecognizers![1] as UIGestureRecognizer
gr0.delaysTouchesBegan = false
gr1.delaysTouchesBegan = false
}
no need to remove those gesture recognizers.
just add this to the main view controller.
It appears that these recognizers are meant to prevent accidental touches near the top and bottom of the screen. They aren't configured with any targets, but can (like any UIResponder) absorb touches to prevent them from being passed up the responder chain.
Notes (tested on iOS 7.1):
Both gesture recognizers are always present in the key window.
I inspected both gestures' _targets ivar, and found they aren't configured with any targets at all. Swizzled out addTarget:action: to verify that targets weren't being added or removed on the fly.
delegate is always nil for both instances.
If you disable the gesture recognizers, they will re-enable themselves
The gesture that that doesn't delay content touches fires when you drag up from the bottom or drag down from the top. I couldn't trigger the instance that delays touches.

Capturing combined button press and pan gesture

I need to capture the following touch events on iOS and interpret them:
Finger A touch down within a UIButton (stays down)
Finger B performs a pan gesture elsewhere on the screen, providing continuous callbacks. (I plan to use a UIPanGestureRecognizer to implement this functionality).
Finger A touch up within the same UIButton
In essence, pressing and holding the UIButton puts the app into a special mode which lasts only as long as the button is held. Pan gestures performed during this time do different things to when the button is not pressed.
What I've tried so far:
hooking up the UIButton Touch Down and Touch Up Inside to IBActions in my UIViewController subclass. I've also
Problem encountered: the Touch Up Inside action is not called when another gesture happens on the screen from another finger while the button is pressed. This is because the original touch is cancelled.
attaching a UITapGestureRecognizer to the UIButton
Problem encountered: This only fires when the finger leaves the screen, hence it cannot be used to put the app into a special mode while the button is pressed.
I need to use a UIButton rather than a UIView so that the button has correct highlighting behaviour when pressed.
What is the best overall approach, given the problems I've encountered so far?
Use UILongPressGestureRecognizer with short minimumPressDuration on the "button"
http://developer.apple.com/library/ios/#documentation/uikit/reference/UILongPressGestureRecognizer_Class/Reference/Reference.html#//apple_ref/occ/cl/UILongPressGestureRecognizer
Important thing is to use gesture recognizer delegate to make sure that gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: returns YES for these recognizers. Then, when your UIPanGestureRecognizer calls the event handler, you can check the state of your UILongPressGestureRecognizer and if the button isn't pressed just ignore the pan gesture.

iPhone: Multiple touches on a UIButton are not recognized

I have a UIButton (a subclass of one, actually) that interacts with the user via the touchesbegan: and touchesmoved: functions.
What I would like is for the user to be able to press down the button, drag their finger away, and have a second finger touch the button (all while the first finger has never left the screen).
Problem is, the second touch event never calls touchesbegan: unless the first finger has been released.
Is there some way to override this, or am I trying to do the impossible?
Have you tried setting multipleTouchesEnabled to YES?
If the interactions are using touchesbegan: and touchesmoved: then use a UIView instead of a UIButton. A button is a UIControl, and the way to interact with UIControls is
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents.
I'm not sure this two ways of getting events mix well.

Resources