如何在iOS Swift中绘制圆圈?

TIM*_*MEX 68 ios swift

let block = UIView(frame: CGRectMake(cellWidth-25, cellHeight/2-8, 16, 16))
block.backgroundColor = UIColor(netHex: 0xff3b30)
block.layer.cornerRadius = 9
block.clipsToBounds = true
Run Code Online (Sandbox Code Playgroud)

这就是我现在所拥有的,但显然不是正确的方法.

最简单的方法是什么?

Dar*_*rio 197

你可以画一个圆圈:

Swift 2.2:

    let circlePath = UIBezierPath(arcCenter: CGPoint(x: 100,y: 100), radius: CGFloat(20), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)

    let shapeLayer = CAShapeLayer()
    shapeLayer.path = circlePath.CGPath

    //change the fill color
    shapeLayer.fillColor = UIColor.clearColor().CGColor
    //you can change the stroke color
    shapeLayer.strokeColor = UIColor.redColor().CGColor
    //you can change the line width
    shapeLayer.lineWidth = 3.0

    view.layer.addSublayer(shapeLayer)
Run Code Online (Sandbox Code Playgroud)

Swift 3.0:

    let circlePath = UIBezierPath(arcCenter: CGPoint(x: 100, y: 100), radius: CGFloat(20), startAngle: CGFloat(0), endAngle: CGFloat(Double.pi * 2), clockwise: true)

    let shapeLayer = CAShapeLayer()
    shapeLayer.path = circlePath.cgPath

    //change the fill color
    shapeLayer.fillColor = UIColor.clear.cgColor
    //you can change the stroke color
    shapeLayer.strokeColor = UIColor.red.cgColor
    //you can change the line width
    shapeLayer.lineWidth = 3.0

    view.layer.addSublayer(shapeLayer)
Run Code Online (Sandbox Code Playgroud)

使用您发布的代码,您正在裁剪UIView的角落,而不是在视图中添加圆圈.


以下是使用该方法的完整示例:

// make the UIView a ring of color
import UIKit
class Ring:UIView
    {
    override func drawRect(rect: CGRect)
        {
        drawRingFittingInsideView()
        }

    internal func drawRingFittingInsideView()->()
        {
        let halfSize:CGFloat = min( bounds.size.width/2, bounds.size.height/2)
        let desiredLineWidth:CGFloat = 1    // your desired value

        let circlePath = UIBezierPath(
            arcCenter: CGPoint(x:halfSize,y:halfSize),
            radius: CGFloat( halfSize - (desiredLineWidth/2) ),
            startAngle: CGFloat(0),
            endAngle:CGFloat(M_PI * 2),
            clockwise: true)

        let shapeLayer = CAShapeLayer()
        shapeLayer.path = circlePath.CGPath

        shapeLayer.fillColor = UIColor.clearColor().CGColor
        shapeLayer.strokeColor = UIColor.redColor().CGColor
        shapeLayer.lineWidth = desiredLineWidth

        layer.addSublayer(shapeLayer)
        }
    }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


但请注意,这是一个非常方便的电话

let circlePath = UIBezierPath(ovalInRect:rect)

它完成了制作路径的所有工作.(不要忘记为线条厚度插入它,这也非常容易layoutSubview.)

internal func drawRingFittingInsideView(rect: CGRect)->()
    {
    let desiredLineWidth:CGFloat = 4    // your desired value
    let hw:CGFloat = desiredLineWidth/2

    let circlePath = UIBezierPath(ovalInRect: CGRectInset(rect,hw,hw) )

    let shapeLayer = CAShapeLayer()
    shapeLayer.path = circlePath.CGPath
    shapeLayer.fillColor = UIColor.clearColor().CGColor
    shapeLayer.strokeColor = UIColor.redColor().CGColor
    shapeLayer.lineWidth = desiredLineWidth
    layer.addSublayer(shapeLayer)
    }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


在斯威夫特的这些日子里,你肯定会使用

@IBDesignable

@IBInspectable

通过这种方式,您可以在Storyboard中实际查看和更改渲染!

如您所见,它实际上为Storyboard上的Inspector添加了新功能,您可以在Storyboard上更改这些功能:

在此输入图像描述

这是代码......

// Dot with border, which you can control completely in Storyboard
import UIKit
@IBDesignable
class Dot:UIView
    {
    @IBInspectable var mainColor: UIColor = UIColor.blueColor()
        {
        didSet { print("mainColor was set here") }
        }
    @IBInspectable var ringColor: UIColor = UIColor.orangeColor()
        {
        didSet { print("bColor was set here") }
        }
    @IBInspectable var ringThickness: CGFloat = 4
        {
        didSet { print("ringThickness was set here") }
        }

    @IBInspectable var isSelected: Bool = true

    override func drawRect(rect: CGRect)
        {
        let dotPath = UIBezierPath(ovalInRect:rect)
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = dotPath.CGPath
        shapeLayer.fillColor = mainColor.CGColor
        layer.addSublayer(shapeLayer)

        if (isSelected) { drawRingFittingInsideView(rect) }
        }

    internal func drawRingFittingInsideView(rect: CGRect)->()
        {
        let hw:CGFloat = ringThickness/2
        let circlePath = UIBezierPath(ovalInRect: CGRectInset(rect,hw,hw) )

        let shapeLayer = CAShapeLayer()
        shapeLayer.path = circlePath.CGPath
        shapeLayer.fillColor = UIColor.clearColor().CGColor
        shapeLayer.strokeColor = ringColor.CGColor
        shapeLayer.lineWidth = ringThickness
        layer.addSublayer(shapeLayer)
        }
    }
Run Code Online (Sandbox Code Playgroud)

