画线然后删除它

时间:2014-04-18 14:03:48

标签: ios line draw

使用Xcode for iOS,我已经绘制了一条画线。 我希望几乎立即将其删除(或逐渐消失)。 我试图重复代码,颜色设置为clear(红色代表测试),因为我的背景是网格图案。但我只得到最后的彩色线条。 是否有关于依次绘制线条的想法?

{
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0.0,100.0)];
[path addLineToPoint:CGPointMake(150.0, 100.0)];
[path addLineToPoint:CGPointMake(155.0, 50.0)];
[path addLineToPoint:CGPointMake(160.0, 150.0)];
[path addLineToPoint:CGPointMake(165.0, 100.0)];
[path addLineToPoint:CGPointMake(350.0, 100.0)];


CAShapeLayer *pathLayer = [CAShapeLayer layer];
pathLayer.frame = self.view.bounds;
pathLayer.path = path.CGPath;
pathLayer.strokeColor = [[UIColor greenColor] CGColor];
pathLayer.fillColor = nil;
pathLayer.lineWidth = 2.0f;
pathLayer.lineJoin = kCALineJoinBevel;

[self.view.layer addSublayer:pathLayer];

CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 1.0;
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
[pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
}

由于

2 个答案:

答案 0 :(得分:1)

添加:

    pathAnimation.autoreverses = true;
    pathAnimation.removedOnCompletion = false;
    pathAnimation.fillMode = kCAFillModeForwards;

您可能还希望使用动画委托功能在完成时删除图层。

或者,如果您希望在动画反转之前有延迟(甚至是轻微),或者想要以与反转动画不同的方式淡出它,则可以使用CAAnimationGroup在同一时间轴上执行一系列动画。

答案 1 :(得分:0)

感谢所有帮助。 我设法找到并成功使用了“Core Animation”Apple文档中的代码。这是我第一次做到这一点。对不起,慢一点,一直在学习。我不得不用“pathLayer”替换“titleLayer”。我相信这会有所帮助。 再次感谢。

这是我的工作代码。在我上面发布的代码之后,在最后一个大括号之前添加。

 CABasicAnimation* fadeAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];

fadeAnim.fromValue = [NSNumber numberWithFloat:1.0];

fadeAnim.toValue = [NSNumber numberWithFloat:0.0];

fadeAnim.duration = 1.5;

[pathLayer addAnimation:fadeAnim forKey:@"opacity"];



// Change the actual data value in the layer to the final value.

pathLayer.opacity = 0.0;