iPhone Core动画-画一个圆

我希望创建一个动画。我能解释的最好方法是,如果您可以想象画一个圆。它从顶部或12点开始,一直沿顺时针方向绘制,直到在10秒钟左右的时间内变成一个完整的圆圈。


我来到的壁橱是使用Core Animation(使用此处的示例代码)绘制围绕中心点旋转的点。但是我对如何画圈不知所措?任何建议都非常欢迎。


非常感谢 :)


守着一只汪
浏览 570回答 3
3回答

缥缈止盈

这是基于@David的答案的另一种解决方案。这种方法使您可以设置圆形动画的方向,并提供更多的控制权。编辑:我已经写了一篇关于如何使用Swift绘制圆的博客文章,我将尝试与beta保持同步。检查下面的代码是否不适合您。let radius = 100.0// Create the circle layervar circle = CAShapeLayer()// Set the center of the circle to be the center of the viewlet center = CGPointMake(CGRectGetMidX(self.frame) - radius, CGRectGetMidY(self.frame) - radius)let fractionOfCircle = 3.0 / 4.0let twoPi = 2.0 * Double(M_PI)// The starting angle is given by the fraction of the circle that the point is at, divided by 2 * Pi and less// We subtract M_PI_2 to rotate the circle 90 degrees to make it more intuitive (i.e. like a clock face with zero at the top, 1/4 at RHS, 1/2 at bottom, etc.)let startAngle = Double(fractionOfCircle) / Double(twoPi) - Double(M_PI_2)let endAngle = 0.0 - Double(M_PI_2)let clockwise: Bool = true// `clockwise` tells the circle whether to animate in a clockwise or anti clockwise directioncircle.path = UIBezierPath(arcCenter: center, radius: radius, startAngle: CGFloat(startAngle), endAngle: CGFloat(endAngle), clockwise: clockwise).CGPath// Configure the circlecircle.fillColor = UIColor.blackColor().CGColorcircle.strokeColor = UIColor.redColor().CGColorcircle.lineWidth = 5// When it gets to the end of its animation, leave it at 0% stroke filledcircle.strokeEnd = 0.0// Add the circle to the parent layerself.layer.addSublayer(circle)// Configure the animationvar drawAnimation = CABasicAnimation(keyPath: "strokeEnd")drawAnimation.repeatCount = 1.0// Animate from the full stroke being drawn to none of the stroke being drawndrawAnimation.fromValue = NSNumber(double: fractionOfCircle)drawAnimation.toValue = NSNumber(float: 0.0)drawAnimation.duration = 30.0drawAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)// Add the animation to the circlecircle.addAnimation(drawAnimation, forKey: "drawCircleAnimation")
打开App,查看更多内容
随时随地看视频慕课网APP