【问题标题】:Draw a circle using strokePath and CGRect使用 strokePath 和 CGRect 画一个圆
【发布时间】:2019-12-19 06:29:00
【问题描述】:
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】:

    你可以用这个画一个圆:

    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)
    

    【讨论】:

      【解决方案2】:

      就我个人而言,我建议留在 UIKit 中探索你的道路。你可以为你的椭圆创建UIBezierPathstroke。无需深入研究 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 属性来确定点击了哪个按钮。

      【讨论】:

        猜你喜欢
        • 2012-04-27
        • 2014-10-13
        • 1970-01-01
        • 2012-03-02
        • 2011-10-15
        • 1970-01-01
        • 1970-01-01
        • 2011-02-28
        • 1970-01-01
        相关资源
        最近更新 更多