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 (英文)
果是什么结构的字hcv8jop9ns2r.cn | 什么是热性水果hcv9jop5ns0r.cn | 鼻窦粘膜增厚什么意思hcv9jop4ns4r.cn | cp是什么意思hcv9jop1ns8r.cn | 额头长痘什么原因hcv9jop5ns1r.cn |
厌氧菌是什么意思hcv9jop3ns1r.cn | 欧阳修是什么居士hcv8jop6ns9r.cn | 海鸥手表是什么档次hcv9jop4ns6r.cn | 血糖高喝什么茶hcv8jop3ns5r.cn | 脂肪肝吃什么hcv9jop2ns3r.cn |
吃什么增强免疫力hcv8jop4ns7r.cn | 5点到7点是什么时辰hcv8jop6ns8r.cn | 什么什么大地hcv7jop4ns8r.cn | 12月8日是什么星座hcv9jop1ns9r.cn | 小孩吃牛肉有什么好处hcv9jop6ns2r.cn |
口红用什么能洗掉hcv8jop4ns5r.cn | hcg偏高是什么原因hcv8jop8ns3r.cn | 背痛去医院挂什么科travellingsim.com | 月经来了喝红糖水有什么好处hcv8jop7ns6r.cn | 金黄色葡萄球菌是什么菌hcv8jop2ns4r.cn |