From e3bdcdc3f6349c6e68ef22f4560193cf53e03275 Mon Sep 17 00:00:00 2001 From: Tsvetan Raikov Date: Fri, 5 Feb 2016 14:21:22 +0200 Subject: [PATCH] Fixed: Setting and instantly animating a property fails for iOS --- ui/animation/animation.ios.ts | 6 +++++ ui/core/view.ios.ts | 47 +++++++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/ui/animation/animation.ios.ts b/ui/animation/animation.ios.ts index 9fd3442ce..c6d4295c2 100644 --- a/ui/animation/animation.ios.ts +++ b/ui/animation/animation.ios.ts @@ -29,6 +29,9 @@ class AnimationDelegateImpl extends NSObject { var nativeView = this._propertyAnimation.target._nativeView; var propertyNameToAnimate = this._propertyAnimation.property; var value = this._propertyAnimation.value; + + (this._propertyAnimation.target)._suspendPresentationLayerUpdates(); + switch (this._propertyAnimation.property) { case common.Properties.backgroundColor: this._propertyAnimation.target.backgroundColor = value; @@ -50,6 +53,9 @@ class AnimationDelegateImpl extends NSObject { } break; } + + (this._propertyAnimation.target)._resumePresentationLayerUpdates(); + } public animationDidStopFinished(anim: CAAnimation, flag: boolean): void { diff --git a/ui/core/view.ios.ts b/ui/core/view.ios.ts index bcebdc032..fdbd8af90 100644 --- a/ui/core/view.ios.ts +++ b/ui/core/view.ios.ts @@ -71,6 +71,7 @@ export class View extends viewCommon.View { private _hasTransfrom = false; private _privateFlags: number; private _cachedFrame: CGRect; + private _suspendCATransaction = false; constructor() { super(); @@ -92,7 +93,7 @@ export class View extends viewCommon.View { public onLoaded() { super.onLoaded(); - // TODO: It is very late to work with options here in the onLoaded method. + // TODO: It is very late to work with options here in the onLoaded method. // We should not do anything that affects UI AFTER the widget has been loaded. utils.copyFrom(this._options, this); delete this._options; @@ -126,7 +127,7 @@ export class View extends viewCommon.View { var measureSpecsChanged = this._setCurrentMeasureSpecs(widthMeasureSpec, heightMeasureSpec); var forceLayout = (this._privateFlags & PFLAG_FORCE_LAYOUT) === PFLAG_FORCE_LAYOUT; if (forceLayout || measureSpecsChanged) { - + // first clears the measured dimension flag this._privateFlags &= ~PFLAG_MEASURED_DIMENSION_SET; @@ -223,7 +224,7 @@ export class View extends viewCommon.View { } // This is done because when rotated in iOS7 there is rotation applied on the first subview on the Window which is our frame.nativeView.view. - // If we set it it should be transformed so it is correct. + // If we set it it should be transformed so it is correct. // When in landscape in iOS 7 there is transformation on the first subview of the window so we set frame to its subview. // in iOS 8 we set frame to subview again otherwise we get clipped. var nativeView: UIView; @@ -285,7 +286,7 @@ export class View extends viewCommon.View { return true; } - + return false; } @@ -293,8 +294,23 @@ export class View extends viewCommon.View { if (this._nativeView) { this._nativeView.removeFromSuperview(); } - } -} + } + + // By default we update the view's presentation layer when setting backgroundColor and opacity properties. + // This is done by calling CATransaction begin and commit methods. + // This action should be disabled when updating those properties during an animation. + public _suspendPresentationLayerUpdates() { + this._suspendCATransaction = true; + } + + public _resumePresentationLayerUpdates() { + this._suspendCATransaction = false; + } + + public _isPresentationLayerUpdateSuspeneded() { + return this._suspendCATransaction; + } + } export class CustomLayoutView extends View { @@ -336,7 +352,14 @@ export class ViewStyler implements style.Styler { var nativeView: UIView = view._nativeView; if (nativeView) { ensureBackground(); + var updateSuspended = view._isPresentationLayerUpdateSuspeneded(); + if (!updateSuspended) { + CATransaction.begin(); + } nativeView.backgroundColor = background.ios.createBackgroundUIColor(view); + if (!updateSuspended) { + CATransaction.commit(); + } } } @@ -374,7 +397,15 @@ export class ViewStyler implements style.Styler { private static setOpacityProperty(view: View, newValue: any) { var nativeView: UIView = view._nativeView; if (nativeView) { - return nativeView.alpha = newValue; + var updateSuspended = view._isPresentationLayerUpdateSuspeneded(); + if (!updateSuspended) { + CATransaction.begin(); + } + var alpha = nativeView.alpha = newValue; + if (!updateSuspended) { + CATransaction.commit(); + } + return alpha; } } @@ -477,4 +508,4 @@ export class ViewStyler implements style.Styler { } } -ViewStyler.registerHandlers(); \ No newline at end of file +ViewStyler.registerHandlers();