如何在iPhone中使用动画绘制线条
我正在用CGContextRef在iphone中画线。有谁能建议我如何在iphone中用动画画线如何在iPhone中使用动画绘制线条,iphone,drawing,cgcontext,Iphone,Drawing,Cgcontext,我正在用CGContextRef在iphone中画线。有谁能建议我如何在iphone中用动画画线 请建议。这是一条直线吗?。。可以使用1像素宽的UIView并设置其帧属性的动画。使用[UIView beginAnimations]和[UIView committeanimations]否则 编辑的回复 使用核心动画,您可以执行以下操作(例如,在按钮后面) 路径动画定义为: - (CAAnimation*)pathAnimation; { CGMutablePathRef path =
请建议。这是一条直线吗?。。可以使用1像素宽的UIView并设置其帧属性的动画。使用
[UIView beginAnimations]
和[UIView committeanimations]代码>否则
编辑的回复
使用核心动画,您可以执行以下操作(例如,在按钮后面)
路径动画定义为:
- (CAAnimation*)pathAnimation;
{
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path,NULL,50.0,120.0);
CGPathAddCurveToPoint(path,NULL,50.0,275.0,
150.0,275.0,
150.0,120.0);
CGPathAddCurveToPoint(path,NULL,150.0,275.0,
250.0,275.0,
250.0,120.0);
CGPathAddCurveToPoint(path,NULL,250.0,275.0,
350.0,275.0,
350.0,120.0);
CGPathAddCurveToPoint(path,NULL,350.0,275.0,
450.0,275.0,
450.0,120.0);
CAKeyframeAnimation *
animation = [CAKeyframeAnimation
animationWithKeyPath:@"position"];
[animation setPath:path];
[animation setDuration:3.0];
[animation setAutoreverses:YES];
CFRelease(path);
return animation;
}
这只是一个指向核心动画的指针。有关详细信息,请参阅。以下是答案(可在此处找到:)
不要忘记导入马丁答案的Swift 3版本(使用Xcode 8.3.3进行检查)
谢谢你的回复。我已经试过了,但不起作用。你能给我一个好的示例代码吗。它不仅包含状态线,还包含弧。类似于CGContextAddArc(上下文,单元格大小,0,32,-90,-180,1);请提出建议。我添加了响应,以指示您可以做什么。您绝对需要使用核心动画关键帧动画来执行此操作。这非常有用,但当它结束时,我如何才能再开始一个新动画?提前感谢。@yucelbayram设置动画的委托,然后实现委托方法:-(void)animationDidStop:(CAAnimation*)anim finished:(BOOL)flag@AskeAnker谢谢,我试试这个。
- (CAAnimation*)pathAnimation;
{
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path,NULL,50.0,120.0);
CGPathAddCurveToPoint(path,NULL,50.0,275.0,
150.0,275.0,
150.0,120.0);
CGPathAddCurveToPoint(path,NULL,150.0,275.0,
250.0,275.0,
250.0,120.0);
CGPathAddCurveToPoint(path,NULL,250.0,275.0,
350.0,275.0,
350.0,120.0);
CGPathAddCurveToPoint(path,NULL,350.0,275.0,
450.0,275.0,
450.0,120.0);
CAKeyframeAnimation *
animation = [CAKeyframeAnimation
animationWithKeyPath:@"position"];
[animation setPath:path];
[animation setDuration:3.0];
[animation setAutoreverses:YES];
CFRelease(path);
return animation;
}
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(50.0,0.0)];
[path addLineToPoint:CGPointMake(120.0, 600.0)];
CAShapeLayer *pathLayer = [CAShapeLayer layer];
pathLayer.frame = self.view.bounds;
pathLayer.path = path.CGPath;
pathLayer.strokeColor = [[UIColor redColor] CGColor];
pathLayer.fillColor = nil;
pathLayer.lineWidth = 2.0f;
pathLayer.lineJoin = kCALineJoinBevel;
[self.view.layer addSublayer:pathLayer];
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 2.0;
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
[pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
let path = UIBezierPath()
path.move(to: CGPoint(x: 50, y: 0))
path.addLine(to: CGPoint(x: 120, y: 600))
let pathLayer = CAShapeLayer()
pathLayer.frame = view.bounds
pathLayer.path = path.cgPath
pathLayer.strokeColor = UIColor.red.cgColor
pathLayer.fillColor = nil
pathLayer.lineWidth = 2
pathLayer.lineJoin = kCALineJoinBevel
view.layer.addSublayer(pathLayer)
let pathAnimation = CABasicAnimation(keyPath: "strokeEnd")
pathAnimation.duration = 2.0
pathAnimation.fromValue = 0
pathAnimation.toValue = 1
pathLayer.add(pathAnimation, forKey: "strokeEnd")