Gesture recognition in Apple Watch (WatchKit)

I'm looking for how to detect gestures in an Apple Watch app through the WatchKit SDK. In iOS, we can use the following code:

- (void)viewDidLoad { ... UISwipeGestureRecognizer *swipeRightBlack = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(slideToRightWithGestureRecognizer:)]; swipeRightBlack.direction = UISwipeGestureRecognizerDirectionRight; [self.viewBlack addGestureRecognizer:swipeRightBlack]; } 

... but this does not work in the Apple Watch simulator. Is there a way to override the default gesture actions using WatchKit or just recognize them when the OS receives them?

+5
source share
7 answers

watchOS 3 adds third-party support for WKGestureRecognizer .

Apple has also updated its WatchKit sample code example to include the WKInterfaceController gesture example, which demonstrates how to use various gestures on the Apple Watch.

Here's a code snippet showing two actions for gestures configured in the storyboard:

 class GestureDetailController : WKInterfaceController { @IBOutlet var tapGroup: WKInterfaceGroup! @IBOutlet var panGroup: WKInterfaceGroup! ... @IBAction func tapRecognized(_ sender: AnyObject) { tapGroup.setBackgroundColor(UIColor.green()) scheduleReset() } @IBAction func panRecognized(_ sender: AnyObject) { if let panGesture = sender as? WKPanGestureRecognizer { panGroup.setBackgroundColor(UIColor.green()) panLabel.setText("offset: \(NSStringFromCGPoint(panGesture.translationInObject()))") scheduleReset() } } 
+6
source

I wonder how this is achieved: in the demo video in Apple, the group in the line scrolls to the left and the buttons below are displayed.

enter image description here

+4
source

As far as I can tell, there is no way to do this. The whole Apple Watch is super limited when it comes to what you can do with it. I assume this makes sense, as it should be just peripheral for your main application, but it is too limited to my liking.

+3
source

Are you reading the iWatch manual? Apples say:

Gestures: The system processes all gestures on your behalf, using them to implement standard behavior:

  • Vertical scrolling scrolls the current screen.
  • Horizontal clicks display the previous or next page in the page-based interface.
  • Left to left, return to the parent interface controller.
  • Taps indicate choice or interaction. Cranes are processed by the system and communicated to your WatchKit extension action methods.

Apple Watch does not support multi-finger gestures, such as forceps.

... from Apple's iWatch User Guide. Therefore, I do not think that this “hack” gives you the same behavior as in iOS.

+3
source

Apple automatically provides user swipe gestures to move forward and backward through your controllers, and to scroll through the clock you’ve viewed. All this is done without the need for third-party code or gestures. All we have is to define segments and layout of objects in Interface Builder, and the OS will take care of the rest.

There is currently no direct access to WatchKit test files or other gestures or locations in WatchKit, and there is no reason to believe that this will change before the Watch release.

It is unclear whether Apple is prepared to include this in the future in the future, or whether they will never open it to developers, but they encourage developers to send promotions through the bug reporter . Be sure to indicate the desired use case and the problem you are trying to solve, as well as what the proposed solution offers, as there may be another tool that they can provide that will solve your problem, even if it happens in a different way than we expect.

+2
source

You cannot do this yet. An alternative now is to use the buttons inside the screen to perform a swipe action.

0
source

Im using Xcode 7.2, currently encoding watchos2, for my application, I want the button to execute the method when they double-tap for 0.5 seconds, if users only have one click, then this will not perform the action. For instance:

In my .h

 #import <WatchKit/WatchKit.h> #import <Foundation/Foundation.h> @interface InterfaceController : WKInterfaceController - (IBAction)btnActivate; @property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceButton *activateBtn; @end 

At the top of my .m file, I declared:

  int taps = 1; - (IBAction)btnActivate { taps = taps + 1; if (taps == 2){ [activateBtn setBackgroundColor:[UIColor grayColor]]; [self performSelector:@selector(tapOne:) withObject:self afterDelay:0.5]; } else if (taps == 3){ [self performSelector:@selector(tapTwo:) withObject:self afterDelay:0]; [activateBtn setEnabled:NO]; [activateBtn setBackgroundColor:[UIColor redColor]]; } else { //Do other things here if you want } } - (void) tapOne: (id) sender{ if (taps == 2){ [self performSelector:@selector(afterTapDone:) withObject:self afterDelay:1]; } else { } } - (void) tapTwo: (id) sender { //Perform button actions here when user double taps [self performSelector:@selector(afterTapDone:) withObject:self afterDelay:10]; } - (void) afterTapDone: (id) sender { taps = 1; [activateBtn setEnabled:YES]; [activateBtn setBackgroundColor:[UIColor colorWithRed:0/255.0f green:174/255.0f blue:239/255.0f alpha:1.0f]]; [activateBtn setTitle:@"Activate"]; } 

Of course, you can configure afterDelays, etc., and clear the code, but this is how I was able to get double taps working

0
source

Source: https://habr.com/ru/post/1209337/


All Articles