我试图制作一个圆圈并为其制作动画。当我单击该按钮时,我想从同一个StartPoint重新启动此动画。当我单击按钮时,我应该停止,清除图像并再次启动它。 我尝试使用下面的代码,但它无效。
-(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)];
}
答案 0 :(得分:0)
可以说您的动画看起来像这样...
func runTimerMaskAnimation(duration: CFTimeInterval, fromValue : Double){
...
let path = UIBezierPath(roundedRect: circleBounds, cornerRadius:
circleBounds.size.width * 0.5)
maskLayer?.path = path.reversing().cgPath
maskLayer?.strokeEnd = 0
parentCALayer.mask = maskLayer
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.duration = duration
animation.fromValue = fromValue
animation.toValue = 0.0
maskLayer?.add(animation!, forKey: "strokeEnd")
}
如果我想从原始位置重新启动计时器,则可以删除动画,请删除maskLayer,然后再次运行动画。
maskLayer?.removeAnimation(forKey: "strokeEnd")
maskLayer?.removeFromSuperlayer()
runTimerMaskAnimation(duration: 15, fromValue : 1)