Disable animation when changing layer / view properties?

I created the animation by adding several layers to the UIView. These layers must be set visible or invisible using a script.

The script is based on objects that implement the protocol:

// the general protocol for a step @protocol ActionStep -(void) applyForTime:(int)playtime; -(void) reset; @end 

a in the timer I repeat the step objects:

 NSEnumerator* enumerator = [ScriptObjects objectEnumerator]; id obj; while ( obj = [enumerator nextObject] ) { id <ActionStep> step = obj; [step applyForTime:currentmilliseconds]; } 

The script object is this object:

 @interface LayerStep : NSObject <ActionStep> { int mTimeOffset; CGPoint mOffset; float mAlpha; LayerObject* mTheLayer; bool mPrepared; } -(id)initWithLayerObject: (LayerObject*) theLayer Milliseconds:(int) milliseconds Offset:(CGPoint) offset Alpha:(float)alpha; @end 

and finally, I implement the protocol in the layer:

 -(void) applyForTime:(int)playtime { if ( mPrepared ) // has the step already been executed? { if ( playtime >= mTimeOffset ) { [mTheLayer setAlpha:mAlpha]; // AssignedLayer.opacity = alpha; [mTheLayer setPosition:mOffset]; // AssignedLayer.position = offset; mPrepared = false; } } } 

Applying changes in a step results in a transition.

Is there any way to disable this transition? I don’t use the CoreAnimation call right now, only the properties themselves (see Code).

+7
source share
2 answers

Changing one of the properties of the "animatable" layer makes Apple docs invoke implicit animation.

To quote Xcode docs by topic:

The Core Animations implicit animation model assumes that all changes to the animated properties of a layer must be gradual and asynchronous. Dynamically animated scenes can be achieved without much animated layers. Changing the property value of an animated layer causes the layer to implicitly animate the change from the old value to the new value. While the animation is in flight, setting a new target value causes the animation to transition to the new target value from its current state.

Under the covers, the system generates CAAnimation, which makes the changes.

As another poster said, you can use setAnimationDuration to make the animation happen in an instant, which causes the animation to turn off. I suspect the system is still generating animations.

The official way to turn off implicit layer animations is to use

 [CATransaction begin]; [CATransaction setDisableActions: YES]; //layer changes [CATransaction commit]; 

Edit:

In Swift 3, this code will look like this:

 CATransaction.begin() CATransaction.setDisableActions(true) //layer changes CATransaction.commit() 
+33
source

Just wrap the code in which you make the changes.

 [CATransaction begin]; [CATransaction setAnimationDuration:0]; [thelayer setAlpha:0]; [CATransaction commit]; 
+2
source

All Articles