且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在复杂的层次结构上下同步CALayer和UIView动画

更新时间:2022-03-26 22:19:49

有一些选择要考虑:


  1. 对于UIView过渡,您可以传递 UIViewAnimationOptionLayoutSubviews 选项。这将为子视图之间的更改设置动画,在您刚刚更改了框架的视图上调用 layoutSubviews 之前和之后。

  2. 而不是使用 layoutSubviews ,您可以在 setBounds: setFrame:上覆盖您的 UIView CALayer 子类。这样,如果在动画块中调用它们,则子视图将与超级视图一起进行动画处理。如果未在动画块中调用它们,它们将立即更新。

  1. For UIView transitions, you can pass the UIViewAnimationOptionLayoutSubviews option. This will animate the changes between subviews before and after calling layoutSubviews on the view whose frame you just changed.
  2. Instead of using layoutSubviews at all, you could override setBounds: or setFrame: on your UIView and CALayer subclasses. This way, if they're called within an animation block, the subviews will animate together with the superview. If they're not called within an animation block, they'll update instantly.




我的担心是并不是在每种情况下都需要对每个属性进行动画处理(如在辅助视图中一样)-我已经打开/关闭setDisableActions以强制/拒绝某些属性动画。

My fear is that not every property needs to be animated in every case (like in the auxiliary views) - I already am flipping setDisableActions on/off to force/deny some property animations.

通常,如果您希望将其动画化,请将其放在动画块中,否则,则不要。显然,它会变得比这更复杂,这就是为什么苹果有时会有一个带有 animated 参数的二传手(例如 setSelected:animated:)。

Generally, if you want it animated, put it in an animation block, and if you don't, don't. Obviously, it can get more complex than this, which is why Apple sometimes has a setter with an animated argument (like setSelected:animated:).

如果您有时会启用属性,有时会关闭属性,请自己遵循这种模式。一种可能的实现方式:

If you have sometimes on, sometimes off properties, follow this pattern yourself. One possible implementation:

- (void) setNumberOfWidgets:(int)widgetCount animated:(BOOL)animated {
    BOOL oldAnimationValue = [UIView areAnimationsEnabled];

    if (!animated) {
        [UIView setAnimationsEnabled:NO];
    }

    // a related property which may or may not cause an animation
    self.someOtherProperty = someValue;

    if (!animated) {
        [UIView setAnimationsEnabled:oldAnimationValue];
    }
}




我的问题是是可维护性之一。

My question is really one of maintainability.

是的,听起来您正在考虑某种巨型对象,可以为您管理所有动画可能性。抵制这种诱惑。让每个视图负责为其自己的子视图设置动画,并 tell 一个视图(不要问)是否应对这些更改进行动画处理。

Yes, it sounds like you're thinking about some kind of giant object that manages all the animation possibilities for you. Resist this temptation. Let each view be responsible for animating its own subviews, and tell a view (don't let it ask) whether or not these changes should be animated.


我认为UIViewControllerTransitionCoordinator没用了,因为我需要做所有这些动画来响应用户操作,而不仅仅是响应外部事物,例如旋转或帧更改。

I think that UIViewControllerTransitionCoordinator is out because I need to do all of these animations in response to user actions, not just external things, like rotations or frame changes.

是的,这是正确的。请勿为此使用 UIViewControllerTransitionCoordinator

Yes, that's correct. Don't use UIViewControllerTransitionCoordinator for this.