完成动画后如何删除CALayer?

时间:2017-02-02 17:13:37

标签: ios objective-c xcode

我有一个视图,我在drawRect方法中绘制所有的CALayers。在方法内部,我开始动画,在完成块上,我从超级图层中删除图层及其子图层。但是,它会继续作为我的动画中的子图层添加回来。我该如何防止这种情况发生?

- (void)drawRect:(CGRect)rect
{

...

[CATransaction begin];
    {
        [CATransaction setAnimationDuration: 5.0];//Dynamic Duration
        [CATransaction setCompletionBlock:^{
            [tutorialCircle removeFromSuperlayer];
            [strokePart removeFromSuperlayer];
        }];
...

for (NSUInteger i = 0; i < 3; i++)
        {
            strokePart = [[CAShapeLayer alloc] init];
            strokePart.fillColor = [[UIColor clearColor] CGColor];
            strokePart.frame = tutorialCircle.bounds;
            strokePart.path = tutorialCircle.path;
            strokePart.lineCap = tutorialCircle.lineCap;
            strokePart.lineWidth = tutorialCircle.lineWidth;
...

[tutorialCircle addSublayer: strokePart];

如何确保我的子图层及其子图层仅为动画显示一次,然后永远消失?

1 个答案:

答案 0 :(得分:-1)

由于某种原因,我必须将两者的不透明度设置为0并删除它们。怪异。

[CATransaction setCompletionBlock:^{
            [tutorialCircle removeFromSuperlayer];
            tutorialCircle.opacity = 0.0;
            strokePart.opacity = 0.0;
            [strokePart removeFromSuperlayer];
        }];