Skip to content
This repository was archived by the owner on Nov 17, 2022. It is now read-only.

Commit e4cb55d

Browse files
committed
Merge pull request #291 from NativeScript/feature/css-animations
Added documentation about CSS animations
2 parents 9d05ba1 + a5b13f3 commit e4cb55d

File tree

5 files changed

+893
-388
lines changed

5 files changed

+893
-388
lines changed

img/modules/animation/hello-world.gif

31 KB
Loading

ui/animation-code.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
---
2+
title: Animations with Code
3+
description: Animated view properties by using code.
4+
slug: animations-code
5+
position: 11
6+
publish: false
7+
---
8+
9+
# Animations with code
10+
11+
The easiest way to animate a **single** [`View`](../ApiReference/ui/core/view/View.md) is by using the `View.animate` method which accepts an [`AnimationDefinition`](../ApiReference/ui/animation/AnimationDefinition.md), immediately starts the animation and then returns its finished promise.
12+
13+
__Example 20: How to execute animation on single view.__
14+
15+
``` JavaScript
16+
view.animate({
17+
translate: { x: 0, y: 100},
18+
duration: 1000,
19+
curve: enums.AnimationCurve.easeIn
20+
});
21+
```
22+
``` TypeScript
23+
view.animate({
24+
translate: { x: 0, y: 100},
25+
duration: 1000,
26+
curve: enums.AnimationCurve.easeIn
27+
});
28+
```
29+
30+
> You should create an [`Animation`](../ApiReference/ui/animation/Animation.md) class in order to be able to **cancel** the animation. This is demonstrated below.
31+
32+
## The AnimationDefinition interface
33+
34+
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/View.md). The animatable properties are:
35+
36+
- **opacity**
37+
- **backgroundColor**
38+
- **translateX** and **translateY**
39+
- **scaleX** and **scaleY**
40+
- **rotate**
41+
42+
The [`AnimationDefinition`](../ApiReference/ui/animation/AnimationDefinition.md) interface has the following members:
43+
44+
- **target**: The view whose property is to be animated.
45+
- **opacity**: Animates the opacity of the view. Value should be a number between 0.0 and 1.0.
46+
- **backgroundColor**: Animates the backgroundColor of the view.
47+
- **translate**: Animates the translate affine transform of the view. Value should be a [`Pair`](../ApiReference/ui/animation/Pair.md).
48+
- **scale**: Animates the scale affine transform of the view. Value should be a [`Pair`](../ApiReference/ui/animation/Pair.md).
49+
- **rotate**: Animates the rotate affine transform of the view. Value should be a number specifying the rotation amount in degrees.
50+
- **duration**: The length of the animation in milliseconds. The default duration is 300 milliseconds.
51+
- **delay**: The amount of time, in milliseconds, to delay starting the animation.
52+
- **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`.
53+
- **curve**: An optional animation curve. Possible values are contained in the [AnimationCurve enumeration](../ApiReference/ui/enums/AnimationCurve/README.md). Alternatively, you can pass an instance 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.
54+
55+
All members of the interface are **optional** and have default values with the following exceptions:
56+
57+
- target is only optional when calling the `animate` method of a [`View`](../ApiReference/ui/core/view/View.md) instance since it is set automatically for you.
58+
- You must specify at least one property from this list: opacity, backgroundColor, scale, rotate or translate.
59+
60+
## The Animation class
61+
62+
The [`Animation`](../ApiReference/ui/animation/Animation.md) class represents a **set** of one or more [`AnimationDefinitions`]({{site.baseurl}}/ApiReference/ui/animation/AnimationDefinition.md) that can be played either **simultaneously or sequentially**. **This class is typically used when you need to animate several views together**. The constructor of 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:
63+
64+
- **play**: A method that starts the animation and returns the instance it was called on for fluent animation chaining.
65+
- **cancel**: A void method that stops the animation.
66+
- **finished**: A promise that will be resolved when the animation finishes or rejected when the animation is cancelled or stops for another reason.
67+
- **isPlaying**: A boolean property returning __True__ if the animation is currently playing.
68+
69+
## Animating multiple properties
70+
71+
It is easy to animate multiple properties at once; just pass the desired animatable properties and the corresponding values when calling the animate function.
72+
73+
__Example 21: How to animate multiple properties.__
74+
75+
``` JavaScript
76+
view.animate({
77+
backgroundColor: new color.Color("#3D5AFE"),
78+
opacity: 0.5,
79+
translate: { x: 100, y: 100 },
80+
rotate: 180,
81+
duration: 3000
82+
});
83+
```
84+
``` TypeScript
85+
view.animate({
86+
backgroundColor: new color.Color("#3D5AFE"),
87+
opacity: 0.5,
88+
translate: {x: 100, y: 100},
89+
rotate: 180,
90+
duration: 3000
91+
});
92+
```
93+
94+
![multiple-properties](../img/modules/animation/multiple-properties.gif "Multiple Properties")
95+
96+
## Chaining animations with promises
97+
98+
The animate method returns a promise that you can use to chain animations, as shown in __Example 21__.
99+
100+
__Example 22: How to create chain animations.__
101+
102+
``` JavaScript
103+
view.animate({ opacity: 0 })
104+
.then(function () { return view.animate({ opacity: 1 }); })
105+
.then(function () { return view.animate({ translate: { x: 100, y: 100 } }); })
106+
.then(function () { return view.animate({ translate: { x: 0, y: 0 } }); })
107+
.then(function () { return view.animate({ scale: { x: 3, y: 3 } }); })
108+
.then(function () { return view.animate({ scale: { x: 1, y: 1 } }); })
109+
.then(function () { return view.animate({ rotate: 180 }); })
110+
.then(function () { return view.animate({ rotate: 0 }); })
111+
.then(function () {
112+
console.log("Animation finished");
113+
})
114+
.catch(function (e) {
115+
console.log(e.message);
116+
});
117+
```
118+
``` TypeScript
119+
view.animate({ opacity: 0 })
120+
.then(() => view.animate({ opacity: 1 }))
121+
.then(() => view.animate({ translate: { x: 100, y: 100 } }))
122+
.then(() => view.animate({ translate: { x: 0, y: 0 } }))
123+
.then(() => view.animate({ scale: { x: 3, y: 3 } }))
124+
.then(() => view.animate({ scale: { x: 1, y: 1 } }))
125+
.then(() => view.animate({ rotate: 180 }))
126+
.then(() => view.animate({ rotate: 0 }))
127+
.then(() => {
128+
console.log("Animation finished");
129+
})
130+
.catch((e) => {
131+
console.log(e.message);
132+
});
133+
```
134+
135+
![chaining-with-promises](../img/modules/animation/chaining-with-promises.gif "Chaining with Promises")

0 commit comments

Comments
 (0)