How to respond to touches when the view is already pressed? - ios

I have two views in a ViewController that perform specific actions when touched down. If I keep one of them pressed with one finger and touch the same view with another finger, nothing happens. The "ok" test below doesn't appear.
I override the method touchesBegan to perform the actions:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("ok")
if let touch = touches.first{
let viewTag = touch.view!.tag
if viewTag == 101 {
// my action for view 1
} else if viewTag == 102 {
// my action for view 2
}
}
super.touchesBegan(touches, withEvent: event)
}
Edit
I'm already using multipleTouchEnabled = true

You have to enable multiple touches on your view:
self.view.multipleTouchEnabled = true;

From Documentation:
multipleTouchEnabled
A Boolean value that indicates whether the receiver handles
multi-touch events.
When set to YES, the receiver receives all touches associated with a
multi-touch sequence. When set to NO, the receiver receives only the
first touch event in a multi-touch sequence. The default value of
this property is NO.
Other views in the same window can still receive touch events when
this property is NO. If you want this view to handle multi-touch
events exclusively, set the values of both this property and the
exclusiveTouch property to YES.

The multipleTouchEnabled = true must be set in both views, not only the main one.
Put this code in the viewDidLoad:
let tags = [101, 102]
for v in view.subviews {
if tags.contains(v.tag) {
v.multipleTouchEnabled = true
}
}

Related

Call hitTest inside of touchesMoved

I have an UIView which is on top of all other views and has overridden hitTest() method which always return itself:
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
return self
}
Then, when I make some operations using points from touchesBegan(), I need to pass hitTest() to the views below of the our UIView:
override public func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
// Do some operations
// ...
// ...
// ...
// pass touch event handling to views below or change hitTest()
}
So basically, on the top UIView I'm overriding touchesBegan(), touchesMoved() and touchesEnded() methods. Then I need to handle touches, perform some operations and then, if needed, to pass to views below. Is it possible?
It is probably simpler and better to solve your problem differently.
UIKit delivers a touch event by sending it to the window (the root of the view hierarchy) in a sendEvent(_:) message. The window's sendEvent(_:) method is responsible for finding the gesture recognizers interested in the touches, and sending the appropriate touchesBegan, touchesMoved, etc. messages to the recognizers and/or the hit view.
This means that you can subclass UIWindow and override sendEvent(_:) to get a look at every touch event in the window, before the event reaches any gesture recognizers or views, without overriding any view's hitTest(_:with:) method. Then you pass the event along to super.sendEvent(event) for normal routing.
Example:
class MyWindow: UIWindow {
override func sendEvent(_ event: UIEvent) {
if event.type == .touches {
if let count = event.allTouches?.filter({ $0.phase == .began }).count, count > 0 {
print("window found \(count) touches began")
}
if let count = event.allTouches?.filter({ $0.phase == .moved }).count, count > 0 {
print("window found \(count) touches moved")
}
if let count = event.allTouches?.filter({ $0.phase == .ended }).count, count > 0 {
print("window found \(count) touches ended")
}
if let count = event.allTouches?.filter({ $0.phase == .cancelled }).count, count > 0 {
print("window found \(count) touches cancelled")
}
}
super.sendEvent(event)
}
}
You can use this window subclass in your app by initializing your app delegate's window outlet to an instance of it, like this:
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow? = MyWindow()
// other app delegate members...
}
Note that UIKit uses hitTest(_:with:) to set the view property of a touch when the touch begins, before it delivers the touch-began event to the window. UIKit also sets each touch's gestureRecognizers property to the set of recognizers that might want the touch (recognizer state .possible) or are actively using the touch (states began, changed, ended, cancelled) before passing the event to the window's sendEvent(_:). So your sendEvent(_:) override can look at each touch's view property if it needs to know where the touch is going.

Why do we need check if a custom gesture recognizer still tracks a UITouch object in touchesBegin(_:with:)

Here is a copy of the Listing 3 of Section "Processing Touch Events" of the documentation page called Implementing a Continuous Gesture Recognizers
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
if touches.count != 1 {
self.state = .failed
}
// Capture the first touch and store some information about it.
if self.trackedTouch == nil { // <-- Q: Should it not be nil anyway?
if let firstTouch = touches.first {
self.trackedTouch = firstTouch
self.addSample(for: firstTouch)
state = .began
}
} else {
// Ignore all but the first touch.
for touch in touches { // <-- Q: We have already set the
if touch != self.trackedTouch { // <-- state to .failed, why do
self.ignore(touch, for: event) // <-- we care about the other
} // <-- touches?
}
}
}
Here, I don't understand why we need to check whether self.trackedTouch is nil or not. As far as I know UIKit must call reset() every time we set the gesture recognizer state to .recognized.
Additionally, in the else clause, it ignores all the touches except the first one. However, at the top of the function body, we set the state to .failed anyway; why do we care about the rest of the touches?
Am I missing something?
I don't think you're missing anything. I agree with you on both points.
Out of interest, what custom behaviour do you need that is not covered by the existing UIGestureRecognizer subclasses?

