更改图层/视图属性时禁用动画? [英] Disabling animation when changing layer/view properties?

查看:78
本文介绍了更改图层/视图属性时禁用动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过向UIView添加多个图层来编写一种动画。这些图层应设置为脚本可见或不可见。

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.

有没有办法禁用此转换?我现在没有使用任何CoreAnimation调用,只是属性本身(参见代码)。

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).

推荐答案

更改其中一个图层的动画属性创建Apple的文档称为隐式动画。

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

引用关于主题的Xcode文档:

To quote the Xcode docs on the subject:


Core Animation的隐式动画模型假设对
可动画图层属性的所有更改都应该是渐进的和异步的。
动态动画场景可以在没有明确
动画图层的情况下实现。更改可动画图层属性
的值会导致图层隐式地将旧值
的更改设置为新值。当动画正在进行中时,设置新的
目标值会导致动画从当前状态转换到新目标值

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.

在幕后,系统会生成一个CAAnimation来进行更改。

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

正如另一张海报所说,你可以使用setAnimationDuration来制作动画瞬间发生,具有关闭动画的效果。我怀疑系统仍会生成动画。

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];



编辑:



在Swift 3中,此代码如下所示:

In Swift 3, this code would look like this:

CATransaction.begin()
CATransaction.setDisableActions(true)
//layer changes
CATransaction.commit()

这篇关于更改图层/视图属性时禁用动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