我必须创建一个带动画的圆圈,我已创建它,一切正常.我的问题是添加渐变色.请参考随附的屏幕截图
下面是我用动画制作圆圈的代码:
let linewidth: CGfloat = 20 dispatch_async(dispatch_get_main_queue()) { () -> VoID in let startAngle = -90.0 let endAngle = -90.01 let circlePath = UIBezIErPath(arcCenter: CGPoint(x: self.frame.wIDth/2,y: self.frame.height/2),radius: CGfloat(self.frame.wIDth/2 - (self.linewidth/2)),startAngle: CGfloat(((startAngle) / 180.0 * M_PI)),endAngle:CGfloat(((endAngle) / 180.0 * M_PI)),clockwise: true) // Circle layer let circleLayer = CAShapeLayer() circleLayer.path = circlePath.CGPath circleLayer.fillcolor = UIcolor.clearcolor().CGcolor circleLayer.strokecolor = UIcolor.greencolor().CGcolor circleLayer.strokeEnd = 94/100 circleLayer.linewidth = self.linewidth circleLayer.zposition = 1 // Background circle layer let circleBackgroundLayer = CAShapeLayer() circleBackgroundLayer.path = circlePath.CGPath circleBackgroundLayer.fillcolor = UIcolor.clearcolor().CGcolor circleBackgroundLayer.strokecolor = UIcolor.lightgraycolor().CGcolor circleBackgroundLayer.strokeEnd = 1.0 circleBackgroundLayer.linewidth = self.linewidth circleBackgroundLayer.zposition = -1 self.layer.addSublayer(circleLayer) self.layer.addSublayer(circleBackgroundLayer) // Add Animation let pathAnimation = CABasicAnimation(keyPath: "strokeEnd") pathAnimation.duration = 0.55 pathAnimation.timingFunction = camediatimingFunction(name: kcamediatimingFunctionlinear) pathAnimation.fromValue = 0 pathAnimation.tovalue = 94/100 circleLayer.addAnimation(pathAnimation,forKey: "strokeEnd") }
我的问题是在上面的代码中我应该在哪里添加CAGradIEntLayer来添加渐变颜色.
下面的代码行是添加渐变颜色:
let gradIEnt: CAGradIEntLayer = CAGradIEntLayer() gradIEnt.frame = CGRectMake(0,170,170) gradIEnt.colors = [UIcolor(hexString: "#27C68A")().CGcolor,UIcolor(hexString: "#86EA26")().CGcolor] gradIEnt.cornerRadius = gradIEnt.frame.wIDth/2 gradIEnt.startPoint = CGPoint(x: 0,y: 1) gradIEnt.endPoint = CGPoint(x: 1,y: 0) self.layer.insertSublayer(gradIEnt,atIndex: 0)解决方法 如果你想要一个真实的渐变颜色模拟,你可以检查 this SO answer.它基于一个十字形,它将你的矩形视图分成4个部分,然后移动每个部分的颜色,以获得应用于图层蒙版的常规渐变. 总结
以上是内存溢出为你收集整理的ios – 圆圈中的渐变色[复制]全部内容,希望文章能够帮你解决ios – 圆圈中的渐变色[复制]所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)