When you run the following SwiftUI code, images unrelated to animation will flicker.
import SwiftUI
struct TestAnimationView: View {
@State private var effectFlg = false
private let imageName = "image1"
@State var rotationDegrees = 0.0
var body: some View {
VStack() {
Text("Rectangle")
Rectangle()
.foregroundColor(.blue)
.frame(width: 100, height: 100)
.padding(.bottom, 30)
Text("PNG Image")
if let _imageName = Bundle.main.path(forResource: imageName, ofType: "png") {
Image(uiImage: UIImage(contentsOfFile: _imageName)!)
.resizable()
.frame(width: 100, height: 100)
.padding(.bottom, 30)
}
Text("PNG Image")
if let _imageName = Bundle.main.path(forResource: imageName, ofType: "png") {
Image(uiImage: UIImage(contentsOfFile: _imageName)!)
.resizable()
.frame(width: 100, height: 100)
// .scaleEffect(self.effectFlg ? 1 : 0.8)
.rotationEffect(.degrees(self.rotationDegrees))
}
Spacer()
Button("Start Animation") {
withAnimation(.default.repeatForever().speed(0.5)) {
if self.effectFlg {
rotationDegrees = 0.0
} else {
rotationDegrees = 360.0
}
self.effectFlg.toggle()
}
}
}
}
}