LongPressGestureRecognizer in watchkit - ios

I am trying to do some action when I long press the button in watchkit.
How can I use long press gesture in Watchkit for WKInterfaceButton?

So far WatchKit doesn't have gesture recognizers. The only way you can use the force touch (the long press in the watch) is using a context menu.

Apple introduced long press gesture recognizers in watchOS3, have a look at the official documentation.

Related

Is there any function for opening a preview (peek) with a long press gesture since 3D Touch won't be supported in iOS 13?

I want users to have the peek & pop functionality which was supported on iPhone 6S and newer but implemented with long press gestures since this is what Apple itself is implementing in iOS 13 by removing 3D Touch on the new iPhones. Are there already some functions and delegates available or is this something that will come with the new Xcode version?
Implementing a long press gesture isn't a problem but the delegates where pretty handy which Apple provided with 3D Touch.
I believe UIContextMenuInteraction is what you're looking for. Example tutorial here.
Using SwiftUI, you can also do a .contextMenu(menuItems:) to get a context menu using long press.
https://www.hackingwithswift.com/quick-start/swiftui/how-to-show-a-context-menu

How to childproof an iOS app by requiring "exit" button to be pushed multiple times before exit

I'm quite new to xCode and am trying to build a simple children's app. I have a young child and have experience with her playing with some apps where it's too easy to exit the screen - either with the main iPhone button or by swiping/pushing a button to exit the app. I know there are workarounds to this through the settings on iPhone, however I want to make my app so it is more childproof upfront with regards to exiting the app too easily.
My app is structured with a main menu for parents to access the settings/help and has a play button to enter the kids zone. Once the play button is pressed, a new view controller comes up which is where I'd like to add the code to press the exit button multiple times in a row to get back to the main menu and to disable the iPhone's main button from exiting the app.
If anyone has a suggestion on how to go about this I'd sure appreciate your help!
Cool concept! I'll suggest you use a UITapGestureRecognizer. Many subclasses of UIView accept gesture recognizers so you could simply do the following:
let tap = UITapGestureRecognizer(target: self, action: #selector(theMethodToExit))
tap.numberOfTapsRequired = 4
self.theTappingButton.addGestureRecognizer(tap)
where theMethodToExit is the method you will call to exit from the app and theTappingButton is the UIView or UIView subclass.
I'm not sure if you can attach a gesture recognizer to a UIButton, but it might be worth trying. If not, you can mimic the the behavior of a UIButton with a UIView.
Unfortunately you won't be able to disable the home button from being pressed, though you can provide a tutorial to parents for how to lock the app on the screen.

Xcode 7 UI Testing not recording swipes

I have a collection view in my app, which is inside a table view cell (yes I know it's weird), and I want to UI Test the scrolling of the collection view. I use the recording button for this, however Xcode identifies the swiping on my collection view as taps. If I manually change the generated UI Test code from [collectionview tap] to [collectionview scrollLeft], it works, but Xcode won't generate the code for swiping automatically.
What could be the problem?
Xcode only recognises a gesture as a swipe if your trajectory with the gesture is fast, straight and true to the (up/down/left/right) direction you are swiping in.
My guess is that this prevents recording drag or tap-and-hold gestures as swipes, since these are unsupported by the recording tool. If you were going for either of those, a tap gesture would be closer.
As a workaround, take note of where you expected a swipe and switch the gesture as you have been doing when your swipes aren't recorded.
I believe you should file a bug with Apple and include a sample project.
It may be hard for the recording system to differentiate between a tap, a long press, and a swipe. While I've seen the recording of tap events to be reliable, I find I'm manually typing any steps for swipes or typeText. Generally I use the UI test recording feature to help with identification of particular elements, which I then work with in code to specify the user interactions and asserts.
If you want to create a sample project on github or somewhere with your collectionView-inside-tableViewCell configuration, I'd be willing to take a look.
EDIT: After trying your example project, I was sometimes able to get Xcode to record swipeLeft and swipeRight events from the first cell. It's not the most elegant approach, but with the trackpad on my MacBook Air, I start a horizontal swipe with one finger without pressing the mouse button, and then press the button with another finger while the first finger is still swiping.
There were some instances when Xcode recorded this simply as a tap, and at least one instance where it recorded a twoFingerTap.

How do we use Watchkit Touch Events?

I wanna use touch events in my app. I know gesture recognisers can not be used in watchKit. Is it possible to use functions like touchesBegan, touchesMove etc ?
Apple Watch app uses WatchKit framework. UIKit events are not applicable here.
Alternate is to use forced touch event which triggers Context Menu (if available)
Instead of just tapping items on the screen, pressing the screen with a small amount of force activates the context menu (if any) associated with the current interface controller.
There is no such an api like "touchesBegan" or "touchesMove".
The only thing you can do to respond to a button event is to use IBAction.
A little late to the party, but it's possible to use SceneKit with WatchKit and SceneKit allows you to add gesture handlers. Please see the Apple example project here:
https://developer.apple.com/library/content/samplecode/WatchPuzzle/Introduction/Intro.html#//apple_ref/doc/uid/TP40017284-Intro-DontLinkElementID_2
Edit: Looks like Tap, Swipe, Long Press and Pan gesture handles can be added to any view added to the InterfaceController.

Disabling iOS multitask gesture recognizer during game play

I am developing a game where the user could draw using up to 5 fingers, 1 finger for each line.
When multitask gesture is enabled and the user try to draw using more than 4 fingers from right to left, the gesture recognizer from iOS on iPad switch to another application.
How can I programmatically disable this recognizer when the user enters on my application?
There is no API for that.
There might be a way to consume the gesture, so it is not forwarded to the system, but I think the order of the messaging chain is in the other way.
You can only ask your users to disable the Multitasking Gestures in the System Preferences, under the General section. Not really great, I know.

Resources