为了账号安全,请及时绑定邮箱和手机立即绑定

iPhone Core动画-画一个圆

iPhone Core动画-画一个圆

守着一只汪 2019-10-05 14:36:04
我希望创建一个动画。我能解释的最好方法是,如果您可以想象画一个圆。它从顶部或12点开始,一直沿顺时针方向绘制,直到在10秒钟左右的时间内变成一个完整的圆圈。我来到的壁橱是使用Core Animation(使用此处的示例代码)绘制围绕中心点旋转的点。但是我对如何画圈不知所措?任何建议都非常欢迎。非常感谢 :)
查看完整描述

3 回答

?
缥缈止盈

TA贡献2041条经验 获得超4个赞

这是基于@David的答案的另一种解决方案。这种方法使您可以设置圆形动画的方向,并提供更多的控制权。编辑:我已经写了一篇关于如何使用Swift绘制圆的博客文章,我将尝试与beta保持同步。检查下面的代码是否不适合您。


let radius = 100.0


// Create the circle layer

var circle = CAShapeLayer()


// Set the center of the circle to be the center of the view

let center = CGPointMake(CGRectGetMidX(self.frame) - radius, CGRectGetMidY(self.frame) - radius)


let fractionOfCircle = 3.0 / 4.0


let 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 direction

circle.path = UIBezierPath(arcCenter: center, radius: radius, startAngle: CGFloat(startAngle), endAngle: CGFloat(endAngle), clockwise: clockwise).CGPath


// Configure the circle

circle.fillColor = UIColor.blackColor().CGColor

circle.strokeColor = UIColor.redColor().CGColor

circle.lineWidth = 5


// When it gets to the end of its animation, leave it at 0% stroke filled

circle.strokeEnd = 0.0


// Add the circle to the parent layer

self.layer.addSublayer(circle)


// Configure the animation

var drawAnimation = CABasicAnimation(keyPath: "strokeEnd")

drawAnimation.repeatCount = 1.0


// Animate from the full stroke being drawn to none of the stroke being drawn

drawAnimation.fromValue = NSNumber(double: fractionOfCircle)

drawAnimation.toValue = NSNumber(float: 0.0)


drawAnimation.duration = 30.0


drawAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)


// Add the animation to the circle

circle.addAnimation(drawAnimation, forKey: "drawCircleAnimation")


查看完整回答
反对 回复 2019-10-05
  • 3 回答
  • 0 关注
  • 528 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信