Touch events are delayed near left screen edge on iOS 9 only. How to fix it?

I am developing a keybaord extension for iOS. On iOS 9 the keys react imediatelly except for keys along left edge of the keyboard. Those react with around 0.2 second delay. The reason is that the touches are simply delivered with this delay to the UIView that is root view of my keyboard. On iOS 8 there is no such delay.
My guess is that this delay is cause by some logic that is supposed to recognize gesture for opening "running apps screen". That is fine but the delay on a keyboard is unacceptable. Is there any way how to get those events without delay? Perhaps just setting delaysTouchesBegan to false on some UIGestureRecognizer?
This is for anyone using later versions of iOS (this is working on iOS 9 and 10 for me). My issue was caused by the swipe to go back gesture interfering with my touchesBegan method by preventing it from firing on the very left edge of the screen until either the touch was ended, or the system recognised the movement to not be that of the swipe to go back gesture.
In your viewDidLoad function in your controller, simply put:
self.navigationController?.interactivePopGestureRecognizer?.delaysTouchesBegan = false
The official solution since iOS11 is overriding preferredScreenEdgesDeferringSystemGestures of your UIInputViewController.
https://developer.apple.com/documentation/uikit/uiviewcontroller/2887512-preferredscreenedgesdeferringsys
However, it doesn't seem to work on iOS 13 at least. As far as I understand, that happens due to preferredScreenEdgesDeferringSystemGestures not working properly when overridden inside UIInputViewController, at least on iOS 13.
When you override this property in a regular view controller, it works as expected:
override var preferredScreenEdgesDeferringSystemGestures: UIRectEdge {
return [.left, .bottom, .right]
}
That' not the case for UIInputViewController, though.
UPD: It appears, gesture recognizers will still get .began state update, without the delay. So, instead of following the rather messy solution below, you can add a custom gesture recognizer to handle touch events.
You can quickly test this adding UILongPressGestureRecognizer with minimumPressDuration = 0 to your control view.
Another solution:
My original workaround was calling touch down effects inside hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?, which is called even when the touches are delayed for the view.
You have to ignore the "real" touch down event, when it fires about 0.4s later or simultaneously with touch up inside event. Also, it's probably better to apply this hack only in case the tested point is inside ~20pt lateral margins.
So for example, for a view with equal to screen width, the implementation may look like:
let edgeProtectedZoneWidth: CGFloat = 20
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let result = super.hitTest(point, with: event)
guard result == self else {
return result
}
if point.x < edgeProtectedZoneWidth || point.x > bounds.width-edgeProtectedZoneWidth
{
if !alreadyTriggeredFocus {
isHighlighted = true
}
triggerFocus()
}
return result
}
private var alreadyTriggeredFocus: Bool = false
#objc override func triggerFocus() {
guard !alreadyTriggeredFocus else { return }
super.triggerFocus()
alreadyTriggeredFocus = true
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesCancelled(touches, with: event)
alreadyTriggeredFocus = false
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
alreadyTriggeredFocus = false
}
...where triggerFocus() is the method you call on touch down event. Alternatively, you may override touchesBegan(_:with:).
If you have access to the view's window property, you can access these system gesture recognizers and set delaysTouchesBegan to false.
Here's a sample code in swift that does that
if let window = view.window,
let recognizers = window.gestureRecognizers {
recognizers.forEach { r in
// add condition here to only affect recognizers that you need to
r.delaysTouchesBegan = false
}
}
Also relevant: UISystemGateGestureRecognizer and delayed taps near bottom of screen

UIControl endTrackingWithTouch not called

