【问题标题】:black box around drawRect shape swift快速绘制矩形形状周围的黑框
【发布时间】:2015-12-18 18:48:41
【问题描述】:

我正在尝试学习 Core Graphics,但在使用 drawRect 时遇到了这个问题。 (下图)

我创建了一个继承自 UIView 的类,该类具有 drawRect 代码。在我的 viewcontroller 类中,我设置了尺寸并添加了子视图。

结果是我的形状周围有一个黑框的图像。我该如何解决这个问题?

class myViewController: UIViewController {

var bg = Background()

override func viewDidLoad() {
    super.viewDidLoad()

    bg = Background(frame: CGRectMake(50, 50, 50, 50))
    self.view.addSubview(bg)
    self.view.sendSubviewToBack(bg)
}


class Background: UIView {

override func drawRect(rect: CGRect) {
    // Drawing code

    var path = UIBezierPath(ovalInRect: rect)
    UIColor.greenColor().setFill()
    path.fill()

  }
}

【问题讨论】:

    标签: ios swift core-graphics drawrect


    【解决方案1】:

    您需要设置bg 元素的背景颜色,只需添加这一行bg.backgroundColor = UIColor.clearColor()。因此,在您的 viewDidLoad 方法中,将代码更新为:

    bg = Background(frame: CGRectMake(50, 50, 50, 50))
    bg.backgroundColor = UIColor.clearColor()
    self.view.addSubview(bg)
    self.view.sendSubviewToBack(bg)
    

    【讨论】:

      【解决方案2】:

      可以设置矩形的背景颜色

      class Background: UIView {
        override func drawRect(rect: CGRect) {
          UIColor.whiteColor().setFill()
          UIRectFill(rect)
          let path = UIBezierPath(ovalInRect: rect)
          UIColor.greenColor().setFill()
          path.fill()
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-08
        • 1970-01-01
        • 2022-01-09
        相关资源
        最近更新 更多