3

How does one continuous loop an animation using animate? In this example, all I want to do is endlessly rotate a white square.

myBall = new Layer
    x: 100
    y: 100
    width: 200
    height: 200
    borderRadius: "20px"
    backgroundColor: "#FFF"

myBall.animate
    properties:
        rotationZ: 360
    time: 1
myBall.on Events.AnimationEnd, ->
    this.animate
        properties:
            rotationZ: 360
        time: 1
    this.backgroundColor = "#666"

After the first 360˚ z rotation, the background color will be changed to #666, but the animations will cease. I think it would be ideal if repeat: -1 indicated continuous without having to listen for AnimationEnd.

nipponese
  • 2,813
  • 6
  • 35
  • 51

3 Answers3

4

After the first animation rotates the layer to 360° you try to rotate it again to 360°, which returns in a visual non-existing animation. the solution for this is to set myBall.rotationZ = 0 right before you start rotating again. in your code:

myBall.on Events.AnimationEnd, ->
    this.rotationZ = 0 # reset to back to zero
    this.animate
        properties:
            rotationZ: 360
        time: 1

Other solution using new Animation

you can do this with states or an animation function, which results in cleaner code:

rotateAnim = new Animation
    layer: myBall
    properties:
        rotationZ: 360
    time: 1

rotateAnim.start() # kick if off once

rotateAnim.on "end", ->
    myBall.rotationZ = 0 # need to reset to zero so we can animate to 360 again
    rotateAnim.start()
0

As frischmilch suggested, you can also use the new Animation method, but his code was a little off:

# Create animation
animation = new Animation({
    layer: myBall
    properties:
        rotationZ: 360
    time: 1
})
# Start animation
animation.start()
# Loop animation
animation.on Events.AnimationEnd, ->
    # Reset rotation before looping
    myBall.rotationZ = 0
    animation.start()
Community
  • 1
  • 1
0

Use reverse() to create the reverse animation and trigger it.

animation = new Animation 
    layer: bookmark
    properties:
        scale: 1.08
    time: 1
    curve: Bezier.easeInOut

animation.start()

animation.onAnimationEnd ->
    reversedAnimation = animation.reverse()
    reversedAnimation.start()
    reversedAnimation.onAnimationEnd ->
        animation.start()

And here it is in action.

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245