I have a view with a tap gesture recognizer. A subview of this view is an instance of my custom class, which inherits from UIControl. I am having an issue where the UIControl subclass will sometimes allow touch events to pass through to the parent view when it shouldn't.
Within the UIControl subclass, I have overridden these functions (code is in Swift)
override func beginTrackingWithTouch(touch: UITouch, withEvent event: UIEvent) -> Bool
{
return true
}
override func continueTrackingWithTouch(touch: UITouch, withEvent event: UIEvent) -> Bool
{
// The code here moves this UIControl so its center is at the touchpoint
return true
}
override func endTrackingWithTouch(touch: UITouch,withEvent event: UIEvent)
{
// Something important happens here!
}
This system works just fine if the user touches down within the UIControl, drags the control around in both X and Y directions, and then lifts off the screen. In this case, all three of these functions are called, and the "something important" happens.
However, if the user touches down with the UIControl, drags the control around only in the X direction, and then lifts off the screen, we have a problem. The first two functions are called, but when the touchpoint lifts off the screen, the tap gesture recognizer is called, and endTrackingWithTouch is not called.
How do I make sure that endTrackingWithTouch is always called?
I fixed this in a way that I consider to be a hack, but there's really no alternative, given how UIGestureRecognizer works.
What was happening was that the tap gesture recognizer was canceling the control's tracking and registering a tap gesture. This was because when I was dragging horizontally, I just happened to be dragging short distances, which gets interpreted as a tap gesture.
The tap gesture recognizer must be disabled while the UIControl is tracking:
override func beginTrackingWithTouch(touch: UITouch, withEvent event: UIEvent) -> Bool
{
pointerToSuperview.pauseGestureRecognizer()
return true
}
override func continueTrackingWithTouch(touch: UITouch, withEvent event: UIEvent) -> Bool
{
// The code here moves this UIControl so its center is at the touchpoint
return true
}
override func endTrackingWithTouch(touch: UITouch,withEvent event: UIEvent)
{
// Something important happens here!
pointerToSuperview.resumeGestureRecognizer()
}
override func cancelTrackingWithEvent(event: UIEvent?)
{
pointerToSuperview.resumeGestureRecognizer()
}
In the superview's class:
pauseGestureRecognizer()
{
tapGestureRecognizer.enabled = false
}
resumeGestureRecognizer()
{
tapGestureRecognizer.enabled = true
}
This works because I'm not dealing with multitouch (it's OK for me not to receive tap touch events while tracking touches with the UIControl).
Ideally, the control shouldn't have to tell the view to pause the gesture recognizer - the gesture recognizer shouldn't be meddling with the control's touches to begin with! However, even setting the gesture recognizer's cancelsTouchesInView to false cannot prevent this.
There's a way to fix this that's nicely self-contained: instantiate your own TapGestureRecognizer and attach it to your custom control, e.g. in Objective-C,
_tapTest = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
_tapTest.numberOfTapsRequired = 1;
[self addGestureRecognizer:_tapTest];
and then implement the tapped action handler to process the tap:
- (void)tapped:(UITapGestureRecognizer *)recognizer {...}
In my case, I handle tapped the same as endTrackingWithTouch:withEvent:; your mileage may vary.
This way, you get the tap before any superview can snatch it, and you don't have to worry about the view hierarchy behind your control.
When a UIControl is moved while tracking touches, it might cancel its tracking. Try overriding cancelTrackingWithEvent and see if this is the case. If you do see the cancel, you're going to have to track your touches in an unmoving view somewhere in the parent hierarchy of this control.
I know this is old, but I run into the same problem, check if one of your superviews has gesture recogniser, and deactivate them when you need to use the UIControl.
I actually ended changed the superview of the UIControl to the main window to avoid this conflicts (Because it was in a popup).

Possible to discard touch events?

I have a iOS game and during loading screens any touches the user does seem to be buffered up, so once the loading it done (it can take a few seconds), I get the touch events.
Is there way for me to discard all touches?
Ok this is a really old question but for anyone stumbling upon this can simply follow either of the below two approaches:
Approach 1.
DiscardTouchView.addGestureRecognizer(UITapGestureRecognizer())
Basically adding an empty gesture. So on tapping that view nothing happens.
Approach 2.
In this case you don't add empty gesture recognizer to the view. In case DiscardTouchView is a subview of SomeParentView which has a UIGestureRecognizer object, you can get that object and ignore it.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let tappedView = touches.first?.view else { return }
if(tappedView == DiscardTouchView) {
guard let recognizer = touches.first?.gestureRecognizers?.first else { return }
recognizer.ignore(touches.first!, for: event!)
}
}
Setting userInteractionEnabled to false actually passes the touch event from subview to superview. It doesn't discard the touch event.
Setting userInteractionEnabled to true for a simple UIView does the same.

Resources