使用 Storyboard 检测 WatchKit 上所有四个方向的滑动
我正在尝试检测 AppleWatch 上所有四个方向的滑动.但是我不清楚如何为我插入故事板的 WKSwipeGestureRecognizer 的方向分配许多值.我试过:
I am trying to detect the swipes on the AppleWatch on all four directions. Yet I am not clear how to assign many values to the direction of the WKSwipeGestureRecognizer I inserted in the Storyboard. I tried with:
swiper.direction = [.right , .left , .up , .down]
有趣的是,编译器接受了这一点,与使用按位 | 不同,但是,使用此配置,功能:
that was funnily accepted by the compiler, differently of using the bitwise |, but, using this configuration, function:
@IBAction func swipe(_ sender: Any) {
switch swiper.direction {
case WKSwipeGestureRecognizerDirection.right:
print("Swiped right")
case WKSwipeGestureRecognizerDirection.down:
print("Swiped down")
case WKSwipeGestureRecognizerDirection.left:
print("Swiped left")
case WKSwipeGestureRecognizerDirection.up:
print("Swiped up")
default:
break
}
}
很少被调用,当它是 swiper.direction 时总是 .right.
is very seldom called and when it is the swiper.direction is invariably .right.
Apple 在以下方面非常神秘:https://developer.apple.com/reference/watchkit/wkswipegesturerecognizer/1650828-direction说:
Apple is very cryptic at: https://developer.apple.com/reference/watchkit/wkswipegesturerecognizer/1650828-direction by saying:
"这个属性的默认值是对的,可以指定更多比一个方向跟踪多个方向的滑动相同的手势识别器."
"The default value of this property is right. You may specify more than one direction to track swipes in multiple directions with the same gesture recognizer."
没有透露如何.
1) 从右侧的 Utilities 框中选择 WKSwipeGestureRecognizer
1) Select WKSwipeGestureRecognizer from Utilities box on the right side
2) 从属性检查器中,选中延迟触摸开始"并选择您需要的滑动"类型
2) From the attributes inspectors, check "Delays touches began" and select "Swipe" type that you need
-
3) Ctrl + 将 IBAction 拖到你的 viewController 并实现功能
3) Ctrl + drag IBAction to your viewController and implement function