繁体   English   中英

为CAShapeLayer添加比例动画

[英]Adding scale animation for CAShapeLayer

我想根据材质设计在两个UIView之间实现过渡动画,如下面的屏幕截图所示(仅动画的第一部分):

在此输入图像描述

但我想通过将动画蒙版应用于UIView使其更加逼真。 这是我开发的代码:

- (IBAction)onButtonTapped:(UIButton *)sender
{
    // 1. make a hole in self.view to make visible another view
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRect:self.view.bounds];
    [maskPath appendPath:[UIBezierPath bezierPathWithArcCenter:sender.center
                                                        radius:sender.frame.size.width/2.0f
                                                    startAngle:0.0f
                                                      endAngle:2.0f*M_PI
                                                     clockwise:NO]];

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.fillRule  = kCAFillRuleEvenOdd;
    maskLayer.fillColor = [UIColor blackColor].CGColor;
    maskLayer.path = maskPath.CGPath;

    // 2. add scale animation for resizing hole
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.fromValue = [NSValue valueWithCGRect:sender.frame];
    animation.toValue = [NSValue valueWithCGRect:self.view.bounds];
    animation.duration = 3.0f;
    [maskLayer addAnimation:animation forKey:@"scaleAnimation"];

    self.view.layer.mask = maskLayer;
}

我的想法是在模态UIView中添加一个“洞”,然后缩放机智动画。

问题在于它无法正常工作。 代码的第一部分做得很好。 但是缩放孔的动画根本不起作用。

如果该层不在某个窗口的层树中,则不能将动画添加到该层。 您需要按以下顺序进行操作:

self.view.layer.mask = maskLayer;
[CATransaction flush];
[maskLayer addAnimation:animation forKey:@"scaleAnimation"];

暂无
暂无

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

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