|
| 1 | +--- |
| 2 | +nav-title: Animation |
| 3 | +title: "Animation" |
| 4 | +description: Learn how to animate view properties. |
| 5 | +position: 18 |
| 6 | +--- |
| 7 | + |
| 8 | +# Animation API |
| 9 | + |
| 10 | +## AnimationDefinition Interface |
| 11 | +The [`AnimationDefinition`](ApiReference/ui/animation/AnimationDefinition.md) interface is central for defining an animation for **one or more properties** of a **single** [`View`](ApiReference/ui/core/View.md). The animatable properties are: |
| 12 | + - opacity |
| 13 | + - backgroundColor |
| 14 | + - translateX and translateY |
| 15 | + - scaleX and scaleY |
| 16 | + - rotate |
| 17 | + |
| 18 | +The [`AnimationDefinition`](ApiReference/ui/animation/AnimationDefinition.md) interface has the following members: |
| 19 | + - target: The view whose property is to be animated. |
| 20 | + - opacity: Animates the opacity of the view. Value should be a number between 0.0 and 1.0. |
| 21 | + - backgroundColor: Animates the backgroundColor of the view. |
| 22 | + - translate: Animates the translate affine transform of the view. Value should be a [`Pair`](ApiReference/ui/animation/Pair.md). |
| 23 | + - scale: Animates the scale affine transform of the view. Value should be a [`Pair`](ApiReference/ui/animation/Pair.md). |
| 24 | + - rotate: Animates the rotate affine transform of the view. Value should be a number specifying the rotation amount in degrees. |
| 25 | + - duration: The length of the animation in milliseconds. The default duration is 300 milliseconds. |
| 26 | + - delay: The amount of time, in milliseconds, to delay starting the animation. |
| 27 | + - iterations: Specifies how many times the animation should be played. Default is 1. iOS animations support fractional iterations, i.e. 1.5. To repeat an animation infinitely, use Number.POSITIVE_INFINITY |
| 28 | + - curve: An optional animation curve of type [`UIViewAnimationCurve`](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/c/tdef/UIViewAnimationCurve) for iOS or [`android.animation.TimeInterpolator`](http://developer.android.com/reference/android/animation/TimeInterpolator.html) for Android. |
| 29 | + |
| 30 | + All members of the interface are **optional** and have default values with the following exceptions: |
| 31 | + - target is only optional when calling the **animate** method of a [`View`](ApiReference/ui/core/View.md) instance since it is set automatically for you. |
| 32 | + - You must specify at least one property among opacity, backgroundColor, scale, rotate and translate. |
| 33 | + |
| 34 | +## Animation Class |
| 35 | +The [`Animation`](ApiReference/ui/animation/Animation.md) class represents a **set** of one or more [`AnimationDefinitions`](ApiReference/ui/animation/AnimationDefinition.md which can be played either **simultaneously or sequentially**. **This class is typically used when you need to animate several views together**. The constructor of the the [`Animation`](ApiReference/ui/animation/Animation.md) class accepts an array of [`AnimationDefinitions`](ApiReference/ui/animation/AnimationDefinition.md) and a boolean parameter indicating whether to play the animations sequentially. Creating an instance of the [`Animation`](ApiReference/ui/animation/Animation.md) class does not start the animation playback. The class has four members: |
| 36 | + - play: a method that starts the animation and returns the instance it was called on for fluent animation chaining. |
| 37 | + - cancel: a void method which stops the animation. |
| 38 | + - finished: a promise which will be resolved when the animation finishes or rejected when the animation is cancelled or stops for another reason. |
| 39 | + - isPlaying: a boolean property returning true if the animation is currently playing. |
| 40 | + |
| 41 | +## View.animate Method |
| 42 | +In case you need to animate a **single** [`View`](ApiReference/ui/core/View.md) and you don't need to be able to **cancel** the animation, you can simply use the shortcut **View.animate** method which accepts an [`AnimationDefinition`](ApiReference/ui/animation/AnimationDefinition.md), immediately starts the animation and returns its finished promise. |
| 43 | + |
| 44 | +# Examples |
| 45 | + |
| 46 | +The full source code for all samples is located [`here`](https://github.com/NativeScript/animation-demo). |
| 47 | + |
| 48 | +## Opacity |
| 49 | + |
| 50 | +``` JavaScript |
| 51 | +view.animate({ |
| 52 | + opacity: 0, |
| 53 | + duration: 3000 |
| 54 | +}); |
| 55 | +``` |
| 56 | +``` TypeScript |
| 57 | +view.animate({ |
| 58 | + opacity: 0, |
| 59 | + duration: 3000 |
| 60 | +}); |
| 61 | +``` |
| 62 | + |
| 63 | +## Background Color |
| 64 | + |
| 65 | +``` JavaScript |
| 66 | +view.animate({ |
| 67 | + backgroundColor: new colorModule.Color("#3D5AFE"), |
| 68 | + duration: 3000 |
| 69 | +}); |
| 70 | +``` |
| 71 | +``` TypeScript |
| 72 | +view.animate({ |
| 73 | + backgroundColor: new colorModule.Color("#3D5AFE"), |
| 74 | + duration: 3000 |
| 75 | +}); |
| 76 | +``` |
| 77 | + |
| 78 | +## Translate |
| 79 | + |
| 80 | +``` JavaScript |
| 81 | +view.animate({ |
| 82 | + translate: { x: 100, y: 100}, |
| 83 | + duration: 3000 |
| 84 | +}); |
| 85 | +``` |
| 86 | +``` TypeScript |
| 87 | +view.animate({ |
| 88 | + translate: { x: 100, y: 100}, |
| 89 | + duration: 3000 |
| 90 | +}); |
| 91 | +``` |
| 92 | + |
| 93 | +## Scale |
| 94 | + |
| 95 | +``` JavaScript |
| 96 | +view.animate({ |
| 97 | + scale: { x: 2, y: 2}, |
| 98 | + duration: 3000 |
| 99 | +}); |
| 100 | +``` |
| 101 | +``` TypeScript |
| 102 | +view.animate({ |
| 103 | + scale: { x: 2, y: 2}, |
| 104 | + duration: 3000 |
| 105 | +}); |
| 106 | +``` |
| 107 | + |
| 108 | +## Rotate |
| 109 | + |
| 110 | +``` JavaScript |
| 111 | +view.animate({ |
| 112 | + rotate: 360, |
| 113 | + duration: 3000 |
| 114 | +}); |
| 115 | +``` |
| 116 | +``` TypeScript |
| 117 | +view.animate({ |
| 118 | + rotate: 360, |
| 119 | + duration: 3000 |
| 120 | +}); |
| 121 | +``` |
| 122 | + |
| 123 | +## Multiple Properties |
| 124 | + |
| 125 | +``` JavaScript |
| 126 | +view.animate({ |
| 127 | + backgroundColor: new color.Color("#3D5AFE"), |
| 128 | + opacity: 0.5, |
| 129 | + translate: { x: 100, y: 100 }, |
| 130 | + rotate: 180, |
| 131 | + duration: 3000 |
| 132 | +}); |
| 133 | +``` |
| 134 | +``` TypeScript |
| 135 | +view.animate({ |
| 136 | + backgroundColor: new color.Color("#3D5AFE"), |
| 137 | + opacity: 0.5, |
| 138 | + translate: {x: 100, y: 100}, |
| 139 | + rotate: 180, |
| 140 | + duration: 3000 |
| 141 | +}); |
| 142 | +``` |
| 143 | + |
| 144 | +## Chaining with Promises |
| 145 | + |
| 146 | +``` JavaScript |
| 147 | +view.animate({ opacity: 0 }) |
| 148 | + .then(function () { return view.animate({ opacity: 1 }); }) |
| 149 | + .then(function () { return view.animate({ translate: { x: 100, y: 100 } }); }) |
| 150 | + .then(function () { return view.animate({ translate: { x: 0, y: 0 } }); }) |
| 151 | + .then(function () { return view.animate({ scale: { x: 3, y: 3 } }); }) |
| 152 | + .then(function () { return view.animate({ scale: { x: 1, y: 1 } }); }) |
| 153 | + .then(function () { return view.animate({ rotate: 180 }); }) |
| 154 | + .then(function () { return view.animate({ rotate: 0 }); }) |
| 155 | + .then(function () { |
| 156 | + console.log("Animation finished"); |
| 157 | +}) |
| 158 | + .catch(function (e) { |
| 159 | + console.log(e.message); |
| 160 | +}); |
| 161 | +``` |
| 162 | +``` TypeScript |
| 163 | +view.animate({ opacity: 0 }) |
| 164 | + .then(() => view.animate({ opacity: 1 })) |
| 165 | + .then(() => view.animate({ translate: { x: 100, y: 100 } })) |
| 166 | + .then(() => view.animate({ translate: { x: 0, y: 0 } })) |
| 167 | + .then(() => view.animate({ scale: { x: 3, y: 3 } })) |
| 168 | + .then(() => view.animate({ scale: { x: 1, y: 1 } })) |
| 169 | + .then(() => view.animate({ rotate: 180 })) |
| 170 | + .then(() => view.animate({ rotate: 0 })) |
| 171 | + .then(() => { |
| 172 | + console.log("Animation finished"); |
| 173 | + }) |
| 174 | + .catch((e) => { |
| 175 | + console.log(e.message); |
| 176 | + }); |
| 177 | +``` |
| 178 | + |
| 179 | +## Chaining with Animation Set |
| 180 | + |
| 181 | +``` JavaScript |
| 182 | +var definitions = new Array(); |
| 183 | +definitions.push({ target: view1, translate: { x: 200, y: 0 }, duration: 3000 }); |
| 184 | +definitions.push({ target: view2, translate: { x: 0, y: 200 }, duration: 3000 }); |
| 185 | +definitions.push({ target: view3, translate: { x: -200, y: 0 }, duration: 3000 }); |
| 186 | +definitions.push({ target: view4, translate: { x: 0, y: -200 }, duration: 3000 }); |
| 187 | +var playSequentially = true; |
| 188 | +var animationSet = new animationModule.Animation(definitions, playSequentially); |
| 189 | +animationSet.play().finished.then(function () { |
| 190 | + console.log("Animation finished"); |
| 191 | +}) |
| 192 | + .catch(function (e) { |
| 193 | + console.log(e.message); |
| 194 | +}); |
| 195 | +``` |
| 196 | +``` TypeScript |
| 197 | +var definitions = new Array<animationModule.AnimationDefinition>(); |
| 198 | +definitions.push({target: view1, translate: {x: 200, y: 0}, duration: 3000 }); |
| 199 | +definitions.push({target: view2, translate: {x: 0, y: 200}, duration: 3000 }); |
| 200 | +definitions.push({target: view3, translate: {x: -200, y: 0}, duration: 3000 }); |
| 201 | +definitions.push({target: view4, translate: {x: 0, y: -200}, duration: 3000 }); |
| 202 | +var playSequentially = true; |
| 203 | +var animationSet = new animationModule.Animation(definitions, playSequentially); |
| 204 | +animationSet.play().finished.then(() => { |
| 205 | + console.log("Animation finished"); |
| 206 | +}) |
| 207 | +.catch((e) => { |
| 208 | + console.log(e.message); |
| 209 | +}); |
| 210 | +``` |
| 211 | + |
| 212 | +## Multiple Views |
| 213 | + |
| 214 | +``` JavaScript |
| 215 | +var definitions = new Array(); |
| 216 | +var a1 = { |
| 217 | + target: view1, |
| 218 | + translate: { x: 200, y: 0 }, |
| 219 | + duration: 3000 |
| 220 | +}; |
| 221 | +definitions.push(a1); |
| 222 | +var a2 = { |
| 223 | + target: view2, |
| 224 | + translate: { x: 0, y: 200 }, |
| 225 | + duration: 3000 |
| 226 | +}; |
| 227 | +definitions.push(a2); |
| 228 | +var a3 = { |
| 229 | + target: view3, |
| 230 | + translate: { x: -200, y: 0 }, |
| 231 | + duration: 3000 |
| 232 | +}; |
| 233 | +definitions.push(a3); |
| 234 | +var a4 = { |
| 235 | + target: view4, |
| 236 | + translate: { x: 0, y: -200 }, |
| 237 | + duration: 3000 |
| 238 | +}; |
| 239 | +definitions.push(a4); |
| 240 | +var animationSet = new animationModule.Animation(definitions); |
| 241 | +animationSet.play().finished.then(function () { |
| 242 | + console.log("Animation finished"); |
| 243 | +}) |
| 244 | + .catch(function (e) { |
| 245 | + console.log(e.message); |
| 246 | +}); |
| 247 | +``` |
| 248 | +``` TypeScript |
| 249 | +var definitions = new Array<animationModule.AnimationDefinition>(); |
| 250 | +var a1: animationModule.AnimationDefinition = { |
| 251 | + target: view1, |
| 252 | + translate: {x: 200, y: 0}, |
| 253 | + duration: 3000 |
| 254 | +}; |
| 255 | +definitions.push(a1); |
| 256 | + |
| 257 | +var a2: animationModule.AnimationDefinition = { |
| 258 | + target: view2, |
| 259 | + translate: {x: 0, y: 200}, |
| 260 | + duration: 3000 |
| 261 | +}; |
| 262 | +definitions.push(a2); |
| 263 | + |
| 264 | +var a3: animationModule.AnimationDefinition = { |
| 265 | + target: view3, |
| 266 | + translate: {x: -200, y: 0}, |
| 267 | + duration: 3000 |
| 268 | +}; |
| 269 | +definitions.push(a3); |
| 270 | + |
| 271 | +var a4: animationModule.AnimationDefinition = { |
| 272 | + target: view4, |
| 273 | + translate: {x: 0, y: -200}, |
| 274 | + duration: 3000 |
| 275 | +}; |
| 276 | +definitions.push(a4); |
| 277 | + |
| 278 | +var animationSet = new animationModule.Animation(definitions); |
| 279 | + |
| 280 | +animationSet.play().finished.then(() => { |
| 281 | + console.log("Animation finished"); |
| 282 | +}) |
| 283 | +.catch((e) => { |
| 284 | + console.log(e.message); |
| 285 | +}); |
| 286 | +``` |
| 287 | + |
| 288 | +## Slide-in Effect |
| 289 | + |
| 290 | +``` JavaScript |
| 291 | +var item = new imageModule.Image(); |
| 292 | +item.src = "~/res/icon_100x100.png"; |
| 293 | +item.width = 90; |
| 294 | +item.height = 90; |
| 295 | +item.style.margin = "5,5,5,5"; |
| 296 | +item.translateX = -300; |
| 297 | +item.opacity = 0; |
| 298 | +item.on("loaded", function (args) { |
| 299 | + args.object.animate({ translate: { x: 0, y: 0 }, opacity: 1 }); |
| 300 | +}); |
| 301 | +wrapLayout.addChild(item); |
| 302 | +``` |
| 303 | +``` TypeScript |
| 304 | +var item = new imageModule.Image(); |
| 305 | +item.src = "~/res/icon_100x100.png"; |
| 306 | +item.width = 90; |
| 307 | +item.height = 90; |
| 308 | +item.style.margin = "5,5,5,5"; |
| 309 | +item.translateX = -300; |
| 310 | +item.opacity = 0; |
| 311 | +item.on("loaded", (args: observable.EventData) => { |
| 312 | + (<viewModule.View>args.object).animate({translate: { x: 0, y: 0 }, opacity: 1}); |
| 313 | +}); |
| 314 | +wrapLayout.addChild(item); |
| 315 | +``` |
| 316 | + |
| 317 | +## Infinite |
| 318 | + |
| 319 | +``` JavaScript |
| 320 | +animationSet = new animationModule.Animation([{ |
| 321 | + target: view, |
| 322 | + rotate: 360, |
| 323 | + duration: 3000, |
| 324 | + iterations: Number.POSITIVE_INFINITY, |
| 325 | + curve: view.ios ? UIViewAnimationCurve.UIViewAnimationCurveLinear : new android.view.animation.LinearInterpolator |
| 326 | + }]); |
| 327 | +animationSet.play().finished.catch(function (e) { |
| 328 | + console.log("Animation stoppped!"); |
| 329 | +}); |
| 330 | +// Call animationSet.cancel() to stop it; |
| 331 | +``` |
| 332 | +``` TypeScript |
| 333 | +animationSet = new animationModule.Animation([{ |
| 334 | + target: view, |
| 335 | + rotate: 360, |
| 336 | + duration: 3000, |
| 337 | + iterations: Number.POSITIVE_INFINITY, |
| 338 | + curve: view.ios ? UIViewAnimationCurve.UIViewAnimationCurveLinear : new android.view.animation.LinearInterpolator |
| 339 | +}]); |
| 340 | +animationSet.play().finished.catch((e) => { |
| 341 | + console.log("Animation stoppped!"); |
| 342 | +}); |
| 343 | +// Call animationSet.cancel() to stop it; |
| 344 | +``` |
0 commit comments