我應用了“.animation (.easeIn)”,但它已被棄用。告訴我:影片在 iOS 15.0 中已被棄用:使用影片或影片(:值:)代替,但影片(:值:)我不知道我需要傳遞哪個值。我無法創建開始緩慢的影片。你能幫我理解如何正確使用影片(_:值:)來制作影片嗎?我必須創建影片,當我移動滑塊時,影像會根據它們放大或縮小的程度在空間中緩慢地重新組合。謝謝你。
'''
import SwiftUI
struct GalleryView: View {
// MARK: - PROPERTIES
@State private var selectedAnimal: String = "lion"
let animals: [Animal] = Bundle.main.decode("animals.json")
let haptics = UIImpactFeedbackGenerator(style: .medium)
@State private var gridLayout: [GridItem] = [GridItem(.flexible())]
@State private var gridColumn: Double = 3.0
func gridSwitch() {
gridLayout = Array(repeating: .init(.flexible()), count: Int(gridColumn))
}
// MARK: - BODY
var body: some View {
ScrollView(.vertical, showsIndicators: false) {
VStack(alignment: .center, spacing: 30) {
// MARK: - IMAGE
Image(selectedAnimal)
.resizable()
.scaledToFit()
.clipShape(Circle())
.overlay(Circle().stroke(Color.white, lineWidth: 8))
// MARK: - SLIDER
Slider(value: $gridColumn, in: 2...4, step: 1) // se aumento il valore nello slider ci saranno più sezioni
.padding(.horizontal)
.onChange(of: gridColumn, perform: { value in
gridSwitch()
})
// MARK: - GRID
LazyVGrid(columns: gridLayout, alignment: .center, spacing: 10) {
ForEach(animals) { item in
Image(item.image)
.resizable()
.scaledToFit()
.clipShape(Circle())
.overlay(Circle().stroke(Color.white, lineWidth: 1))
.onTapGesture {
selectedAnimal = item.image
haptics.impactOccurred()
}
} //: LOOP
} //: GRID
.animation(.easeIn)
.onAppear(perform: {
gridSwitch()
})
} //: VSTACK
.padding(.horizontal, 10)
.padding(.vertical,50)
} //: SCROLL
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(MotionAnimationView())
}
}
// MARK: - PREVIEW
struct GalleryView_Previews: PreviewProvider {
static var previews: some View {
GalleryView()
}
}
uj5u.com熱心網友回復:
正如錯誤所說,.animation()不推薦使用iOS 15,您需要立即使用.animation(value:)。其中value是您希望觸發影片的系結,在您的情況下,我認為它是selectedAnimal.
應用它就像
VStack {
}
.animation(.easeIn, value: selectedAnimal)
正如評論中所討論的,如果你想讓你gridLayout的影片變得可影片,那就有點棘手了。Equatable因為如果您希望它們是Arrays 必須是animatable,因為擴展GridItem不是一個好的解決方案,所以我想出了這個:
洗掉你的.animation方法用這個改變你的gridSwitch功能:
struct GalleryView: View {
// ... irrelevant code
@State private var isGridChanged = false
func gridSwitch() {
if (isGridChanged) {
withAnimation {
gridLayout = Array(repeating: .init(.flexible()), count: Int(gridColumn))
}
}
else {
gridLayout = Array(repeating: .init(.flexible()), count: Int(gridColumn))
isGridChanged = true
}
}
isGridChanged是必需的,因為當您View在初始化時更改 gridLayout 時,它會導致一個奇怪的錯誤,即當應用程式啟動時,所有內容都會縮小,因為withAnimation.
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/521024.html
標籤:代码动画迅捷
下一篇:如何在應用欄上滾動和影片物件?
