最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

[已解决]swift如何画圆圈

Swift crifan 941浏览 0评论

折腾:

[已解决]swift给图片右上角加上提醒作用的带数字的大红色圆圈和红色小圆点

期间,需要先去画出来一个圆圈。

[折腾过程]

1.搜:

swift draw circle

参考:

How do I draw a circle in iOS Swift? – Stack Overflow

An iOS 8 Swift Graphics Tutorial using Core Graphics and Core Image – Techotopia

Core Graphics Tutorial Part 1: Getting Started – Ray Wenderlich

搜:

swift UIBezierPath

参考:

UIBezierPath Class Reference

试试:

    func drawCircle(circleRadius:CGFloat) -> CAShapeLayer {

        let circlePath = UIBezierPath(

            arcCenter: CGPoint(x: circleRadius,y: circleRadius),

            radius: circleRadius,

            startAngle: CGFloat(0),

            endAngle:CGFloat(M_PI * 2),

            clockwise: true)

       

        let circleLayer = CAShapeLayer()

        circleLayer.path = circlePath.CGPath

       

        //circle inside fill color

        circleLayer.fillColor = UIColor.redColor().CGColor

//        //set circle line color

//        circleLayer.strokeColor = UIColor.yellowColor().CGColor

//        //set circle line width

//        circleLayer.lineWidth = 3.0

       

        return circleLayer

    }

        let badgeRadius:CGFloat = 9

        let badgeFrame = CGRectMake(0, 0, badgeRadius*2, badgeRadius*2)

       

        let circleLayer = drawCircle(badgeRadius)

        //let badgeView = UIView(frame: CGRectMake(0, 0, badgeRadius*2, badgeRadius*2))

        let badgeView = UIView(frame: badgeFrame)

        badgeView.layer.addSublayer(circleLayer)

效果如图:

加上stroke边框:

    func drawCircle(circleRadius:CGFloat) -> CAShapeLayer {

        let circlePath = UIBezierPath(

            arcCenter: CGPoint(x: circleRadius,y: circleRadius),

            radius: circleRadius,

            startAngle: CGFloat(0),

            endAngle:CGFloat(M_PI * 2),

            clockwise: true)

       

        let circleLayer = CAShapeLayer()

        circleLayer.path = circlePath.CGPath

       

        //circle inside fill color

        circleLayer.fillColor = UIColor.redColor().CGColor

        //set circle line color

        circleLayer.strokeColor = UIColor.yellowColor().CGColor

        //set circle line width

        circleLayer.lineWidth = 3.0

       

        return circleLayer

    }

        let badgeRadius:CGFloat = 9

        let badgeFrame = CGRectMake(0, 0, badgeRadius*2, badgeRadius*2)

       

        let circleLayer = drawCircle(badgeRadius)

        //let badgeView = UIView(frame: CGRectMake(0, 0, badgeRadius*2, badgeRadius*2))

        let badgeView = UIView(frame: badgeFrame)

        badgeView.layer.addSublayer(circleLayer)

的效果:

[总结]

后来整理出独立的函数:

//draw a circle fill with color

func drawCircleLayer(circleRadius:CGFloat, fillColor:UIColor) -> CAShapeLayer {

    let circlePath = UIBezierPath(

        arcCenter: CGPoint(x: circleRadius,y: circleRadius),

        radius: circleRadius,

        startAngle: CGFloat(0),

        endAngle:CGFloat(M_PI * 2),

        clockwise: true)

    let circleLayer = CAShapeLayer()

    circleLayer.path = circlePath.CGPath

    //circle inside fill color

    circleLayer.fillColor = fillColor.CGColor

    //        //set circle line color

    //        circleLayer.strokeColor = UIColor.yellowColor().CGColor

    //        //set circle line width

    //        circleLayer.lineWidth = 3.0

    return circleLayer

}

//draw a circle view

func drawCircle(circleRadius:CGFloat, fillColor:UIColor) -> UIView {

    //    let circleRadius:CGFloat = 4

    let circleFrame = CGRectMake(0, 0, circleRadius * 2, circleRadius * 2)

    let circleView = UIView(frame: circleFrame)

    let circleLayer = drawCircleLayer(circleRadius, fillColor: fillColor)

    circleView.layer.addSublayer(circleLayer)

    return circleView

}

供参考。

更多最新完整代码详见:

https://github.com/crifan/crifanLib/blob/master/swift/UI/Image/CrifanUIImage.swift

https://github.com/crifan/crifanLib/blob/master/swift/UI/Image/CrifanUIImageDemo.swift

转载请注明:在路上 » [已解决]swift如何画圆圈

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
58 queries in 0.085 seconds, using 18.86MB memory