我正在嘗試解決 iOS 15 的警告:
'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead.在出現在螢屏頂部的視圖中,看起來像推送通知警報(或所謂的小吃店),但是當我使用引數Equatable中的任何值時,視圖會失去影片效果value,該視圖的代碼如下(您可以按原樣復制粘貼來試用):
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))
}
}
我嘗試使用一個單獨的布爾狀態變數,當設定為 true 但無濟于事時,我如何使用 iOS 15 的影片樣式self.show為這個小吃店視圖的外觀設定影片?easeInOut(duration:)
uj5u.com熱心網友回復:
將影片修改器放在頂部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 測驗
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/452656.html
標籤:IOS 迅捷 swiftui-动画
上一篇:首次出現時顯示所選專案單元格
