使用strokePath和CGRect画一个圆

时间:2019-12-19 06:29:00

标签: swift render uibezierpath cgrect

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
        }
    }
}

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

就个人而言,我建议您呆在UIKit中以摸索您的路径。您可以为椭圆创建UIBezierPath,然后为stroke创建椭圆。无需深入研究CoreGraphics。

但是关键的问题是,您应该从drawCircle中提取viewDidLoad函数声明,并使其成为完整的实例方法。

因此,如果您想定义适合图像视图内部的椭圆形图像:

class ViewController: UIViewController {

    @IBOutlet weak var imgView: UIImageView!

    @IBAction func buttonPressed(_ sender: UIButton) {
        switch sender.tag {
        case 0: drawOval()

        default: print("default")
        }
    }

    func drawOval() {
        let bounds = imgView.bounds

        imgView.image = UIGraphicsImageRenderer(bounds: bounds).image { _ in
            let lineWidth: CGFloat = 10
            let rect = bounds.insetBy(dx: lineWidth / 2, dy: lineWidth / 2)
            let path = UIBezierPath(ovalIn: rect)

            UIColor.blue.setFill()
            path.fill()

            UIColor.black.setStroke()
            path.lineWidth = lineWidth
            path.stroke()
        }
    }
}

或者,如果您想要的图像是在图像视图中居中的圆形:

func drawCircle() {
    let bounds = imgView.bounds

    imgView.image = UIGraphicsImageRenderer(bounds: bounds).image { _ in
        let lineWidth: CGFloat = 10
        let center = CGPoint(x: bounds.midX, y: bounds.midY)
        let radius = (min(bounds.width, bounds.height) - lineWidth) / 2
        let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: 0, endAngle: 2 * .pi, clockwise: true)

        UIColor.blue.setFill()
        path.fill()

        UIColor.black.setStroke()
        path.lineWidth = lineWidth
        path.stroke()
    }
}

无论如何,我通常建议不要使用tag属性来找出被点击的按钮。