使用strokePath和CGRect画一个圆

问题描述 投票:0回答:1
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var imgView: UIImageView!

    @IBAction func buttonpressed(_ sender: UIButton) {
        switch sender.tag {

        case 0: drawCircle() 

        default: print("default")
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        func drawCircle() {

            let renderer = UIGraphicsImageRenderer(size: CGSize(width: 280, height: 250))

            let img = renderer.image { ctx in
                let rect = CGRect(x: 5, y: 5, width: 270, height: 240)
                // 6

                ctx.cgContext.setFillColor(UIColor.blue.cgColor)
                ctx.cgContext.setStrokeColor(UIColor.black.cgColor)
                ctx.cgContext.setLineWidth(10)

                ctx.cgContext.addEllipse(in: rect)
                ctx.cgContext.drawPath(using: .fillStroke)
            }
            imgView.image = img
        }
    }
}
swift render uibezierpath cgrect
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.