Problem to Achieve Curved Animation

Problem to achieve curved animation

You can do a Frame by Frame animation. You can position the object step by step to create a curve. That will be of limited reuse, but you can do it.

Or you can write your own animation to create a subclass of TweenAnimation that can animate along a curve. If you're good with math and can understand bezier curves then that might be a straight forward option. Once you have that class you could easily animate across any curved path, but it's more work.

http://en.wikipedia.org/wiki/B%C3%A9zier_curve

Here's some Java code:

http://www.math.ubc.ca/~cass/gfx/bezier.html

Creating Animation class problem

You can do a Frame by Frame animation. You can position the object step by step to create a curve. That will be of limited reuse, but you can do it.

Or you can write your own animation to create a subclass of TweenAnimation that can animate along a curve. If you're good with math and can understand bezier curves then that might be a straight forward option. Once you have that class you could easily animate across any curved path, but it's more work.

http://en.wikipedia.org/wiki/B%C3%A9zier_curve

Here's some VB code:

http://www.codetoad.com/vb_bezier.asp

Here's some Java code:

http://www.math.ubc.ca/~cass/gfx/bezier.html

And also take a look at these questions

animate-image-along-path-on-android

Strange problem with UIBezierPath animation

In my experience, the code that generates arcs creates different numbers of cubic bezier curves under the covers as the arc angle changes.

That changes the number of control points in the two curves, and messes up the animation. (as David Rönnqvist says, animations are undefined if the starting and ending path have a different number of control points.)

From what I've read, a full circle requires 4 cubic bezier curves to complete.

It wouldn't be that hard to create a variant of the addArc method that always built the arc using 4 cubic bezier curves, regardless of the arc angle. That's what I would suggest.

You could probably break your arc into 4 pieces (Using 4 sequential calls to addArc(withCenter:...) with different start and end angles such that they combine to make your desired full arc. Each of those should be short enough arc lengths to be a single Bezier curve, so you should get the same number of control points for the beginning and ending combined curve.

If you rewrite your calculateMoonPath function like this:

func calculateMoonPath(for angle: CGFloat) -> UIBezierPath {
let center = CGPoint(x: view.bounds.midX, y: view.bounds.midY)
let radius = view.bounds.height/2

let path = UIBezierPath(arcCenter: center,
radius: radius,
startAngle: -.pi/2,
endAngle: .pi/2,
clockwise: true)
let startAngle = .pi/2 - angle
let endAngle = angle - .pi/2
let delta = (endAngle - startAngle) / 4
for index in 0...3 {
let thisStart = startAngle + delta * CGFloat(index)
let thisEnd = startAngle + delta * CGFloat(index + 1)
path.addArc(withCenter: .init(x: center.x - radius * tan(angle), y: center.y),
radius: radius / CGFloat(cosf(Float(angle))),
startAngle: thisStart,
endAngle: thisEnd,
clockwise: false
)
}
path.close()
return path
}

That yields the following:

Sample Image

Flutter Animation Is Not Working Correctly

Wrap your Bottom sheet container with AnimatedBuilder widget and pass the animation instances to it instead of calling setState method in the listener.

Refer here for documentation:
https://api.flutter.dev/flutter/widgets/AnimatedBuilder-class.html

Animation not working properly in flutter?

Your code looks fine, but one piece is missing,

That you should listen to each change of your AnimationController and each time rebuild that part of your view that should be animated.

Just add that in your initState() method:

_controller.addListener((){
setState(() {});
});

Also, you could remove setState from RaisedButton onPressed:, it's not necessary to have it there.



Related Topics



Leave a reply



Submit