|
| 1 | +--- |
| 2 | +nav-title: "animation How-To" |
| 3 | +title: "How-To" |
| 4 | +description: "Examples for using animation" |
| 5 | +--- |
| 6 | +# Animation |
| 7 | +Animating view properties requires the "ui/animation" module. |
| 8 | +``` JavaScript |
| 9 | +var animation = require("ui/animation"); |
| 10 | +``` |
| 11 | +# Animating properties |
| 12 | +``` JavaScript |
| 13 | +label.animate({ |
| 14 | + opacity: 0.75, |
| 15 | + backgroundColor: new colorModule.Color("Red"), |
| 16 | + translate: { x: 100, y: 100 }, |
| 17 | + scale: { x: 2, y: 2 }, |
| 18 | + rotate: 180, |
| 19 | + duration: 1000, |
| 20 | + delay: 100, |
| 21 | + iterations: 3, |
| 22 | + curve: label.ios ? UIViewAnimationCurve.UIViewAnimationCurveEaseIn : new android.view.animation.AccelerateInterpolator(1), |
| 23 | +}) |
| 24 | + .then(function () { |
| 25 | + //console.log("Animation finished."); |
| 26 | +}) |
| 27 | + .catch(function (e) { |
| 28 | + console.log(e.message); |
| 29 | +}); |
| 30 | +``` |
| 31 | +# Cancelling animation |
| 32 | +``` JavaScript |
| 33 | +var animation1 = label.createAnimation({ translate: { x: 100, y: 100 } }); |
| 34 | +animation1.play().finished |
| 35 | + .then(function () { |
| 36 | + //console.log("Animation finished"); |
| 37 | +}) |
| 38 | + .catch(function (e) { |
| 39 | + //console.log("Animation cancelled"); |
| 40 | +}); |
| 41 | +animation1.cancel(); |
| 42 | +``` |
| 43 | +# Chaining animations |
| 44 | +``` JavaScript |
| 45 | +label.animate({ opacity: 0 }) |
| 46 | + .then(function () { return label.animate({ opacity: 1 }); }) |
| 47 | + .then(function () { return label.animate({ translate: { x: 200, y: 200 } }); }) |
| 48 | + .then(function () { return label.animate({ translate: { x: 0, y: 0 } }); }) |
| 49 | + .then(function () { return label.animate({ scale: { x: 5, y: 5 } }); }) |
| 50 | + .then(function () { return label.animate({ scale: { x: 1, y: 1 } }); }) |
| 51 | + .then(function () { return label.animate({ rotate: 180 }); }) |
| 52 | + .then(function () { return label.animate({ rotate: 0 }); }) |
| 53 | + .then(function () { |
| 54 | + //console.log("Animation finished"); |
| 55 | +}) |
| 56 | + .catch(function (e) { |
| 57 | + console.log(e.message); |
| 58 | +}); |
| 59 | +``` |
| 60 | +# Reusing animations |
| 61 | +``` JavaScript |
| 62 | +var animation1 = label.createAnimation({ translate: { x: 100, y: 100 } }); |
| 63 | +var animation2 = label.createAnimation({ translate: { x: 0, y: 0 } }); |
| 64 | +animation1.play().finished |
| 65 | + .then(function () { return animation1.play().finished; }) |
| 66 | + .then(function () { return animation1.play().finished; }) |
| 67 | + .then(function () { return animation2.play().finished; }) |
| 68 | + .then(function () { return animation1.play().finished; }) |
| 69 | + .then(function () { return animation2.play().finished; }) |
| 70 | + .then(function () { |
| 71 | + //console.log("Animation finished"); |
| 72 | +}) |
| 73 | + .catch(function (e) { |
| 74 | + console.log(e.message); |
| 75 | +}); |
| 76 | +``` |
| 77 | +# Animating multiple views simultaneously |
| 78 | +``` JavaScript |
| 79 | +var animations = [ |
| 80 | + { target: label1, translate: { x: 200, y: 200 }, duration: 1000, delay: 0 }, |
| 81 | + { target: label2, translate: { x: 200, y: 200 }, duration: 1000, delay: 333 }, |
| 82 | + { target: label3, translate: { x: 200, y: 200 }, duration: 1000, delay: 666 }, |
| 83 | +]; |
| 84 | +var a = new animation.Animation(animations); |
| 85 | +a.play().finished |
| 86 | + .then(function () { |
| 87 | + //console.log("Animations finished"); |
| 88 | +}) |
| 89 | + .catch(function (e) { |
| 90 | + console.log(e.message); |
| 91 | +}); |
| 92 | +``` |
0 commit comments