简体   繁体   English

为什么CALayer和CAShapeLayer表现不同

[英]Why CALayer and CAShapeLayer behaving differently

First I created the CAShapeLayer for masking the UIView at the bottom with height of 10px like this: 首先,我创建了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;

After commenting the above code I created a CALayer for masking the UIView at the bottom with height of 10px like this: 在注释完上面的代码后,我创建了一个CALayer用于以10px的高度屏蔽底部的UIView ,如下所示:

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;

Question 1: 问题1:

To perfectly touch the bottom not a pixel above or below, for CAShapeLayer the CGRect I defined is 为了完美触摸底部而不是上方或下方的像素,对于CAShapeLayer ,我定义的CGRect

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

and for CALayer the CGRect I defined is 对于CALayer ,我定义的CGRect

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

why -10 for CAShapeLayer and why -5 for CALayer ? 为什么CAShapeLayer为-10,为什么CALayer为-5?


The animation code and its effects on CAShapeLayer and CALayer : 动画代码及其对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;

Effect on CAShapeLayer the position is changed but no effect on hight like this: CAShapeLayer位置的影响已更改,但对高度没有影响,如下所示:

在此处输入图片说明

Effect on CALayer which worked perfectly and the result I wanted: CALayer效果很好,我想要的结果是:

在此处输入图片说明

Question 2: Why CAShapeLayer changed the position not the height? 问题2:为什么CAShapeLayer更改位置而不更改高度?

I don't know why but I am having a feeling like CALayer animation is not going smooth? 我不知道为什么,但是我感觉到CALayer动画运行不流畅吗?

Question 3: 问题3:

The [UIView animateWithDuration:1.0 animations:^{ ... block do not have any effect on animation, why? [UIView animateWithDuration:1.0 animations:^{ ...块对动画没有任何影响,为什么?

Answer 1: Not sure why it is 5 and 10 for CAShapeLayer and CALayer , need to debug closely, but maybe problem is in adjusting anchorPoint or because you use path of shape layer 答案1:不确定CAShapeLayerCALayer为何分别为5和10,需要进行紧密调试,但是可能是由于调整anchorPoint或因为您使用形状层的path

Answer 2: About effect on CAShapeLayer : this is because you assigned CGPath to it, and changed shape layer frame, but not path. 答案2:关于对CAShapeLayer :这是因为您CGPath分配了CGPath ,并更改了形状图层框架,但未更改路径。 So you need to set new path property with correct coordinates. 因此,您需要使用正确的坐标设置新的path属性。 Something like this: 像这样:

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);

Answer 3: 答案三:

CABasicAnimation is class, which will do all animation, and you don't need to use it in UIView animateWithDuration block. CABasicAnimation是一个类,它将执行所有动画,并且您不需要在UIView animateWithDuration块中使用它。

This code should work as expected: 此代码应按预期工作:

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

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

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