最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift如何画圆圈

Swift crifan 3568浏览 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,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.183 seconds, using 22.09MB memory