最后请注意,如果您有一个UIView(它是正方形,并且您在Storyboard中设置为红色)并且您只想将其转换为红色圆圈,则可以执行以下操作:

// It makes a UIView into a circular dot of color
import UIKit
class Dot:UIView
    {
    override func layoutSubviews()
        { layer.cornerRadius = bounds.size.width/2; }
    }
Run Code Online (Sandbox Code Playgroud)

  • 在`func drawRect(rect:CGRect)`方法中添加新的CALayer可能不是一个好主意,因为这个方法应该在自己的层中进行自定义绘制,这个方法也会被称为多次. (4认同)
  • `M_PI`已弃用.它现在是`Double.pi`(在Swift 3中) (3认同)
  • 请不要在drawRect方法中添加子层!首先,在视图生命周期中,实际上是在每个布局过程中,都会多次调用drawRect。这意味着,您最终将在层次结构中拥有数十个相等(或可能没有)的子层,从而消耗内存并降低应用程序的运行速度。其次,drawRect不适用于修改视图层次结构。如果您确实需要绘制一些特殊的东西,可以将其与CoreGraphics上下文函数结合使用。更好的方法-只是在init方法中添加形状层。 (3认同)

Oxc*_*cug 8

如果要使用UIView绘制它,则需要设置半径/高度或宽度.

所以只需改变:

block.layer.cornerRadius = 9
Run Code Online (Sandbox Code Playgroud)

至:

block.layer.cornerRadius = block.frame.width / 2
Run Code Online (Sandbox Code Playgroud)

但是,您需要使高度和宽度相同.如果你想使用coregraphics,那么你会想做这样的事情:

CGContextRef ctx= UIGraphicsGetCurrentContext();
CGRect bounds = [self bounds];

CGPoint center;
center.x = bounds.origin.x + bounds.size.width / 2.0;
center.y = bounds.origin.y + bounds.size.height / 2.0;
CGContextSaveGState(ctx);

CGContextSetLineWidth(ctx,5);
CGContextSetRGBStrokeColor(ctx,0.8,0.8,0.8,1.0);
CGContextAddArc(ctx,locationOfTouch.x,locationOfTouch.y,30,0.0,M_PI*2,YES);
CGContextStrokePath(ctx);
Run Code Online (Sandbox Code Playgroud)


小智 5

制作一个类UIView并将其分配给此代码一个简单的圆圈

import UIKit
@IBDesignable
class DRAW: UIView {

    override func draw(_ rect: CGRect) {

        var path = UIBezierPath()
        path = UIBezierPath(ovalIn: CGRect(x: 50, y: 50, width: 100, height: 100))
        UIColor.yellow.setStroke()
        UIColor.red.setFill()
        path.lineWidth = 5
        path.stroke()
        path.fill()


    }


}
Run Code Online (Sandbox Code Playgroud)


ate*_*kov 5

Swift 4版本接受的答案:

@IBDesignable
class CircledDotView: UIView {

    @IBInspectable var mainColor: UIColor = .white {
        didSet { print("mainColor was set here") }
    }
    @IBInspectable var ringColor: UIColor = .black {
        didSet { print("bColor was set here") }
    }
    @IBInspectable var ringThickness: CGFloat = 4 {
        didSet { print("ringThickness was set here") }
    }

    @IBInspectable var isSelected: Bool = true

    override func draw(_ rect: CGRect) {
        let dotPath = UIBezierPath(ovalIn: rect)
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = dotPath.cgPath
        shapeLayer.fillColor = mainColor.cgColor
        layer.addSublayer(shapeLayer)

        if (isSelected) {
            drawRingFittingInsideView(rect: rect)
        }
    }

    internal func drawRingFittingInsideView(rect: CGRect) {
        let hw: CGFloat = ringThickness / 2
        let circlePath = UIBezierPath(ovalIn: rect.insetBy(dx: hw, dy: hw))

        let shapeLayer = CAShapeLayer()
        shapeLayer.path = circlePath.cgPath
        shapeLayer.fillColor = UIColor.clear.cgColor
        shapeLayer.strokeColor = ringColor.cgColor
        shapeLayer.lineWidth = ringThickness
        layer.addSublayer(shapeLayer)
    }
}
Run Code Online (Sandbox Code Playgroud)


pau*_*o62 5

这是我使用 Swift 5 和 Core Graphics 的版本。

我创建了一个类来绘制两个圆圈。第一个圆是使用addEllipse(). 它将椭圆放入一个正方形,从而创建一个圆。我觉得很奇怪没有函数addCircle()。第二个圆是使用addArc()2pi 弧度创建的

import UIKit

@IBDesignable
class DrawCircles: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func draw(_ rect: CGRect) {

        guard let context = UIGraphicsGetCurrentContext() else {
            print("could not get graphics context")
            return
        }

        context.setLineWidth(2)

        context.setStrokeColor(UIColor.blue.cgColor)

        context.addEllipse(in: CGRect(x: 30, y: 30, width: 50.0, height: 50.0))

        context.strokePath()

        context.setStrokeColor(UIColor.red.cgColor)

        context.beginPath() // this prevents a straight line being drawn from the current point to the arc

        context.addArc(center: CGPoint(x:100, y: 100), radius: 20, startAngle: 0, endAngle: 2.0*CGFloat.pi, clockwise: false)

        context.strokePath()
    }
}
Run Code Online (Sandbox Code Playgroud)

在您的 ViewController 中didViewLoad()添加以下内容:

let myView = DrawCircles(frame: CGRect(x: 50, y: 50, width: 300, height: 300))

self.view.addSubview(myView)
Run Code Online (Sandbox Code Playgroud)

当它运行时,它应该是这样的。我希望你喜欢我的解决方案!

在此处输入图片说明