struct TapGesture (英文)
struct LongPressGesture (英文)
struct DragGesture (英文)
struct MagnificationGesture (英文)
struct RotationGesture (英文)
手势修饰符用于处理用户输入事件 (例如,触控) 处理所需的所有逻辑,并识别这类事件何时与已知的手势模式 (例如,长按或旋转) 匹配。在识别到模式时,SwiftUI 会运行一个回调,供你用来更新视图状态或执行操作。
你添加的每个手势都会应用于视图层次结构中的特定视图。若要识别特定视图上的手势事件,请创建并配置相关手势,然后使用 gesture(_:
(英文) 修饰符:
struct ShapeTapView: View {
var body: some View {
let tap = TapGesture()
.onEnded { _ in
print("View tapped!")
}
return Circle()
.fill(Color.blue)
.frame(width: 100, height: 100, alignment: .center)
.gesture(tap)
}
}
每当手势状态发生变化时,SwiftUI 都会根据你添加到手势修饰符的回调反馈给你的代码。手势修饰符提供三种方式来接收更新:updating(_:
(英文)、on
(英文) 和 on
(英文)。
若要在手势发生变化时更新视图,请将 Gesture
(英文) 属性添加到你的视图,并在 updating(_:
(英文) 回调中更新它。SwiftUI 会在识别到手势以及手势的值发生变化时立即调用更新回调。例如,SwiftUI 会在放大手势一开始便调用更新回调,然后在每次放大值发生变化时再次调用回调。SwiftUI 在用户结束或取消手势时不会调用更新回调。这时,手势状态属性会自动将其状态重置为初始值。
例如,若要创建一个在用户执行长按时改变颜色的视图,请添加一个手势状态属性并在更新回调中更新它。
struct CounterView: View {
@GestureState var isDetectingLongPress = false
var body: some View {
let press = LongPressGesture(minimumDuration: 1)
.updating($isDetectingLongPress) { currentState, gestureState, transaction in
gestureState = currentState
}
return Circle()
.fill(isDetectingLongPress ? Color.yellow : Color.green)
.frame(width: 100, height: 100, alignment: .center)
.gesture(press)
}
}
若要跟踪手势的变化且在手势结束后不应重置,请使用 on
(英文) 回调。例如,若要对你的 App 识别长按的次数进行计数,请添加一个 on
(英文) 回调并递增计数器值。
struct CounterView: View {
@GestureState var isDetectingLongPress = false
@State var totalNumberOfTaps = 0
var body: some View {
let press = LongPressGesture(minimumDuration: 1)
.updating($isDetectingLongPress) { currentState, gestureState, transaction in
gestureState = currentState
}.onChanged { _ in
self.totalNumberOfTaps += 1
}
return VStack {
Text("\(totalNumberOfTaps)")
.font(.largeTitle)
Circle()
.fill(isDetectingLongPress ? Color.yellow : Color.green)
.frame(width: 100, height: 100, alignment: .center)
.gesture(press)
}
}
}
若要识别手势在何时成功完成并检索手势的最终值,请在回调中使用 on
(英文) 函数来更新你的 App 的状态。SwiftUI 仅在手势成功时才会调用 on
(英文) 回调。例如,在一个 Long
(英文) 期间,如果用户在经过 minimum
(英文) 秒之前停止触控视图,或者他们的手指移动超过了 maximum
(英文) 点,则 SwiftUI 不会调用 on
(英文) 回调。
例如,若要在用户完成长按后停止长按尝试计数,请添加一个 on
(英文) 回调并有条件地应用手势修饰符。
struct CounterView: View {
@GestureState var isDetectingLongPress = false
@State var totalNumberOfTaps = 0
@State var doneCounting = false
var body: some View {
let press = LongPressGesture(minimumDuration: 1)
.updating($isDetectingLongPress) { currentState, gestureState, transaction in
gestureState = currentState
}.onChanged { _ in
self.totalNumberOfTaps += 1
}
.onEnded { _ in
self.doneCounting = true
}
return VStack {
Text("\(totalNumberOfTaps)")
.font(.largeTitle)
Circle()
.fill(doneCounting ? Color.red : isDetectingLongPress ? Color.yellow : Color.green)
.frame(width: 100, height: 100, alignment: .center)
.gesture(doneCounting ? nil : press)
}
}
}
struct TapGesture (英文)
struct LongPressGesture (英文)
struct DragGesture (英文)
struct MagnificationGesture (英文)
struct RotationGesture (英文)
葡萄糖高是什么意思 | 什么是房颤 | 什么是冰丝面料 | 浪凡算是什么档次的 | 结节性甲状腺肿是什么意思 |
昂热为什么认识路鸣泽 | 皮肤自愈能力差缺什么 | 侄子是什么关系 | 难缠是什么意思 | 念五行属什么 |
母鸡什么意思 | 黄腔是什么意思 | 属兔是什么命 | 过敏性紫癜什么症状 | 陈皮和橘子皮有什么区别 |
花嫁是什么意思 | 女性去泰国要注意什么 | 年轻人头晕是什么原因 | pcm是什么意思 | 舌苔白厚吃什么药 |
择日不如撞日什么意思hcv8jop8ns7r.cn | 女性什么时候退休hcv7jop6ns1r.cn | 红底白杠是什么标志hcv8jop3ns8r.cn | 开什么店好hcv8jop2ns7r.cn | 北阳台适合种什么植物hcv8jop5ns8r.cn |
什么是双高hcv9jop2ns7r.cn | 柿子不能和什么食物一起吃hcv9jop2ns1r.cn | 腹黑是什么意思tiangongnft.com | 8月13号什么星座hcv9jop1ns7r.cn | kick什么意思hcv7jop5ns3r.cn |
慧外秀中什么意思hcv7jop9ns1r.cn | 一什么小狗hcv9jop2ns5r.cn | 黄花菜都凉了什么意思zhongyiyatai.com | 氧气湿化瓶里加什么水hcv8jop4ns8r.cn | 荷兰猪是什么动物hcv8jop9ns8r.cn |
紫菜和海带有什么区别hcv9jop2ns3r.cn | 肾积水是什么原因引起的hcv9jop8ns3r.cn | 颈椎病用什么药最好bysq.com | 小狗吐白沫不吃东西没精神吃什么药hcv9jop1ns9r.cn | 点状强回声是什么意思hcv8jop5ns3r.cn |