How do you animate the sublayers of the layer of a UIView during a UIView animation?

后端 未结 1 1496
后悔当初
后悔当初 2020-12-14 03:12

In a UIView animation for a view, you can animate its subviews being laid out by including UIViewAnimationOptionLayoutSubviews in the options parameter

相关标签:
1条回答
  • 2020-12-14 03:20

    You might have found the answer already, but I also faced similar problems recently, since solved it, I will post the answer.

    In - layoutSubViews method, you can get current UIView animation as backing layer's CAAnimation with - animationForKey: method. Using this, You can implement - layoutSubviews like:

    - (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];
    }
    
    0 讨论(0)
提交回复
热议问题