Apple Watch (WatchKit) push action

断了今生、忘了曾经 提交于 2020-01-04 17:37:13

问题


I am developing an app for the Apple Watch and I would like to use the action demonstrated at the launch event where the user pushes into the screen (rather than a tap). Do you know what this is called and how I can access this?


回答1:


You can only show a menu on that action. You use addMenuItem methods on WKInterfaceController.




回答2:


The gesture you are referring to is called a Force Touch.

It is possible to use Force Touch as an input method in third party apps. You cannot register to receive notifications of Force Touch events however; there is nothing equivalent to a UIGestureRecogniser in WatchKit at present.

When you have a contextual menu in the current screen of your WatchKit app, it will automatically be activated by the OS when the user initiates a force touch. You can simulate this in the Apple Watch simulator by a click and hold with the mouse... the resulting animation will make it clear when a Force Touch even has been initiated, even on screens that do not have a contextual menu enabled.

To utilise this via Interface Builder, you simply:

  1. Drag a menu into the relevant Watch App scene in interface builder.
  2. Add between one and four menu items to the menu by dragging those into the menu.
  3. Set names and images for those menu items.
  4. Wire those menu items to IBActions in your WatchKit Extension.

Alternatively, you can set and clear menu items programatically from your WatchKit Extension, as outlined in the WatchKit API documentation. There are four relevant WKInterfaceController methods. In Swift:

func addMenuItemWithItemIcon(_ itemIcon: WKMenuItemIcon,
                   title title: String,
                  action action: Selector)

func addMenuItemWithImageNamed(_ imageName: String,
                     title title: String,
                    action action: Selector)

func addMenuItemWithImage(_ image: UIImage,
                title title: String,
               action action: Selector)

func clearAllMenuItems()

In Objective-C:

- (void)addMenuItemWithItemIcon:(WKMenuItemIcon)itemIcon
                      title:(NSString *)title
                     action:(SEL)action

- (void)addMenuItemWithImageNamed:(NSString *)imageName
                        title:(NSString *)title
                       action:(SEL)action

- (void)addMenuItemWithImage:(UIImage *)image
                   title:(NSString *)title
                  action:(SEL)action

- (void)clearAllMenuItems

Full information is in the API documentation for Configuring the Contextual Menu in WatchKit.

The first time you do create one of these menus, particularly if you do it in Interface Builder, it may well feel like you must have missed a step, since you did not have to connect the menu to something equivalent to a force touch gesture recogniser, but when you try it, you'll just find it works. It may well be that this will continue to be the only access third party developers have to force touch, even after we have the ability to make native apps for the Watch later in 2015.




回答3:


it is called a contextual menu. you can do that using addMenuItem or in storyboards you add a menu which comes with an item, and then add additional items.



来源:https://stackoverflow.com/questions/27160398/apple-watch-watchkit-push-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!