简体   繁体   English

使用CALayer作为子视图动画UIView框架

[英]Animate UIView frame with CALayer as subview

What I have: 我有的:

I have aa hierarchy of UIView's and a CAGradientLayer that I use to set the "background colour" of this hierarchy. 我有一个UIView's层次结构和一个CAGradientLayer ,我用它来设置这个层次结构的“背景颜色”。 So I have something like this: 所以我有这样的事情:

ViewA -> ViewB -> CAGradientLayer ViewA - > ViewB - > CAGradientLayer

What I am doing: 我在做什么:

At one point, I want to animate the frame (only the height of it) of the ViewA. 有一次,我想为ViewA的frame (只有它的高度)设置动画。 So it would be something like this: 所以它会是这样的:

[UIView animateWithDuration:timeInterval
                      delay:0
                    options:UIViewAnimationOptionCurveEaseIn
                 animations:^{
                              viewA.frame = newFrame;
}];

The issue: 问题:

The problem with this is that the CAGradientLayer won't animate it's frame, it simply change it's frame, and then the viewA starts animating as intended (this is clearly visible by setting the simulator to slow-motion animations). 这个问题是CAGradientLayer不会为它的框架设置动画,它只是改变它的框架,然后viewA开始按预期动画(通过将模拟器设置为慢动作动画可以清楚地看到这一点)。

What I tried: 我尝试了什么:

I tried to do what Apple recommends here , by doing it inside the UIView animation block, but it didn't actually work, because I am still animating the frame here: 我尝试在这里做了苹果推荐的东西 ,在UIView动画块里面做了,但它实际上没有用,因为我仍然在这里动画框架:

viewA.frame = newFrame;

I also understand that the I should animate the bounds, and not the frame of the CALayer , the problem is that, how can I animate the bounds and when animating the frame of the ViewA leave that CALayer out of the animation? 我也明白我应该为边界设置动画,而不是CALayer的框架,问题在于,我如何设置边界的动画,并且当动画ViewA的框架动画时,将CALayer从动画中移出?

What I want: 我想要的是:

I just want to animate the change of its height, so I would see both the viewA and the CAGradientLayer animating at the same time. 我只想动画它的高度变化,所以我会同时看到viewACAGradientLayer动画。

You'll have to understand that the UIView and CALayer hierarchy are different. 您必须了解UIViewCALayer层次结构是不同的。 Layout changes and animations operating on a UIView will not automatically affect the CALayer hierarchy. UIView上运行的布局更改和动画不会自动影响CALayer层次结构。

So if you want changes applied to your view affect a layer you'll have to update that yourself. 因此,如果您希望应用于视图的更改影响图层,则必须自行更新。

Your code is also missing details. 您的代码也缺少详细信息。 For example you claim you see your gradient layer change it's frame, but you don't show where you are changing it. 例如,您声称您看到渐变图层更改了它的框架,但您没有显示更改它的位置。 Did you overwrite layoutSubviews somewhere to update it's frame ? 你有没有覆盖layoutSubviews来更新它的frame

By default updating a layer frame will trigger it's implicit animations for the position & bounds properties. 默认情况下,更新图层frame将触发它的positionbounds属性的隐式动画。 To change the speed of the animation you could use: 要更改动画的速度,您可以使用:

[CATransaction begin];
[CATransaction setAnimationDuration:10.0];
_myLayer.frame = newFrame;
[CATransaction commit]; 

First off, read this . 首先,请阅读此内容

Try encapsulating your view animation code along with layer animation explicitly. 尝试显式封装视图动画代码和图层动画。 Do not expect layer to animate automatically. 不要指望图层自动动画。 Since you are dealing with positioning, you would need to change graidentlayer.bounds . 由于您正在处理定位,因此您需要更改graidentlayer.bounds

If that does not work, try this before your animation code: 如果这不起作用,请在动画代码之前尝试:

[CATransaction begin];    
[CATransaction setDisableActions: NO];    
[CATransaction commit];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM