Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling animation when changing layer/view properties?

I composed a kind of animation by adding several layers to a UIView. Those layers shall be set visible or invisible by a script.

The script is based on objects that implement a protocol:

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

an in a timer I iterate through the step objects:

NSEnumerator* enumerator = [ScriptObjects objectEnumerator];
id obj;

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

One 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 the changes in the step results in a transition.

Is there a way to disable this transition? I am not using any CoreAnimation call at all right now, just the properties itself (see code).

like image 261
Zuppa Avatar asked Apr 14 '12 17:04

Zuppa


People also ask

How do I Turn Off visual effects and animations?

It will be under the Visual Effects tab, which the window opens to by default. For complete control, customize which animations (and visual effects) you want to see. Click the radio button next to "Custom:". Right below, uncheck the boxes of effects that you would like to disable, and put a check in the boxes of the ones you would like to enable.

How do I change the animation settings in Windows 10?

Press the Settings... button under the "Performance" section of the window. The button is located under the Advanced tab of the System Properties window. Select the settings you want. It will be under the Visual Effects tab, which the window opens to by default. For complete control, customize which animations (and visual effects) you want to see.

Will I be able to disable animations on my computer?

Not all animations you see while using your computer will be disabled. For example, animations on websites you visit will not be affected. Also, classic desktop applications (programs) that have animations (which is rare) will continue to play.

What are the negative effects of animations?

People who need to remain focused on a task or people with certain mental/visual disabilities may be negatively affected by animations. Additionally, they slow down devices with lower system specs.


2 Answers

Changing one of a layer's "animatable" properties creates what Apple's docs calls an implicit animation.

To quote the Xcode docs on the subject:

Core Animation’s implicit animation model assumes that all changes to animatable layer properties should be gradual and asynchronous. Dynamically animated scenes can be achieved without ever explicitly animating layers. Changing the value of an animatable layer property causes the layer to implicitly animate the change from the old value to the new value. While an animation is in-flight, setting a new target value causes the animation transition to the new target value from its current state.

Under the covers, the system generates a CAAnimation that makes the change.

As the other poster said, you can use setAnimationDuration to make the animation happen in an instant, which has the effect of turning animations off. I suspect that the system still generates an animation however.

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 would look like this:

CATransaction.begin()
CATransaction.setDisableActions(true)
//layer changes
CATransaction.commit()
like image 191
Duncan C Avatar answered Sep 20 '22 12:09

Duncan C


Just wrap the code where you are making the change.

[CATransaction begin];
[CATransaction setAnimationDuration:0];

[thelayer setAlpha:0];

[CATransaction commit];
like image 30
mprivat Avatar answered Sep 20 '22 12:09

mprivat