0

I am using Shape animatableData to animate a random value for use, the value that get animated does not need a Shape or any other view modifier either or AnimatableModifier, I am using Shape just for accessing animatableData I could be get same result with AnimatableModifier as well but the idea is just accessing those sweet values, here my code:

struct ContentView: View {
    
    @State private var value: CGFloat = .zero
    
    var body: some View {
        
        ZStack {
            
            Button("update value") {
                value += 10.0
            }

            BaseShape(value: value)
            
        }
        .frame(width: 300.0, height: 200.0)
        .animation(Animation.easeInOut(duration: 5.0), value: value)

    }
}

struct BaseShape: Shape {
    
    var value: CGFloat
    
    init(value: CGFloat) {
        self.value = value
    }

    internal var animatableData: CGFloat {
        get { return value }
        set(newValue) { value = newValue }
    }
    
    func path(in rect: CGRect) -> Path {
        print(value)
        return Path()
    }
}

With that said, I want build a function to put out the Shape that I do not need it, like this:

struct AnimatableType: Animatable {
    
    var value: CGFloat
    var animatableData: CGFloat {
        get { return value }
        set(newValue) {
            value = newValue
        }
    }
    
}


func valueAnimator(value: CGFloat, animatedValue: @escaping (AnimatableType) -> Void) {

    // Incoming value get processed ...
    
    // Sending the animatedValue ...
    animatedValue(AnimatableType(value: value))
 
}

And my use case goal is like this:

struct ContentView: View {
    
    @State private var value: CGFloat = .zero
    
    var body: some View {
        
        ZStack {
            
            Button("update value") {
                
                value += 10.0

                valueAnimator(value: value, animatedValue: { value in
                    
                    print(value.animatableData)
 
                })
            }
 
        }
        .frame(width: 300.0, height: 200.0)
        .animation(Animation.easeInOut(duration: 5.0), value: value)

    }
}

Is it a way around to reach the goal, the use case goal with Swift? or SwiftUI?

ios coder
  • 1
  • 4
  • 31
  • 91
  • I am not looking to visual effect of animation or what ever you think, I am looking just for data called animatableData, for example when I update value from zero to 10, the in between vales. Also As you can see in question I said, I do not need Shape or modifier. – ios coder Feb 20 '22 at 19:09
  • this might help: https://stackoverflow.com/questions/57763709/swiftui-withanimation-completion-callback – ChrisR Feb 20 '22 at 20:56

0 Answers0