【问题标题】:Adding scale animation for CAShapeLayer为 CAShapeLayer 添加缩放动画
【发布时间】:2016-03-06 17:56:29
【问题描述】:

我想根据如下截图所示的材料设计实现两个 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 中添加一个“洞”,然后缩放机智动画。

问题是它不能正常工作。代码的第一部分很好地打了一个洞。但是孔的缩放动画根本不起作用。

【问题讨论】:

    标签: ios objective-c animation uiview material-design


    【解决方案1】:

    如果图层不在某些窗口的图层树中,则无法向图层添加动画。你需要按这个顺序做事:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多