无法对序列中的SwiftUI图像进行动画处理或过渡

时间:2019-12-30 00:45:30

标签: xcode animation swiftui

我有一个带有视图的应用程序,其中显示了使用设备的相机拍摄的图像(已存储 使用Core Data),我想创建延时效果-说您拍照 一段时间内的一朵花,想要在流中显示这些图像。我有 能够做到这一点-但我希望图像之间具有过渡效果 一直未能做到。我可以应用旋转和静态比例,但是不能 从0变到1的不透明度,也不会从零变到全帧。我一定很想念 真的很简单。这是使用照片数组而不是核心数据的代码 为简单起见。

struct ContentView: View {
    @State private var activeImageIndex = 0
    @State private var startTimer = false
    @State private var myAnimationBool = true

    let timer = Timer.publish(every: 1.0, on: .main, in: .default).autoconnect()
    let myShots = ["CoolEquipment", "FishAndChipsMedium", "FlatheadLake1", "GlacierBusMedium", "Hike", "HuckALaHuckMedium", "JohnAndRiverMedium", "MacDonaldLodgeLakesideMedium"
    ]

    var body: some View {
        GeometryReader { geo in
            VStack {
                Text("Sequence")
                    .foregroundColor(.blue)
                    .font(.system(size: 25))

                Image(self.myShots[self.activeImageIndex])
                    .resizable()
                    .aspectRatio(contentMode: .fill)
                    .frame(width: geo.size.width - 20, height: geo.size.width - 20, alignment: .center)
                    .cornerRadius(20)
                    .shadow(radius: 10, x: 10, y: 10)

                    //None of these do anything
                    //.animation(.easeInOut(duration: 0.5))
                    //.animation(.easeInOut)
                    //.transition(.opacity)
                    //.transition(self.myAnimationBool ? .opacity : .slide)
                    //.transition(.scale)
                    //.transition(AnyTransition.opacity.combined(with: .slide))

                    //this works but is static
                    //.scaleEffect(self.myAnimationBool ? 0.5 : 1.0)

                    //this works but again is static
                    //.rotationEffect(.degrees(self.myAnimationBool ? 90 : 0))

                    .onReceive(self.timer) { t in
                        print("in fixed timer")
                        if self.startTimer {
                            self.activeImageIndex = (self.activeImageIndex + 1) % self.myShots.count
                        }
                    }

                    HStack {
                        Button(action: {
                            self.startTimer.toggle()
                        }) {
                            ZStack {
                                RoundedRectangle(cornerRadius: 20)
                                    .fill(Color.yellow)
                                    .frame(width: 200, height: 40)
                                Text(self.startTimer ? "Stop" : "Start").font(.headline)
                            }
                        }
                    }
                    .padding()
            }//top VStack
        }.onDisappear{ self.startTimer = false }
    }
}

任何指导将不胜感激。 Xcode 11.3(11C29)

0 个答案:

没有答案