官网说明:https://developer.apple.com/design/human-interface-guidelines/watchos/interaction/digital-crown/

有时,我们希望二次开发Digital Crown,比如点击Digital Crown时,App响应一些动作,但是不幸的是,这个手势已经是IOS系统占用的手势了,没法进行自定义开发。但是我们可以捕捉到Digital Crown旋转的数值,从而做一些事情。
下面是具体的Demo:
https://dev.to/no2s14/how-to-use-wkcrowndelegate-in-watchos-development-1lj9
- UIkit的情况
import WatchKit
import Foundation
class InterfaceController: WKInterfaceController {
override func awake(withContext context: Any?) {
// Configure interface objects here.
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
}
override func didAppear() {
super.didAppear()
crownSequencer.delegate = self
crownSequencer.focus()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
}
}
extension InterfaceController : WKCrownDelegate {
func crownDidRotate(_ crownSequencer: WKCrownSequencer?, rotationalDelta: Double) {
print(rotationalDelta)
}
}

- SwiftUI的情况
SwiftUI实现起来就比较简单了
import SwiftUI
struct ContentView: View {
@State var rotationValue = 0.0
var body: some View {
VStack {
Text(("\(self.rotationValue)"))
.padding()
// 这里把表冠旋转的数值绑定到rotationValue
.focusable(true)
// 这里把表冠旋转的数值绑定到rotationValue
.digitalCrownRotation($rotationValue)
// 这里监听到rotationValue的变化,可以做一些事情
.onChange(of: rotationValue) {
value in
// 打印rotationValue的值
print(value)
}
}
}
}
