【问题标题】:Why CALayer and CAShapeLayer behaving differently为什么 CALayer 和 CAShapeLayer 表现不同
【发布时间】:2015-11-01 17:07:10
【问题描述】:

首先我创建了CAShapeLayer,用于在底部以10px 的高度遮盖UIView,如下所示:

CAShapeLayer *mask = [[CAShapeLayer alloc]init];
CGRect maskRect = CGRectMake(0, self.view.frame.size.height-10, self.view.frame.size.width, 10.0);
CGPathRef path = CGPathCreateWithRect(maskRect, NULL);
mask.path = path;
CGPathRelease(path);
mask.anchorPoint = CGPointMake(0.5, 1.0);
self.view.layer.mask = mask;

在注释完上面的代码后,我创建了一个CALayer,用于将底部的UIView 隐藏起来,高度为10px,如下所示:

CALayer *mask = [CALayer layer];
mask.contents = (id)[[self getImg]CGImage];
mask.frame = CGRectMake(0, self.view.frame.size.height-5, self.view.frame.size.width, 10.0);
mask.anchorPoint = CGPointMake(0.5, 1.0);
self.view.layer.mask = mask;

问题 1:

要完美地触摸底部而不是上方或下方的像素,对于CAShapeLayer 我定义的CGRect

CGRectMake(0, self.view.frame.size.height-10, self.view.frame.size.width, 10.0);

对于CALayer,我定义的CGRect

CGRectMake(0, self.view.frame.size.height-5, self.view.frame.size.width, 10.0);

为什么CAShapeLayer 是-10,CALayer 为什么是-5?


CAShapeLayerCALayer 上的动画代码及其效果:

CGRect oldBounds = mask.bounds;
    CGRect newBounds = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/2);

    [UIView animateWithDuration:1.0 animations:^{
        CABasicAnimation* revealAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];
        revealAnimation.fromValue = [NSValue valueWithCGRect:oldBounds];
        revealAnimation.toValue = [NSValue valueWithCGRect:newBounds];
        revealAnimation.duration = 0.5;
        [mask addAnimation:revealAnimation forKey:@"revealAnimation"];
    }];
    // Update the bounds so the layer doesn't snap back when the animation completes.
    mask.bounds = newBounds;

CAShapeLayer 的影响位置发生了变化,但对高度没有影响,如下所示:

CALayer 的影响,效果完美,我想要的结果:

问题 2: 为什么CAShapeLayer改变了位置而不是高度?

我不知道为什么,但我感觉CALayer 动画不流畅?

问题 3:

[UIView animateWithDuration:1.0 animations:^{ ... 块对动画没有任何影响,为什么?

【问题讨论】:

    标签: ios objective-c uiview calayer cashapelayer


    【解决方案1】:

    答案 1: 不知道为什么CAShapeLayerCALayer 是 5 和 10,需要仔细调试,但可能问题是在调整 anchorPoint 或者因为你使用了形状层的path

    答案 2: 关于CAShapeLayer 的影响:这是因为您将CGPath 分配给它,并更改了形状图层框架,但没有更改路径。所以你需要用正确的坐标设置新的path 属性。像这样的:

    CGRect maskRect =  CGRect newBounds = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/2);
    CGPathRef path = CGPathCreateWithRect(maskRect, NULL);
    mask.path = path;
    CGPathRelease(path);
    

    答案3:

    CABasicAnimation 是类,它会做所有的动画,你不需要在UIView animateWithDuration 块中使用它。

    此代码应按预期工作:

    CABasicAnimation* revealAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];
    revealAnimation.fromValue = [NSValue valueWithCGRect:oldBounds];
    revealAnimation.toValue = [NSValue valueWithCGRect:newBounds];
    revealAnimation.duration = 0.5;
    [mask addAnimation:revealAnimation forKey:@"revealAnimation"];
    

    【讨论】:

      猜你喜欢
      • 2021-09-04
      • 1970-01-01
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 2020-12-28
      • 2015-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多