AudioKit / AudioKitUI

Controls and Visualization for AudioKit apps
MIT License
187 stars 52 forks source link

.animation is deprecated in iOS15 #32

Open transat opened 2 years ago

transat commented 2 years ago

I believe .animation now needs to be more specific, by adding a value parameter or by changing it to .withAnimation.

I'm not sure if this is the right way, and I haven't tested it but the following suppresses the warning in FFTView.swift, for example:

struct AmplitudeBar: View {
    var amplitude: Double
    var linearGradient: LinearGradient
    var paddingFraction: CGFloat = 0.2
    var includeCaps: Bool = true
    @State private var offset: CGFloat = 0
    @State private var geoSize: CGFloat = 0

    var body: some View {
        GeometryReader { geometry in
            ZStack(alignment: .bottom) {
                // Colored rectangle in back of ZStack
                Rectangle()
                    .fill(self.linearGradient)

                // Dynamic black mask padded from bottom in relation to the amplitude
                Rectangle()
                    .fill(Color.black)
                    .mask(Rectangle().padding(.bottom, geoSize))
                    .onAppear {
                        withAnimation(.easeOut(duration: 0.15)) { self.geoSize = geometry.size.height * CGFloat(amplitude) }
                    }
//                    .animation(.easeOut(duration: 0.15))

                // White bar with slower animation for floating effect
                if includeCaps {
                    addCap(width: geometry.size.width, height: geometry.size.height)
                }
            }
            .padding(geometry.size.width * paddingFraction / 2)
            .border(Color.black, width: geometry.size.width * paddingFraction / 2)
        }
    }

    // Creates the Cap View - seperate method allows variable definitions inside a GeometryReader
    func addCap(width: CGFloat, height: CGFloat) -> some View {
        let padding = width * paddingFraction / 2
        let capHeight = height * 0.005
        let capDisplacement = height * 0.02
        let capOffset = -height * CGFloat(amplitude) - capDisplacement - padding * 2
        let capMaxOffset = -height + capHeight + padding * 2

        return Rectangle()
            .fill(Color.white)
            .frame(height: capHeight)
            .offset(x: 0.0, y: offset) // prevents offset from pushing cap outside of it's frame
            .onAppear {
                withAnimation(.easeOut(duration: 0.6)) { self.offset = -height > capOffset - capHeight ? capMaxOffset : capOffset }
            }
//            .animation(.easeOut(duration: 0.6))
    }
}
ShowtimeSoft commented 2 years ago

As you say, you could also just add a value parameter to .animation.

So I presume that:

.animation(.easeOut(duration: 0.6), value: amplitude)

would also work.