SwiftUI:'value' 参数在 iOS 15 的新动画方法签名中未按预期工作
SwiftUI: 'value' parameter not working as expected in iOS 15's new animation method signature
我正在尝试解决 iOS 15 的警告:
'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead.
在出现在屏幕顶部的视图中,看起来像推送通知警报(或所谓的小吃店),但是当我在 [=15] 中使用任何 Equatable
值时,视图会失去动画效果=]参数,这个view的代码如下(可以原样复制粘贴试试):
import Foundation
import SwiftUI
struct ContentView: View {
@State private var showSnackBar = false
var body: some View {
ZStack {
Color.gray
Text("Show Me")
.padding()
.onTapGesture {
showSnackBar = true
}
EmptyView()
.snackBar(title: "this is a test message", show: $showSnackBar)
.offset(x: 0, y: 50)
}
.edgesIgnoringSafeArea(.all)
}
}
struct SnackBarModifier: ViewModifier {
var title: String
@Binding var show: Bool
@State var task: DispatchWorkItem?
func body(content: Content) -> some View {
ZStack {
if self.show {
VStack {
HStack {
Text(title)
Spacer()
}
.foregroundColor(.white)
.padding(12)
.background(Color.orange)
.cornerRadius(8)
Spacer()
}
.frame(maxWidth: UIScreen.main.bounds.width - 40)
.animation(.easeInOut(duration: 0.7), value: ...) //<---- this is where I should use the value in order to solve iOS 15 animation deprecation warning
.transition(AnyTransition.move(edge: .top)
.combined(with: .opacity))
.onTapGesture {
withAnimation {
self.show = false
}
}
.onAppear {
self.task = DispatchWorkItem {
withAnimation {
self.show = false
}
}
// Auto dismiss after 2 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: self.task!)
}
.onDisappear {
self.task?.cancel()
}
}
content
}
}
}
extension View {
func snackBar(title: String, show: Binding<Bool>) -> some View {
self.modifier(SnackBarModifier(title: title, show: show))
}
}
我尝试使用一个单独的布尔状态变量,该变量在 self.show
设置为 true 时切换但无济于事,我如何使用 easeInOut(duration:)
为这个小吃店视图的外观设置动画动画使用 iOS 15 的动画风格 ?
将动画修改器放在 ZStack
容器顶部,例如
struct SnackBarModifier: ViewModifier {
var title: String
@Binding var show: Bool
@State var task: DispatchWorkItem?
func body(content: Content) -> some View {
ZStack {
if self.show {
// other content ...
}
content
}
.animation(.easeInOut(duration: 0.7), value: show) // << here !!
}
}
测试 Xcode 13.2 / iOS 15.2
我正在尝试解决 iOS 15 的警告:
'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead.
在出现在屏幕顶部的视图中,看起来像推送通知警报(或所谓的小吃店),但是当我在 [=15] 中使用任何 Equatable
值时,视图会失去动画效果=]参数,这个view的代码如下(可以原样复制粘贴试试):
import Foundation
import SwiftUI
struct ContentView: View {
@State private var showSnackBar = false
var body: some View {
ZStack {
Color.gray
Text("Show Me")
.padding()
.onTapGesture {
showSnackBar = true
}
EmptyView()
.snackBar(title: "this is a test message", show: $showSnackBar)
.offset(x: 0, y: 50)
}
.edgesIgnoringSafeArea(.all)
}
}
struct SnackBarModifier: ViewModifier {
var title: String
@Binding var show: Bool
@State var task: DispatchWorkItem?
func body(content: Content) -> some View {
ZStack {
if self.show {
VStack {
HStack {
Text(title)
Spacer()
}
.foregroundColor(.white)
.padding(12)
.background(Color.orange)
.cornerRadius(8)
Spacer()
}
.frame(maxWidth: UIScreen.main.bounds.width - 40)
.animation(.easeInOut(duration: 0.7), value: ...) //<---- this is where I should use the value in order to solve iOS 15 animation deprecation warning
.transition(AnyTransition.move(edge: .top)
.combined(with: .opacity))
.onTapGesture {
withAnimation {
self.show = false
}
}
.onAppear {
self.task = DispatchWorkItem {
withAnimation {
self.show = false
}
}
// Auto dismiss after 2 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: self.task!)
}
.onDisappear {
self.task?.cancel()
}
}
content
}
}
}
extension View {
func snackBar(title: String, show: Binding<Bool>) -> some View {
self.modifier(SnackBarModifier(title: title, show: show))
}
}
我尝试使用一个单独的布尔状态变量,该变量在 self.show
设置为 true 时切换但无济于事,我如何使用 easeInOut(duration:)
为这个小吃店视图的外观设置动画动画使用 iOS 15 的动画风格 ?
将动画修改器放在 ZStack
容器顶部,例如
struct SnackBarModifier: ViewModifier {
var title: String
@Binding var show: Bool
@State var task: DispatchWorkItem?
func body(content: Content) -> some View {
ZStack {
if self.show {
// other content ...
}
content
}
.animation(.easeInOut(duration: 0.7), value: show) // << here !!
}
}
测试 Xcode 13.2 / iOS 15.2