mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Code refactoring
This commit is contained in:
@@ -13,6 +13,16 @@ var FLT_MAX = 340282346638528859811704183484516925440.000000;
|
|||||||
|
|
||||||
declare var CASpringAnimation:any;
|
declare var CASpringAnimation:any;
|
||||||
|
|
||||||
|
class AnimationInfo
|
||||||
|
{
|
||||||
|
public propertyNameToAnimate: string;
|
||||||
|
public fromValue: any;
|
||||||
|
public toValue: any;
|
||||||
|
public duration: number;
|
||||||
|
public repeatCount: number;
|
||||||
|
public delay: number;
|
||||||
|
}
|
||||||
|
|
||||||
class AnimationDelegateImpl extends NSObject {
|
class AnimationDelegateImpl extends NSObject {
|
||||||
|
|
||||||
public nextAnimation: Function;
|
public nextAnimation: Function;
|
||||||
@@ -59,42 +69,19 @@ class AnimationDelegateImpl extends NSObject {
|
|||||||
(<any>this._propertyAnimation.target)._resumePresentationLayerUpdates();
|
(<any>this._propertyAnimation.target)._resumePresentationLayerUpdates();
|
||||||
}
|
}
|
||||||
|
|
||||||
public animationDidStopFinished(anim: CAAnimation, flag: boolean): void {
|
public animationDidStopFinished(anim: CAAnimation, finished: boolean): void {
|
||||||
if (this._finishedCallback) {
|
if (this._finishedCallback) {
|
||||||
var cancelled = !flag;
|
this._finishedCallback(!finished);
|
||||||
// This could either be the master finishedCallback or an nextAnimationCallback depending on the playSequentially argument values.
|
|
||||||
this._finishedCallback(cancelled);
|
|
||||||
}
|
}
|
||||||
if (!flag) {
|
if (!finished) {
|
||||||
if ((<any>this._propertyAnimation)._propertyResetCallback) {
|
if ((<any>this._propertyAnimation)._propertyResetCallback) {
|
||||||
(<any>this._propertyAnimation)._propertyResetCallback((<any>this._propertyAnimation)._originalValue);
|
(<any>this._propertyAnimation)._propertyResetCallback((<any>this._propertyAnimation)._originalValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (flag && this.nextAnimation) {
|
if (finished && this.nextAnimation) {
|
||||||
this.nextAnimation();
|
this.nextAnimation();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// public uiview_animationWillStart(animationID: string, context: any): void {
|
|
||||||
// trace.write("AnimationDelegateImpl.animationWillStart, animationID: " + animationID, trace.categories.Animation);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// public uiview_animationDidStop(animationID: string, finished: boolean, context: any): void {
|
|
||||||
// trace.write("AnimationDelegateImpl.animationDidStop, animationID: " + animationID + ", finished: " + finished, trace.categories.Animation);
|
|
||||||
// if (this._finishedCallback) {
|
|
||||||
// var cancelled = !finished;
|
|
||||||
// // This could either be the master finishedCallback or an nextAnimationCallback depending on the playSequentially argument values.
|
|
||||||
// this._finishedCallback(cancelled);
|
|
||||||
// }
|
|
||||||
// if (!finished) {
|
|
||||||
// if ((<any>this._propertyAnimation)._propertyResetCallback) {
|
|
||||||
// (<any>this._propertyAnimation)._propertyResetCallback((<any>this._propertyAnimation)._originalValue);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// if (finished && this.nextAnimation) {
|
|
||||||
// this.nextAnimation();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Animation extends common.Animation implements definition.Animation {
|
export class Animation extends common.Animation implements definition.Animation {
|
||||||
@@ -105,10 +92,8 @@ export class Animation extends common.Animation implements definition.Animation
|
|||||||
|
|
||||||
public play(): Promise<void> {
|
public play(): Promise<void> {
|
||||||
var animationFinishedPromise = super.play();
|
var animationFinishedPromise = super.play();
|
||||||
|
|
||||||
this._finishedAnimations = 0;
|
this._finishedAnimations = 0;
|
||||||
this._cancelledAnimations = 0;
|
this._cancelledAnimations = 0;
|
||||||
|
|
||||||
this._iOSAnimationFunction();
|
this._iOSAnimationFunction();
|
||||||
return animationFinishedPromise;
|
return animationFinishedPromise;
|
||||||
}
|
}
|
||||||
@@ -161,11 +146,12 @@ export class Animation extends common.Animation implements definition.Animation
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
this._iOSAnimationFunction = Animation._createiOSAnimationFunction(this._mergedPropertyAnimations, 0, this._playSequentially, animationFinishedCallback, this);
|
this._iOSAnimationFunction = Animation._createiOSAnimationFunction(this._mergedPropertyAnimations, 0, this._playSequentially, animationFinishedCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static _createiOSAnimationFunction(propertyAnimations: Array<common.PropertyAnimation>, index: number, playSequentially: boolean, finishedCallback: (cancelled?: boolean) => void, that:Animation): Function {
|
private static _createiOSAnimationFunction(propertyAnimations: Array<common.PropertyAnimation>, index: number, playSequentially: boolean, finishedCallback: (cancelled?: boolean) => void): Function {
|
||||||
return (cancelled?: boolean) => {
|
return (cancelled?: boolean) => {
|
||||||
|
|
||||||
if (cancelled && finishedCallback) {
|
if (cancelled && finishedCallback) {
|
||||||
trace.write("Animation " + (index - 1).toString() + " was cancelled. Will skip the rest of animations and call finishedCallback(true).", trace.categories.Animation);
|
trace.write("Animation " + (index - 1).toString() + " was cancelled. Will skip the rest of animations and call finishedCallback(true).", trace.categories.Animation);
|
||||||
finishedCallback(cancelled);
|
finishedCallback(cancelled);
|
||||||
@@ -173,11 +159,25 @@ export class Animation extends common.Animation implements definition.Animation
|
|||||||
}
|
}
|
||||||
|
|
||||||
var animation = propertyAnimations[index];
|
var animation = propertyAnimations[index];
|
||||||
|
var args = Animation._getNativeAnimationArguments(animation);
|
||||||
|
|
||||||
|
if (animation.curve === enums.AnimationCurve.spring) {
|
||||||
|
Animation._createNativeSpringAnimation(propertyAnimations, index, playSequentially, args, animation, finishedCallback);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Animation._createNativeAnimation(propertyAnimations, index, playSequentially, args, animation, finishedCallback);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static _getNativeAnimationArguments(animation: common.PropertyAnimation): AnimationInfo {
|
||||||
|
|
||||||
var nativeView = <UIView>animation.target._nativeView;
|
var nativeView = <UIView>animation.target._nativeView;
|
||||||
|
var presentationLayer = nativeView.layer.presentationLayer();
|
||||||
var propertyNameToAnimate = animation.property;
|
var propertyNameToAnimate = animation.property;
|
||||||
var value = animation.value;
|
var value = animation.value;
|
||||||
var originalValue;
|
var originalValue;
|
||||||
var presentationLayer = nativeView.layer.presentationLayer();
|
|
||||||
var tempRotate = animation.target.rotate * Math.PI / 180;
|
var tempRotate = animation.target.rotate * Math.PI / 180;
|
||||||
var abs
|
var abs
|
||||||
|
|
||||||
@@ -186,9 +186,6 @@ export class Animation extends common.Animation implements definition.Animation
|
|||||||
(<any>animation)._originalValue = animation.target.backgroundColor;
|
(<any>animation)._originalValue = animation.target.backgroundColor;
|
||||||
(<any>animation)._propertyResetCallback = (value) => { animation.target.backgroundColor = value };
|
(<any>animation)._propertyResetCallback = (value) => { animation.target.backgroundColor = value };
|
||||||
if (presentationLayer != null) {
|
if (presentationLayer != null) {
|
||||||
if (nativeView instanceof UILabel) {
|
|
||||||
nativeView.backgroundColor = UIColor.clearColor();
|
|
||||||
}
|
|
||||||
originalValue = presentationLayer.backgroundColor;
|
originalValue = presentationLayer.backgroundColor;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -272,28 +269,78 @@ export class Animation extends common.Animation implements definition.Animation
|
|||||||
throw new Error("Cannot animate " + animation.property);
|
throw new Error("Cannot animate " + animation.property);
|
||||||
}
|
}
|
||||||
|
|
||||||
var nativeAnimation;
|
|
||||||
var isSpring = false;
|
|
||||||
|
|
||||||
if (animation.curve === enums.AnimationCurve.spring) {
|
|
||||||
|
|
||||||
// nativeAnimation = CASpringAnimation.animationWithKeyPath(propertyNameToAnimate);
|
|
||||||
// nativeAnimation.duration = nativeAnimation.settlingDuration;
|
|
||||||
// nativeAnimation.damping = 3;
|
|
||||||
|
|
||||||
var duration = 0.3;
|
var duration = 0.3;
|
||||||
if (animation.duration !== undefined) {
|
if (animation.duration !== undefined) {
|
||||||
duration = animation.duration / 1000.0;
|
duration = animation.duration / 1000.0;
|
||||||
}
|
}
|
||||||
var delay = 0;
|
|
||||||
if (animation.delay !== undefined) {
|
var delay = undefined;
|
||||||
delay = CACurrentMediaTime() + (animation.delay / 1000.0);
|
if (animation.delay) {
|
||||||
|
delay = animation.delay / 1000.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var repeatCount = undefined;
|
||||||
|
if (animation.iterations !== undefined) {
|
||||||
|
if (animation.iterations === Number.POSITIVE_INFINITY) {
|
||||||
|
repeatCount = FLT_MAX;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
repeatCount = animation.iterations - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
propertyNameToAnimate: propertyNameToAnimate,
|
||||||
|
fromValue: originalValue,
|
||||||
|
toValue: value,
|
||||||
|
duration: duration,
|
||||||
|
repeatCount: repeatCount,
|
||||||
|
delay: delay
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static _createNativeAnimation(propertyAnimations: Array<common.PropertyAnimation>, index: number, playSequentially: boolean, args: AnimationInfo, animation: common.PropertyAnimation, finishedCallback: (cancelled?: boolean) => void) {
|
||||||
|
|
||||||
|
var nativeView = <UIView>animation.target._nativeView;
|
||||||
|
var nativeAnimation = CABasicAnimation.animationWithKeyPath(args.propertyNameToAnimate);
|
||||||
|
nativeAnimation.fromValue = args.fromValue;
|
||||||
|
nativeAnimation.toValue = args.toValue;
|
||||||
|
nativeAnimation.duration = args.duration;
|
||||||
|
if (args.repeatCount !== undefined) {
|
||||||
|
nativeAnimation.repeatCount = args.repeatCount;
|
||||||
|
}
|
||||||
|
if (args.delay !== undefined) {
|
||||||
|
nativeAnimation.beginTime = CACurrentMediaTime() + args.delay;
|
||||||
|
}
|
||||||
|
if (animation.curve !== undefined) {
|
||||||
|
nativeAnimation.timingFunction = animation.curve;
|
||||||
|
}
|
||||||
|
|
||||||
|
var animationDelegate = AnimationDelegateImpl.initWithFinishedCallback(finishedCallback, animation);
|
||||||
|
nativeAnimation.setValueForKey(animationDelegate, "delegate");
|
||||||
|
|
||||||
|
nativeView.layer.addAnimationForKey(nativeAnimation, args.propertyNameToAnimate);
|
||||||
|
|
||||||
|
var callback = undefined;
|
||||||
|
if (index+1 < propertyAnimations.length) {
|
||||||
|
callback = Animation._createiOSAnimationFunction(propertyAnimations, index+1, playSequentially, finishedCallback);
|
||||||
|
if (!playSequentially) {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
animationDelegate.nextAnimation = callback;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static _createNativeSpringAnimation(propertyAnimations: Array<common.PropertyAnimation>, index: number, playSequentially: boolean, args: AnimationInfo, animation: common.PropertyAnimation, finishedCallback: (cancelled?: boolean) => void) {
|
||||||
|
|
||||||
|
var nativeView = <UIView>animation.target._nativeView;
|
||||||
|
|
||||||
var callback = undefined;
|
var callback = undefined;
|
||||||
var nextAnimation;
|
var nextAnimation;
|
||||||
if (index + 1 < propertyAnimations.length) {
|
if (index + 1 < propertyAnimations.length) {
|
||||||
callback = Animation._createiOSAnimationFunction(propertyAnimations, index + 1, playSequentially, finishedCallback, that);
|
callback = Animation._createiOSAnimationFunction(propertyAnimations, index + 1, playSequentially, finishedCallback);
|
||||||
if (!playSequentially) {
|
if (!playSequentially) {
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
@@ -302,37 +349,36 @@ export class Animation extends common.Animation implements definition.Animation
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
UIView.animateWithDurationDelayUsingSpringWithDampingInitialSpringVelocityOptionsAnimationsCompletion(duration, delay, 0.2, 0,
|
var delay = 0;
|
||||||
|
if (args.delay) {
|
||||||
|
delay = args.delay;
|
||||||
|
}
|
||||||
|
|
||||||
|
UIView.animateWithDurationDelayUsingSpringWithDampingInitialSpringVelocityOptionsAnimationsCompletion(args.duration, delay, 0.2, 0,
|
||||||
UIViewKeyframeAnimationOptions.UIViewKeyframeAnimationOptionCalculationModeLinear,
|
UIViewKeyframeAnimationOptions.UIViewKeyframeAnimationOptionCalculationModeLinear,
|
||||||
() => {
|
() => {
|
||||||
if (animation.iterations !== undefined) {
|
if (args.repeatCount !== undefined) {
|
||||||
if (animation.iterations === Number.POSITIVE_INFINITY) {
|
UIView.setAnimationRepeatCount(args.repeatCount);
|
||||||
UIView.setAnimationRepeatCount(FLT_MAX);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
UIView.setAnimationRepeatCount(animation.iterations - 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (animation.property) {
|
switch (animation.property) {
|
||||||
case common.Properties.backgroundColor:
|
case common.Properties.backgroundColor:
|
||||||
animation.target.backgroundColor = value;
|
animation.target.backgroundColor = args.toValue;
|
||||||
break;
|
break;
|
||||||
case common.Properties.opacity:
|
case common.Properties.opacity:
|
||||||
animation.target.opacity = value;
|
animation.target.opacity = args.toValue;
|
||||||
break;
|
break;
|
||||||
case common.Properties.rotate:
|
case common.Properties.rotate:
|
||||||
nativeView.layer.setValueForKey(value, propertyNameToAnimate);
|
nativeView.layer.setValueForKey(args.toValue, args.propertyNameToAnimate);
|
||||||
break;
|
break;
|
||||||
case _transform:
|
case _transform:
|
||||||
(<any>animation)._originalValue = nativeView.layer.transform;
|
(<any>animation)._originalValue = nativeView.layer.transform;
|
||||||
nativeView.layer.setValueForKey(value, propertyNameToAnimate);
|
nativeView.layer.setValueForKey(args.toValue, args.propertyNameToAnimate);
|
||||||
(<any>animation)._propertyResetCallback = function (value) {
|
(<any>animation)._propertyResetCallback = function (value) {
|
||||||
nativeView.layer.transform = value;
|
nativeView.layer.transform = value;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}, function (finished:boolean) {
|
}, function (finished:boolean) {
|
||||||
if (finished) {
|
if (finished) {
|
||||||
if (animation.property === _transform) {
|
if (animation.property === _transform) {
|
||||||
@@ -359,53 +405,6 @@ export class Animation extends common.Animation implements definition.Animation
|
|||||||
nextAnimation();
|
nextAnimation();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
nativeAnimation = CABasicAnimation.animationWithKeyPath(propertyNameToAnimate);
|
|
||||||
}
|
|
||||||
nativeAnimation.fromValue = originalValue;
|
|
||||||
nativeAnimation.toValue = value;
|
|
||||||
if (animation.duration !== undefined) {
|
|
||||||
nativeAnimation.duration = animation.duration / 1000.0;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
nativeAnimation.duration = 0.3;
|
|
||||||
}
|
|
||||||
if (animation.delay !== undefined) {
|
|
||||||
nativeAnimation.beginTime = CACurrentMediaTime() + (animation.delay / 1000.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (animation.iterations !== undefined) {
|
|
||||||
if (animation.iterations === Number.POSITIVE_INFINITY) {
|
|
||||||
nativeAnimation.repeatCount = FLT_MAX;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
nativeAnimation.repeatCount = animation.iterations - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!isSpring && animation.curve !== undefined) {
|
|
||||||
trace.write("The animation curve is " + animation.curve, trace.categories.Animation);
|
|
||||||
nativeAnimation.timingFunction = animation.curve;
|
|
||||||
}
|
|
||||||
|
|
||||||
var animationDelegate: AnimationDelegateImpl = AnimationDelegateImpl.initWithFinishedCallback(finishedCallback, animation);
|
|
||||||
nativeAnimation.setValueForKey(animationDelegate, "delegate");
|
|
||||||
|
|
||||||
nativeView.layer.addAnimationForKey(nativeAnimation, propertyNameToAnimate);
|
|
||||||
|
|
||||||
var callback = undefined;
|
|
||||||
if (index+1 < propertyAnimations.length) {
|
|
||||||
callback = Animation._createiOSAnimationFunction(propertyAnimations, index+1, playSequentially, finishedCallback, that);
|
|
||||||
if (!playSequentially) {
|
|
||||||
callback();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
animationDelegate.nextAnimation = callback;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static _createNativeAffineTransform(animation: common.PropertyAnimation): CATransform3D {
|
private static _createNativeAffineTransform(animation: common.PropertyAnimation): CATransform3D {
|
||||||
|
|||||||
Reference in New Issue
Block a user