How can I make a custom Animation View modifier in SwiftUI in purpose of accessing internal values for changed Value

Viewed 109

I want access to internal values of animation Modifier for use in my project,

For example: I am changing the offset of yellow Circle from -200.0 to 200.0 with a simple State and also I am applying animation Modifier for smooth transition, and I want show the changed value from -200.0 to 200.0, but as all of you know SwiftUI just update View for -200.0 and 200.0, maybe apple SwiftUI team would thought lots about it that they do not need update Views for tiny small changes from -200.0 to 200.0 and they capsulate that part inside of animation modifier, how ever they are some people like me want access those internal values.

First of all if there is any way to access those values I want to know how?

If there is no way that we can access those sweet values then I want try build a custom animation modifier, which take same input as apple animation modifier, and i want to know how can i make this happen?

Do we have access to apple animation modifier in github or some where, I need help for start, also I do not want update offset or the value with a timer or something like that, because those ways force View update their body while we are updating our State value in a timer, so that option looks inconvenient or even economically not good in process power.

struct ContentView: View {
    
    @State private var offset: CGFloat = -200.0
    
    var body: some View {
        
        Circle()
            .fill(Color.yellow)
            .frame(width: 200.0, height: 200.0, alignment: .center)
            .cornerRadius(10.0)
            .overlay(Text(String(describing: offset)).bold())
            .offset(y: offset)
            .animation(.easeInOut, value: offset)
            .customAnimation(.easeInOut, value: offset)   // <<: Here!!!
            .onTapGesture {
                
                if (offset == -200.0) { offset = 200.0 }
                else { offset = -200.0 }
                
            }
            .shadow(radius: 10.0)
        
    }
    
}


struct CustomAnimationViewModifier<V: Equatable>: ViewModifier {
    
    let animation: Animation?
    let value: V
    
    func body(content: Content) -> some View {
        
        return content
        
        
    }
    
}


extension View {
    
    func customAnimation<V: Equatable>(_ animation: Animation?, value: V) -> some View {
        
        return self.modifier(CustomAnimationViewModifier(animation: animation, value: value))
        
    }
    
}

enter image description here

0 Answers
Related