UIView: Inheriting Touch - ios

Example 1:
When invoking 3D Touch on app icon, you are able to make selections without lifting the finger up.
Example 2
Long pressing on a keyboard key allowing you to drag in to different selections without lifting finger up.
If the app icon is the first view and the pop up is the second view, how can I transfer touch down from first to second view?

Normally, a view loses control of the touches when the fingers leave its area. But if you set isMultipleTouchEnabled to true, it will keep control over touches if the finger leave its area. If you use a button or another UIControl you can assign actions to touchDragExit, touchUpOutside or touchDragOutside etc. to handle events outside of the control.

Related

React Native press and hold, drag finger to another touchable and capture touch by that view

In my React Native app I'm trying to have a button that the user can long press, and without lifting their finger, able to interact with another view. Here is roughly what I want:
Think of it like how 3D touch/long press worked prior to iOS 13/14 (depending on place in system and device): user either 3D touched or long pressed a button, for example an app icon, and a contextual menu popped up. Then, users could, without lifting the finger, hover onto one of the buttons and release their finger, triggering the button tap.
I have complete control over my buttons, touchables, and views (even the tab bar is custom, as opposed to the illustrations I made above).
How can I achieve this? (I'm on React Native 0.63)
There may be a better solution to this but off the top of my head I would use the Gesture Responder System
https://reactnative.dev/docs/gesture-responder-system
You can have a one container view that wraps tab bar and buttons. Then listen to the onResponderMove event to decide when these buttons should appear. This may happen for example when the locationY exceeds some value.
You can also use the onResponderRelease event (again with the help of locationX and locationY parameters) to determine if the finger was released above the button.

iOS Scrubbable Buttons

On the keyboard and in the native calculator app on iOS, it's possible to put your finger down on one button, like '0', and then move your finger to the another button, like '1', release your finger, and have it enter '1'. On the calculator it darkens the button under your finger.
If you start pressing a button, drag your finger outside of the buttons, and then move it back in, it'll continue to highlight the buttons under your finger. However, if you don't start on a button—like you start dragging from the calculator results label—and drag onto the buttons, the buttons do not highlight.
What's the best approach to mimic the calculator's behaviour for buttons? I'm mostly looking for code structuring guidance rather than code examples here!
It seems I won't be encapsulate each button in its own view class, but I'll have to have a Keyboard that handles all the touches, and manually draws the buttons.
I think the easiest way is to add handling for touch control events:
UIControlEventTouchDragInside
UIControlEventTouchDragOutside
UIControlEventTouchDragEnter
And link all the components with some kind of processing logic. Initial control event location and so on.

Fire and cancel touch events manually

I have a question about programming my own third-party keyboard for iOS8. My keyboard already looks pretty good, but some functionalities are missing. If you look at the standard keyboard on an iPhone you can press any button and if you swipe your finger to another button, the touch event of the first button gets cancelled and your second button is "active". So e.g. if I press the button "E" and swipe my finger to "R" and release my finger, the letter "R" is the selected one. But I don't know how to implement this.
Now in my app when I press a button and swipe my finger around, this button never gets "released". Seems like I'm stuck on that button as long is I have my finger put on the display.
I think I need these touch events:
TouchUpInside: when the user taps a button, and releases it inside the buttons' frame, this event gets fired (that's when I want to write a letter)
TouchDragInside: That's the event when I already have my finger on the display and swipe my finger "inside" the buttons' frame.
TouchDragOutside: Same as above, just swiping outside the buttons' frame.
But here's the problem: TouchDragInside just get's fired for the button I tap. So when TouchDragOutside gets fired, I have to "release" the button and make the button active where my finger is at the moment.
I hope you understand my question, if you need some further information or some details just let me know.
You could consider not using UIControl at all.
Just use a transparent overlay on top of all the keys so that you can manually deal with which key the message should be dispatched towards.

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!

How to detect "Drag Enter" on UIScrollView

Imagine a UIButton and a user who taps somewhere OUTSIDE the button and then slides onto it. If I want the button to detect such touches I can register it for any "Touch Drag Enter" events.
My question: is there some way to achieve the same for a UIScrollView?
I.e., I tap somewhere outside the scrollview, drag my finger onto the scrollview and as soon as I enter the scrollviews frame it starts panning? (Because by default it doesn't, I have to start my touch INSIDE the scrollview in order to pan)
If you want to do this, you will have to do a custom implementation using -touchesBegan, -touchesMoved, and -touchesEnded
The documentation for the UIResponder class (which all ViewControllers inherit from) that allows you to do this is here.
Hopefully you can figure out what to do from here. :)
As an extra hint, you will also most likely need to use this function
bool contains = CGRectContainsPoint(CGRect rect, CGPoint point);
Imagine a UIButton and a user who taps somewhere OUTSIDE the button and then slides onto it. If I want the button to detect such touches I can register it for any "Touch Drag Enter" events
Actually, no you can't. A UIControl will not get any of the control events unless the touch started in the control.
And that's for the same reason that you are seeing a similar effect with the scroll view. This is the entire basis of touch delivery on iOS. A touch "belongs" to the view that was initially touched.
After all, the runtime cannot possibly know that you are going to drag into the scroll view...

Resources