實例方法
transaction(_:)
將給定的事務突變函數應用於視圖中使用的所有動畫。
聲明
func transaction(_ transform: @escaping (inout Transaction) -> Void) -> some View
返回值
包裝此視圖並將轉換應用於視圖中使用的所有事務的視圖。
參數
transform
應用於此視圖中事務的轉換。
詳述
使用此修改器更改或替換視圖中使用的動畫。考慮由同時執行所有三個動畫的按鈕控製的三個相同的動畫:
-
第一個動畫將 “Rotation”
Text
視圖旋轉 360 度。 -
第二個使用
transaction(_:)
修改器通過在動畫開始時添加兩秒延遲來更改動畫,然後將 “Rotation\nModified”Text
視圖動畫的旋轉速度增加 2 倍。 -
第三個動畫使用
transaction(_:)
修改器將影響“Animation\nReplaced”Text
視圖的旋轉動畫替換為彈簧動畫。
以下代碼實現了這些動畫:
struct TransactionExample: View {
@State var flag = false
var body: some View {
VStack(spacing: 50) {
HStack(spacing: 30) {
Text("Rotation")
.rotationEffect(Angle(degrees:
self.flag ? 360 : 0))
Text("Rotation\nModified")
.rotationEffect(Angle(degrees:
self.flag ? 360 : 0))
.transaction { view in
view.animation =
view.animation?.delay(2.0).speed(2)
}
Text("Animation\nReplaced")
.rotationEffect(Angle(degrees:
self.flag ? 360 : 0))
.transaction { view in
view.animation = .interactiveSpring(
response: 0.60,
dampingFraction: 0.20,
blendDuration: 0.25)
}
}
Button("Animate") {
withAnimation(.easeIn(duration: 2.0)) {
self.flag.toggle()
}
}
}
}
}
在 Image
或 Button
等葉視圖上使用此修飾符,而不是在 VStack
或 HStack
等容器視圖上使用。轉換適用於該視圖中的所有子視圖;在容器視圖上調用 transaction(_:)
可能會導致執行範圍不受限製,具體取決於視圖層次結構的深度。
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相關用法
- Swift Optional transformEffect(_:)用法及代碼示例
- Swift Optional truncationMode(_:)用法及代碼示例
- Swift Optional textSelection(_:)用法及代碼示例
- Swift Optional toolbar(id:content:)用法及代碼示例
- Swift Optional tint(_:)用法及代碼示例
- Swift Optional toolbar(content:)用法及代碼示例
- Swift Optional tabItem(_:)用法及代碼示例
- Swift Optional textContentType(_:)用法及代碼示例
- Swift Optional touchBarItemPresence(_:)用法及代碼示例
- Swift Optional touchBar(_:)用法及代碼示例
- Swift Optional task(priority:_:)用法及代碼示例
- Swift Optional touchBar(content:)用法及代碼示例
- Swift Optional toggleStyle(_:)用法及代碼示例
- Swift Optional textInputAutocapitalization(_:)用法及代碼示例
- Swift Optional touchBarCustomizationLabel(_:)用法及代碼示例
- Swift Optional tag(_:)用法及代碼示例
- Swift Optional task(id:priority:_:)用法及代碼示例
- Swift Optional touchBarItemPrincipal(_:)用法及代碼示例
- Swift Optional symbolVariant(_:)用法及代碼示例
- Swift Optional popover(isPresented:attachmentAnchor:arrowEdge:content:)用法及代碼示例
- Swift Optional mask(alignment:_:)用法及代碼示例
- Swift Optional listSectionSeparatorTint(_:edges:)用法及代碼示例
- Swift Optional badge(_:)用法及代碼示例
- Swift Optional fullScreenCover(isPresented:onDismiss:content:)用法及代碼示例
- Swift Optional keyboardType(_:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Optional transaction(_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。