【问题标题】:Gradient color in Circle [duplicate]圆形中的渐变色[重复]
【发布时间】:2016-06-11 07:56:41
【问题描述】:

我必须用动画创建一个圆圈,我已经创建了它,并且一切正常。我的问题是添加渐变色。请参考附件截图

下面是我用动画创建圆圈的代码:

      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, 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)

【问题讨论】:

标签: ios swift swift2 gradient


【解决方案1】:

如果您想要真正模拟渐变颜色,您可以查看this SO answer。它基于将矩形视图分成 4 部分的十字,然后移动每个部分的颜色以获得应用于图层蒙版的规则渐变.

【讨论】:

    猜你喜欢
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    相关资源
    最近更新 更多