From b1e55f1066af83ce9e99e9467434ea812513bd94 Mon Sep 17 00:00:00 2001 From: Rossen Hristov Date: Wed, 29 Jul 2015 15:23:53 +0300 Subject: [PATCH] Animations 1.0 --- CrossPlatformModules.csproj | 3 +- apps/animations/main-page.ts | 142 +++---- apps/animations/main-page.xml | 11 +- apps/animations/model.ts | 16 +- apps/tests/testRunner.ts | 1 + apps/tests/ui/animation/animation-tests.ts | 243 ++++++++++++ ui/animation/animation-common.ts | 177 ++++++++- ui/animation/animation.android.ts | 413 +++++++++++---------- ui/animation/animation.d.ts | 105 +++--- ui/animation/animation.ios.ts | 413 +++++++++++---------- ui/core/view-common.ts | 11 + ui/core/view.d.ts | 5 + 12 files changed, 977 insertions(+), 563 deletions(-) create mode 100644 apps/tests/ui/animation/animation-tests.ts diff --git a/CrossPlatformModules.csproj b/CrossPlatformModules.csproj index 34ba87a53..9688da4ba 100644 --- a/CrossPlatformModules.csproj +++ b/CrossPlatformModules.csproj @@ -135,6 +135,7 @@ page-title-icon.xml + animation.d.ts @@ -1763,7 +1764,7 @@ PreserveNewest - + PreserveNewest diff --git a/apps/animations/main-page.ts b/apps/animations/main-page.ts index 7b31af095..39c7d293f 100644 --- a/apps/animations/main-page.ts +++ b/apps/animations/main-page.ts @@ -2,130 +2,92 @@ import observable = require("data/observable"); import pages = require("ui/page"); import buttonModule = require("ui/button"); import abs = require("ui/layouts/absolute-layout"); -import animation = require("ui/animation"); +import animationModule = require("ui/animation"); import colorModule = require("color"); import model = require("./model"); var vm = new model.ViewModel(); var page: pages.Page; -var panel1: abs.AbsoluteLayout; +var panel: abs.AbsoluteLayout; var button1: buttonModule.Button; var button2: buttonModule.Button; var button3: buttonModule.Button; -var cancelToken: any; +var buttonAnimation: animationModule.Animation; +var panelAnimation: animationModule.Animation; export function pageLoaded(args: observable.EventData) { page = args.object; page.bindingContext = vm; - panel1 = page.getViewById("panel1"); + panel = page.getViewById("panel1"); button1 = page.getViewById("button1"); button2 = page.getViewById("button2"); button3 = page.getViewById("button3"); } export function onSlideOut(args: observable.EventData) { - var animations: Array; + console.log("onSlideOut"); + var curve = page.android ? new android.view.animation.AccelerateInterpolator(1) : UIViewAnimationCurve.UIViewAnimationCurveEaseIn; - animations = new Array(); - animations.push({ target: button1, property: animation.Properties.translate, value: { x: -240, y: 0 }, duration: vm.duration, delay: 0, repeatCount: vm.repeatCount }); - animations.push({ target: button1, property: animation.Properties.scale, value: { x: 0.5, y: 0.5 }, duration: vm.duration, delay: 0, repeatCount: vm.repeatCount }); - animations.push({ target: button1, property: animation.Properties.opacity, value: 0, duration: vm.duration, delay: 0, repeatCount: vm.repeatCount }); + var buttonAnimations = [ + { target: button1, translate: { x: -240, y: 0 }, scale: { x: 0.5, y: 0.5 }, opacity: 0, duration: vm.duration, delay: 0, iterations: vm.iterations, curve: curve }, + { target: button2, translate: { x: -240, y: 0 }, scale: { x: 0.5, y: 0.5 }, opacity: 0, duration: vm.duration, delay: vm.duration, iterations: vm.iterations, curve: curve }, + { target: button3, translate: { x: -240, y: 0 }, scale: { x: 0.5, y: 0.5 }, opacity: 0, duration: vm.duration, delay: vm.duration * 2, iterations: vm.iterations, curve: curve }, + ] + buttonAnimation = new animationModule.Animation(buttonAnimations, vm.playSequentially); - animations.push({ target: button2, property: animation.Properties.translate, value: { x: -240, y: 0 }, duration: vm.duration, delay: vm.duration, repeatCount: vm.repeatCount }); - animations.push({ target: button2, property: animation.Properties.scale, value: { x: 0.5, y: 0.5 }, duration: vm.duration, delay: vm.duration, repeatCount: vm.repeatCount }); - animations.push({ target: button2, property: animation.Properties.opacity, value: 0, duration: vm.duration, delay: vm.duration, repeatCount: vm.repeatCount }); + panelAnimation = panel.createAnimation({ opacity: 0, scale: { x: 0.5, y: 0.5 }, rotate: -360, backgroundColor: new colorModule.Color("red"), duration: vm.duration, iterations: vm.iterations }); - animations.push({ target: button3, property: animation.Properties.translate, value: { x: -240, y: 0 }, duration: vm.duration, delay: vm.duration * 2, repeatCount: vm.repeatCount }); - animations.push({ target: button3, property: animation.Properties.scale, value: { x: 0, y: 0 }, duration: vm.duration, delay: vm.duration * 2, repeatCount: vm.repeatCount }); - animations.push({ target: button3, property: animation.Properties.opacity, value: 0, duration: vm.duration, delay: vm.duration * 2, repeatCount: vm.repeatCount }); - - configureAnimationCurve(animations, true); - - cancelToken = animation.start(animations, vm.playSequentially, (cancelled?: boolean) => { - if (cancelled) { - console.log("Buttons slide out animations cancelled"); - return; - } - console.log("Buttons slide out animations completed!"); - - animations = new Array(); - animations.push({ target: panel1, property: animation.Properties.scale, value: { x: 0, y: 0 }, duration: vm.duration, delay: 0, repeatCount: vm.repeatCount }); - animations.push({ target: panel1, property: animation.Properties.rotate, value: 1080, duration: vm.duration, delay: 0, repeatCount: vm.repeatCount }); - animations.push({ target: panel1, property: animation.Properties.backgroundColor, value: new colorModule.Color("red"), duration: vm.duration, delay: 0, repeatCount: vm.repeatCount }); - configureAnimationCurve(animations, true); - - cancelToken = animation.start(animations, vm.playSequentially,(cancelled?: boolean) => { - if (cancelled) { - console.log("Panel animation cancelled"); - return; - } - console.log("Panel animation completed!"); - }); - }); + buttonAnimation.play().finished + .then(() => panelAnimation.play().finished) + .catch((e) => console.log(e.message)); } export function onSlideIn(args: observable.EventData) { - var animations: Array; + console.log("onSlideIn"); + var curve = page.android ? new android.view.animation.DecelerateInterpolator(1) : UIViewAnimationCurve.UIViewAnimationCurveEaseOut; - animations = new Array(); - animations.push({ target: panel1, property: animation.Properties.scale, value: { x: 1, y: 1 }, duration: vm.duration, delay: 0, repeatCount: vm.repeatCount }); - animations.push({ target: panel1, property: animation.Properties.rotate, value: 0, duration: vm.duration, delay: 0, repeatCount: vm.repeatCount }); - animations.push({ target: panel1, property: animation.Properties.backgroundColor, value: new colorModule.Color("yellow"), duration: vm.duration, delay: 0, repeatCount: vm.repeatCount }); - configureAnimationCurve(animations, false); + panelAnimation = panel.createAnimation({ opacity: 1, scale: { x: 1, y: 1 }, rotate: 0, backgroundColor: new colorModule.Color("yellow"), duration: vm.duration, iterations: vm.iterations }); - cancelToken = animation.start(animations, vm.playSequentially,(cancelled?: boolean) => { - if (cancelled) { - console.log("Panel animation cancelled"); - return; - } - console.log("Panel animation completed!"); + var buttonAnimations = [ + { target: button3, translate: { x: 0, y: 0 }, scale: { x: 1, y: 1 }, opacity: 1, duration: vm.duration, delay: 0, iterations: vm.iterations, curve: curve }, + { target: button2, translate: { x: 0, y: 0 }, scale: { x: 1, y: 1 }, opacity: 1, duration: vm.duration, delay: vm.duration, iterations: vm.iterations, curve: curve }, + { target: button1, translate: { x: 0, y: 0 }, scale: { x: 1, y: 1 }, opacity: 1, duration: vm.duration, delay: vm.duration * 2, iterations: vm.iterations, curve: curve }, + ] + buttonAnimation = new animationModule.Animation(buttonAnimations, vm.playSequentially); - animations = new Array(); - animations.push({ target: button1, property: animation.Properties.translate, value: { x: 0, y: 0 }, duration: vm.duration, delay: 0, repeatCount: vm.repeatCount }); - animations.push({ target: button1, property: animation.Properties.scale, value: { x: 1, y: 1 }, duration: vm.duration, delay: 0, repeatCount: vm.repeatCount }); - animations.push({ target: button1, property: animation.Properties.opacity, value: 1, duration: vm.duration, delay: 0, repeatCount: vm.repeatCount }); - - animations.push({ target: button2, property: animation.Properties.translate, value: { x: 0, y: 0 }, duration: vm.duration, delay: vm.duration, repeatCount: vm.repeatCount }); - animations.push({ target: button2, property: animation.Properties.scale, value: { x: 1, y: 1 }, duration: vm.duration, delay: vm.duration, repeatCount: vm.repeatCount }); - animations.push({ target: button2, property: animation.Properties.opacity, value: 1, duration: vm.duration, delay: vm.duration, repeatCount: vm.repeatCount }); - - animations.push({ target: button3, property: animation.Properties.translate, value: { x: 0, y: 0 }, duration: vm.duration, delay: vm.duration * 2, repeatCount: vm.repeatCount }); - animations.push({ target: button3, property: animation.Properties.scale, value: { x: 1, y: 1 }, duration: vm.duration, delay: vm.duration * 2, repeatCount: vm.repeatCount }); - animations.push({ target: button3, property: animation.Properties.opacity, value: 1, duration: vm.duration, delay: vm.duration * 2, repeatCount: vm.repeatCount }); - - configureAnimationCurve(animations, false); - - cancelToken = animation.start(animations, vm.playSequentially,(cancelled?: boolean) => { - if (cancelled) { - console.log("Buttons slide in animations cancelled"); - return; - } - console.log("Buttons slide in animations completed!"); - }); - }); + panelAnimation.play().finished + .then(() => buttonAnimation.play().finished) + .catch((e) => console.log(e.message)); } -function configureAnimationCurve(animations: Array, slideOut: boolean) { - var i = 0; - var length = animations.length; - if (page.android) { - var interpolator = slideOut ? new android.view.animation.AccelerateInterpolator(1) : new android.view.animation.DecelerateInterpolator(1); - for (; i < length; i++) { - animations[i].androidInterpolator = interpolator; - } +export function onCancel(args: observable.EventData) { + console.log("onCancel"); + if (panelAnimation.isPlaying) { + panelAnimation.cancel(); } - else { - for (; i < length; i++) { - animations[i].iosUIViewAnimationCurve = slideOut ? UIViewAnimationCurve.UIViewAnimationCurveEaseIn : UIViewAnimationCurve.UIViewAnimationCurveEaseOut; - } + if (buttonAnimation.isPlaying) { + buttonAnimation.cancel(); } } -export function onStop(args: observable.EventData) { - cancelToken.cancel(); -} - export function onTap(args: observable.EventData) { console.log((args.object).text); +} + +export function onSingle(args: observable.EventData) { + console.log("onSingle"); + button1.animate({ + opacity: 0.75, + backgroundColor: new colorModule.Color("Red"), + translate: { x: 100, y: 100 }, + scale: { x: 2, y: 2 }, + rotate: 180, + duration: vm.duration, + delay: 0, + iterations: vm.iterations, + curve: button1.ios ? UIViewAnimationCurve.UIViewAnimationCurveEaseIn : new android.view.animation.AccelerateInterpolator(1), + }) + .then(() => console.log("Animation finished")) + .catch((e) => console.log(e.message)); } \ No newline at end of file diff --git a/apps/animations/main-page.xml b/apps/animations/main-page.xml index 1e2ee8387..8a132ce87 100644 --- a/apps/animations/main-page.xml +++ b/apps/animations/main-page.xml @@ -5,8 +5,8 @@