我正在嘗試制作一個簡單的影像選擇器,我希望從一個影像到另一個影像的過渡是影片的。似乎是一件非常簡單的事情,但我一直無法找到單一的解決方案。我必須在這里遺漏一些非常簡單的東西。
這是一個簡化版本:
struct ContentView: View {
let photos = ["gear", "person", "person.2", "car", "leaf"]
@State private var currentIndex: Int = 0
@State private var showAnimation: Bool = true
private var scalingFactor: CGFloat = 0.5
var body: some View {
VStack {
Text("Image Detection")
.font(.system(size: 30))
.fontWeight(.heavy)
.padding(.top, 30)
Spacer()
Image(systemName: photos[currentIndex])
.resizable()
.frame(width: 250, height: 250)
//this does nothing
//.transition(.move(edge: .bottom))
//this does nothing
//.animation(.spring(), value: currentIndex)
//this does nothing
//.animation(.easeInOut, value: showAnimation)
//this does nothing
//.transition(AnyTransition.opacity.animation(.easeInOut(duration: 1.0)))
//this does nothing
// .animation(showAnimation ? Animation.easeOut(duration: 2.0) : Animation.easeOut(duration: 1.0), value: showAnimation)
//this works - but alternates scaled and not with each image
//and is not really an animation
.scaleEffect(showAnimation ? 1.0 : 0.5)
Spacer()
HStack {
Button(action: {
if self.currentIndex >= self.photos.count {
self.currentIndex = self.currentIndex - 1
} else {
self.currentIndex = 0
}
withAnimation {
self.showAnimation.toggle()
}
}, label: {
Image(systemName: "arrowtriangle.backward.fill")
})
.padding()
.foregroundColor(Color.blue)
.font(.largeTitle)
Spacer()
Button(action: {
if self.currentIndex < self.photos.count - 1 {
self.currentIndex = self.currentIndex 1
} else {
self.currentIndex = self.photos.count - 1
}
withAnimation {
self.showAnimation.toggle()
}
}, label: {
Image(systemName: "arrowtriangle.forward.fill")
})
.padding()
.foregroundColor(Color.blue)
.font(.largeTitle)
}
.padding(.horizontal, 50)
Spacer()
}//v
}//body
}//content view
任何指導將不勝感激。Xcode 13.2.1 iOS 15.2
uj5u.com熱心網友回復:
當前使用 SwiftUI 的方法是使用.transition(), 因為.animation()已被棄用。
重要的是要了解它.transition()是在視圖出現或消失時觸發的。您的視圖不會因為您更改@State變數而被完全重新繪制:在您的代碼中,Image更改但它始終保留在視圖中。
一種解決方案是觸發影像完全消失并重新出現一個新影像。下面的代碼會根據showAnimation. 看到我只使用.transition()了 ,但效果很好:
- 它是不對稱的
withAnimation()閉包也包含了currentIndex
struct Example: View {
let photos = ["gear", "person", "person.2", "car", "leaf"]
@State private var currentIndex: Int = 0
@State private var showAnimation: Bool = true
private var scalingFactor: CGFloat = 0.5
var body: some View {
VStack {
Text("Image Detection")
.font(.system(size: 30))
.fontWeight(.heavy)
.padding(.top, 30)
Spacer()
if showAnimation {
image
} else {
image
}
Spacer()
HStack {
Button(action: {
withAnimation {
showAnimation.toggle()
if self.currentIndex >= self.photos.count {
self.currentIndex = self.currentIndex - 1
} else {
self.currentIndex = 0
}
}
}, label: {
Image(systemName: "arrowtriangle.backward.fill")
})
.padding()
.foregroundColor(Color.blue)
.font(.largeTitle)
Spacer()
Button(action: {
withAnimation {
showAnimation.toggle()
if self.currentIndex < self.photos.count - 1 {
self.currentIndex = self.currentIndex 1
} else {
self.currentIndex = self.photos.count - 1
}
}
}, label: {
Image(systemName: "arrowtriangle.forward.fill")
})
.padding()
.foregroundColor(Color.blue)
.font(.largeTitle)
}
.padding(.horizontal, 50)
Spacer()
}//v
}//body
private var image: some View {
Image(systemName: photos[currentIndex])
.resizable()
.frame(width: 250, height: 250)
.transition(.asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading)))
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/433139.html
