【发布时间】:2015-03-08 00:12:13
【问题描述】:
我正在尝试制作一个圆圈并为其设置动画。当我点击按钮时,我想从同一个起点重新开始这个动画。当我单击按钮时,我应该停止,清除图像并重新启动。 我正在尝试使用下面的代码,但它不起作用。
-(IBAction)MakeCircle:(id)sender{
// Make a circular shape
CAShapeLayer *circle = [CAShapeLayer layer];
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius)
cornerRadius:radius].CGPath;
// Positioning
circle.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius,
CGRectGetMidY(self.view.frame)-radius);
// Configure color and linewidth
circle.fillColor = [UIColor whiteColor].CGColor;
circle.lineWidth = 10;
// Add to parent layer
[self.view.layer addSublayer:circle];
// Configure animation
//CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.duration = 5.0;
drawAnimation.repeatCount = 1.0;
drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
drawAnimation.toValue = [NSNumber numberWithFloat:1.0f];
drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
// Add the animation
[circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
}
//Stop, "clean"screen and start again
-(IBAction)ButtonStopClearStart:(id)sender{
circle.speed = 0.0;
[circle removeAnimationForKey:@"drawCircleAnimation"];
[self MakeCircle:(sender)];
}
【问题讨论】:
-
不是每次都为每个动画创建 CAShapeLayer,而是在 viewDidLoad 中创建一次,然后像你一样简单地将动画添加到它。当在你计划的动画上指定 fromValue 时,它将重置动画并重新开始。
-
现在我遇到了同样的问题。
-
但我无法理解@Andy 的建议,如果能发布详细的答案就好了:(
标签: ios objective-c cabasicanimation