iPhone CALayer动画 [英] iPhone CALayer animation

查看:66
本文介绍了iPhone CALayer动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在画一条线动画,用一条线连接2个点。这是我的代码:

I am drawing a line animation, connecting 2 points with a line. This is my code:

-(void) drawObject{     
    rootLayer   = [CALayer layer];
    rootLayer.frame = self.view.bounds;
    [self.view.layer addSublayer:rootLayer];

    lineStartPath = CGPathCreateMutable();
    CGPathMoveToPoint(lineStartPath, nil, 160, 50);
    CGPathAddLineToPoint(lineStartPath, nil, 160, 50);
    CGPathCloseSubpath(lineStartPath);

    lineEndPath = CGPathCreateMutable();
    CGPathMoveToPoint(lineEndPath, nil, 160, 50);
    CGPathAddLineToPoint(lineEndPath, nil, 160, 300);
    CGPathCloseSubpath(lineEndPath);

    shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = lineStartPath;
    UIColor *strokeColor = [UIColor colorWithHue:0.557 saturation:0.55 brightness:0.96 alpha:1.0];
    shapeLayer.strokeColor = strokeColor.CGColor;
    shapeLayer.lineWidth = 3.0;

    [rootLayer addSublayer:shapeLayer];

    [self performSelector:@selector(startAnimation) withObject:nil afterDelay:1.0];
}

-(void) startAnimation{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
    animation.duration = 0.5;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    animation.fromValue = (id)lineStartPath;
    animation.toValue = (id)lineEndPath;
    [shapeLayer addAnimation:animation forKey:@"animatePath"];
}

弱点是动画结束后,线条消失了。我希望在这两点之间画线后,id不会消失。请给我一个提示。

The weak point is after the animation is ended, the line disappears. I want that after my line is drawn between those 2 points, id does not disappear. Give me a hint please.

推荐答案

尝试添加

shapeLayer.path = lineEndPath;

此视频对此进行了说明:会议424-核心动画中的动画,第1部分,来自WWDC 2010(大约38:00)。

It's explained in this video: Session 424 - Core Animation in Practice, Part 1 from the WWDC 2010 (around 38:00).

,并替换 [shapeLayer addAnimation :animation forKey:@ animatePath]; [shapeLayer addAnimation:animation forKey:@ path];

这篇关于iPhone CALayer动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