mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
* feat(animation): support animating width/height properties - width/height can be specified in any valid PercentLength form that can be parsed. - make width/height properties be based on animatable CSS property. TODO: affectsLayout???? - add a few basic tests. Could probably use a few more? - fix a few null pointer exceptions in PercentLength helpers * test(ui): add animation examples to ui-tests-app - basic height animation - height animation in StackLayout - fix an issue where strings were not automatically converted to PercentLength when calling directly into `View.animate` * test(ui): cleanup and add summary/details layout example - use height transition to cover textview content. - when clicking on the summary view, animate the summary height up to a small header and show the text view. - fake animating the height on the textview by very subtly animating its translateY value while shrinking the header height. This tricks your mind into think that the text view is also vertically growing, even thought it's just slightly moving up along the Y axis. * test(ui): add animation curves test page - verify all built-in animation curve types work as expected. * test(ui): update animation curve example for multiple properties - add a segmented bar that allows choosing which properties to animate using the various curves. - interestingly, a whole bunch of properties fail with spring on iOS. - refactor width/height animations handlers to remove duplication on iOS. - implement proper spring animation for width/height on iOS. * test(ui): add stress example with 100 labels animating and fps meter - same curve/property selector as the curves example, but with 10x10 grid of items that stagger animate, and an FPS meter. - sadly it looks like width/height animations are considerably slower than the others when you have a bunch of them. I'm not sure that's entirely surprising since they interact with the layout system. - the better news is that even with the army example, my really old android 4 tablet manages ~30fps. On height/width animations from the curves example, the old tablet does fine with no noticeable FPS hit. * refactor: deduplicate existing droid width/height animations - stash to prep for replacing with LayoutTransition. * test(animation): unit tests for extent animation and PercentLength parse - update animation scaffold to allow specifying the parent stack layout height/width - test basic supported units, px, % - test basic percent length parser behaviors * chore: cleanup cruft and remove noise from diff - undo the import mangling that WebStorm helpfully applied - remove .editorconfig file - clean up in tests, remove cruft * chore: cleanup from review - more import changes * chore: remove .editorconfig
291 lines
9.8 KiB
TypeScript
291 lines
9.8 KiB
TypeScript
// Definitions.
|
|
import {
|
|
Keyframes as KeyframesDefinition,
|
|
UnparsedKeyframe as UnparsedKeyframeDefinition,
|
|
KeyframeDeclaration as KeyframeDeclarationDefinition,
|
|
KeyframeInfo as KeyframeInfoDefinition,
|
|
KeyframeAnimationInfo as KeyframeAnimationInfoDefinition,
|
|
KeyframeAnimation as KeyframeAnimationDefinition,
|
|
} from "./keyframe-animation";
|
|
|
|
import { View, Color } from "../core/view";
|
|
|
|
import { AnimationCurve } from "../enums";
|
|
|
|
// Types.
|
|
import { unsetValue } from "../core/properties";
|
|
import { Animation } from "./animation";
|
|
import {
|
|
backgroundColorProperty,
|
|
scaleXProperty, scaleYProperty,
|
|
translateXProperty, translateYProperty,
|
|
rotateProperty, opacityProperty,
|
|
widthProperty, heightProperty, PercentLength
|
|
} from "../styling/style-properties";
|
|
|
|
export class Keyframes implements KeyframesDefinition {
|
|
name: string;
|
|
keyframes: Array<UnparsedKeyframe>;
|
|
}
|
|
|
|
export class UnparsedKeyframe implements UnparsedKeyframeDefinition {
|
|
values: Array<any>;
|
|
declarations: Array<KeyframeDeclaration>;
|
|
}
|
|
|
|
export class KeyframeDeclaration implements KeyframeDeclarationDefinition {
|
|
public property: string;
|
|
public value: any;
|
|
}
|
|
|
|
export class KeyframeInfo implements KeyframeInfoDefinition {
|
|
public duration: number;
|
|
public declarations: Array<KeyframeDeclaration>;
|
|
public curve?: any = AnimationCurve.ease;
|
|
}
|
|
|
|
export class KeyframeAnimationInfo implements KeyframeAnimationInfoDefinition {
|
|
public keyframes: Array<KeyframeInfo>;
|
|
public name?: string = "";
|
|
public duration?: number = 0.3;
|
|
public delay?: number = 0;
|
|
public iterations?: number = 1;
|
|
public curve?: any = "ease";
|
|
public isForwards?: boolean = false;
|
|
public isReverse?: boolean = false;
|
|
}
|
|
|
|
interface Keyframe {
|
|
backgroundColor?: Color;
|
|
scale?: { x: number, y: number };
|
|
translate?: { x: number, y: number };
|
|
rotate?: number;
|
|
opacity?: number;
|
|
width?: PercentLength;
|
|
height?: PercentLength;
|
|
valueSource?: "keyframe" | "animation";
|
|
duration?: number;
|
|
curve?: any;
|
|
forceLayer?: boolean;
|
|
}
|
|
|
|
export class KeyframeAnimation implements KeyframeAnimationDefinition {
|
|
public animations: Array<Keyframe>;
|
|
public delay: number = 0;
|
|
public iterations: number = 1;
|
|
|
|
private _resolve;
|
|
private _reject;
|
|
private _isPlaying: boolean;
|
|
private _isForwards: boolean;
|
|
private _nativeAnimations: Array<Animation>;
|
|
private _target: View;
|
|
|
|
public static keyframeAnimationFromInfo(info: KeyframeAnimationInfo)
|
|
: KeyframeAnimation {
|
|
|
|
const length = info.keyframes.length;
|
|
let animations = new Array<Keyframe>();
|
|
let startDuration = 0;
|
|
|
|
if (info.isReverse) {
|
|
for (let index = length - 1; index >= 0; index--) {
|
|
let keyframe = info.keyframes[index];
|
|
startDuration = KeyframeAnimation.parseKeyframe(info, keyframe, animations, startDuration);
|
|
}
|
|
} else {
|
|
for (let index = 0; index < length; index++) {
|
|
let keyframe = info.keyframes[index];
|
|
startDuration = KeyframeAnimation.parseKeyframe(info, keyframe, animations, startDuration);
|
|
}
|
|
for (let index = length - 1; index > 0; index--) {
|
|
let a1 = animations[index];
|
|
let a2 = animations[index - 1];
|
|
if (a2["curve"] !== undefined) {
|
|
a1["curve"] = a2["curve"];
|
|
a2["curve"] = undefined;
|
|
}
|
|
}
|
|
}
|
|
|
|
animations.map(a => a["curve"] ? a : Object.assign(a, {curve: info.curve}));
|
|
|
|
const animation: KeyframeAnimation = new KeyframeAnimation();
|
|
animation.delay = info.delay;
|
|
animation.iterations = info.iterations;
|
|
animation.animations = animations;
|
|
animation._isForwards = info.isForwards;
|
|
|
|
return animation;
|
|
}
|
|
|
|
private static parseKeyframe(info: KeyframeAnimationInfo, keyframe: KeyframeInfo, animations: Array<Object>, startDuration: number): number {
|
|
let animation: Keyframe = {};
|
|
for (let declaration of keyframe.declarations) {
|
|
animation[declaration.property] = declaration.value;
|
|
}
|
|
|
|
let duration = keyframe.duration;
|
|
if (duration === 0) {
|
|
duration = 0.01;
|
|
} else {
|
|
duration = (info.duration * duration) - startDuration;
|
|
startDuration += duration;
|
|
}
|
|
animation.duration = info.isReverse ? info.duration - duration : duration;
|
|
animation.curve = keyframe.curve;
|
|
animation.forceLayer = true;
|
|
animation.valueSource = "keyframe";
|
|
animations.push(animation);
|
|
|
|
return startDuration;
|
|
}
|
|
|
|
public get isPlaying(): boolean {
|
|
return this._isPlaying;
|
|
}
|
|
|
|
public cancel() {
|
|
if (this._isPlaying) {
|
|
this._isPlaying = false;
|
|
for (let i = this._nativeAnimations.length - 1; i >= 0; i--) {
|
|
let animation = this._nativeAnimations[i];
|
|
if (animation.isPlaying) {
|
|
animation.cancel();
|
|
}
|
|
}
|
|
if (this._nativeAnimations.length > 0) {
|
|
let animation = this._nativeAnimations[0];
|
|
this._resetAnimationValues(this._target, animation);
|
|
}
|
|
this._rejectAnimationFinishedPromise();
|
|
}
|
|
}
|
|
|
|
public play(view: View): Promise<void> {
|
|
if (this._isPlaying) {
|
|
throw new Error("Animation is already playing.");
|
|
}
|
|
|
|
let animationFinishedPromise = new Promise<void>((resolve, reject) => {
|
|
this._resolve = resolve;
|
|
this._reject = reject;
|
|
});
|
|
|
|
this._isPlaying = true;
|
|
this._nativeAnimations = new Array<Animation>();
|
|
this._target = view;
|
|
|
|
if (this.delay !== 0) {
|
|
setTimeout(() => this.animate(view, 0, this.iterations), this.delay);
|
|
}
|
|
else {
|
|
this.animate(view, 0, this.iterations);
|
|
}
|
|
|
|
return animationFinishedPromise;
|
|
}
|
|
|
|
private animate(view: View, index: number, iterations: number) {
|
|
if (!this._isPlaying) {
|
|
return;
|
|
}
|
|
if (index === 0) {
|
|
let animation = this.animations[0];
|
|
|
|
if ("backgroundColor" in animation) {
|
|
view.style[backgroundColorProperty.keyframe] = animation.backgroundColor;
|
|
}
|
|
if ("scale" in animation) {
|
|
view.style[scaleXProperty.keyframe] = animation.scale.x;
|
|
view.style[scaleYProperty.keyframe] = animation.scale.y;
|
|
}
|
|
if ("translate" in animation) {
|
|
view.style[translateXProperty.keyframe] = animation.translate.x;
|
|
view.style[translateYProperty.keyframe] = animation.translate.y;
|
|
}
|
|
if ("rotate" in animation) {
|
|
view.style[rotateProperty.keyframe] = animation.rotate;
|
|
}
|
|
if ("opacity" in animation) {
|
|
view.style[opacityProperty.keyframe] = animation.opacity;
|
|
}
|
|
if ("height" in animation) {
|
|
view.style[heightProperty.keyframe] = animation.height;
|
|
}
|
|
if ("width" in animation) {
|
|
view.style[widthProperty.keyframe] = animation.width;
|
|
}
|
|
|
|
setTimeout(() => this.animate(view, 1, iterations), 1);
|
|
}
|
|
else if (index < 0 || index >= this.animations.length) {
|
|
iterations -= 1;
|
|
if (iterations > 0) {
|
|
this.animate(view, 0, iterations);
|
|
}
|
|
else {
|
|
if (this._isForwards === false) {
|
|
let animation = this.animations[this.animations.length - 1];
|
|
this._resetAnimationValues(view, animation);
|
|
}
|
|
this._resolveAnimationFinishedPromise();
|
|
}
|
|
}
|
|
else {
|
|
let animationDef = this.animations[index];
|
|
(<any>animationDef).target = view;
|
|
let animation = new Animation([animationDef]);
|
|
// Catch the animation cancel to prevent unhandled promise rejection warnings
|
|
animation.play().then(() => {
|
|
this.animate(view, index + 1, iterations);
|
|
}).catch((error: any) => {
|
|
if (error.message.indexOf("Animation cancelled") < 0) {
|
|
throw error;
|
|
}
|
|
}); // tslint:disable-line
|
|
this._nativeAnimations.push(animation);
|
|
}
|
|
}
|
|
|
|
public _resolveAnimationFinishedPromise() {
|
|
this._nativeAnimations = new Array<Animation>();
|
|
this._isPlaying = false;
|
|
this._target = null;
|
|
this._resolve();
|
|
}
|
|
|
|
public _rejectAnimationFinishedPromise() {
|
|
this._nativeAnimations = new Array<Animation>();
|
|
this._isPlaying = false;
|
|
this._target = null;
|
|
this._reject(new Error("Animation cancelled."));
|
|
}
|
|
|
|
private _resetAnimationValues(view: View, animation: Object) {
|
|
if ("backgroundColor" in animation) {
|
|
view.style[backgroundColorProperty.keyframe] = unsetValue;
|
|
}
|
|
if ("scale" in animation) {
|
|
view.style[scaleXProperty.keyframe] = unsetValue;
|
|
view.style[scaleYProperty.keyframe] = unsetValue;
|
|
}
|
|
if ("translate" in animation) {
|
|
view.style[translateXProperty.keyframe] = unsetValue;
|
|
view.style[translateYProperty.keyframe] = unsetValue;
|
|
}
|
|
if ("rotate" in animation) {
|
|
view.style[rotateProperty.keyframe] = unsetValue;
|
|
}
|
|
if ("opacity" in animation) {
|
|
view.style[opacityProperty.keyframe] = unsetValue;
|
|
}
|
|
if ("height" in animation) {
|
|
view.style[heightProperty.keyframe] = unsetValue;
|
|
}
|
|
if ("width" in animation) {
|
|
view.style[widthProperty.keyframe] = unsetValue;
|
|
}
|
|
}
|
|
}
|