如何在UIViewanimation过程中animationUIView图层的子图层?

在视图的UIViewanimation中,通过在[UIView animateWithDuration:delay:options:animations:completion:]的options参数中包含UIViewAnimationOptionLayoutSubviews ,可以为其子视图设置[UIView animateWithDuration:delay:options:animations:completion:] 。 然而,当我们不是某个视图的背景层的时候,我找不到一个方法来为它的子层设置animation。 他们只是跳到适当的位置,以配合视图的新界限。 由于我正在使用图层而不是视图,看起来像我不得不使用核心animation而不是UIViewanimation,但我不知道如何(和何时)做到这一点,使图层animation将匹配视图animation。

这是我的基本问题。 如果你想知道我想要完成的具体事情,请阅读更多内容。

我已经创build了一个带有虚线边框的视图,在视图的图层中添加了一个CAShapeLayer(参见这个stackoverflow问题: UIView周围的虚线边框 )。 我调整了CAShapeLayer的path以匹配layoutSubviews视图的边界。

这个工作,但有一个化妆品的问题:当视图的边界animation在一个UIViewanimation(如在旋转期间),虚线边框跳转到视图的新边界,而不是平滑的animation,因为视图animation的边界。 也就是说,当视图animation时,虚线边框的右侧和底部不分别保持与视图的右侧和底部部分相邻。 我怎样才能从CAShapeLayer的虚线边界animation的边界animation的视图旁边?

我到目前为止所做的是将一个CABasicAnimation附加到CAShapeLayer上:

 - (void)layoutSubviews { [super layoutSubviews]; self.borderLayer.path = [UIBezierPath bezierPathWithRect:self.bounds].CGPath; self.borderLayer.frame = self.bounds; CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"path"]; [self.borderLayer addAnimation:pathAnimation forKey:nil]; } 

这确实会使虚线边框生成animation,但它没有正确的计时function和animation持续时间以匹配视图animation。 另外,有时候,我们不希望虚线边框动起来,就像当视图第一次布局时,边框不应该从一些旧pathanimation到新的正确path; 它应该只是出现。

你可能已经find了答案,但最近我也面临类似的问题,自解决它,我会发表答案。

- layoutSubViews方法中,可以使用- animationForKey:方法获取当前UIViewanimation作为背景层的CAAnimation 。 使用这个,你可以实现- layoutSubviews像:

 - (void)layoutSubviews { [super layoutSubviews]; // get current animation for bounds CAAnimation *anim = [self.layer animationForKey:@"bounds"]; [CATransaction begin]; if(anim) { // animating, apply same duration and timing function. [CATransaction setAnimationDuration:anim.duration]; [CATransaction setAnimationTimingFunction:anim.timingFunction]; CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"path"]; [self.borderLayer addAnimation:pathAnimation forKey:@"path"]; } else { // not animating, we should disable implicit animations. [CATransaction disableActions]; } self.borderLayer.path = [UIBezierPath bezierPathWithRect:self.bounds].CGPath; self.borderLayer.frame = self.bounds; [CATransaction commit]; }