UIView边界/框架与drawRect

时间:2016-08-21 19:36:12

标签: ios swift uiview frame drawrect

我正在学习如何创建自定义UIViews。我制作的这个特定视图包含几个按钮。我注意到当我从惰性实例化块中调用frame / height属性时,我得到值128,但是当我在drawRect函数中传递的rect参数上调用height属性以及边界时,我得到值94。

我不明白为什么从drawRect函数调用的bounds / frame width / height属性不与初始化程序中使用的frame / bounds height和width对齐。有人可以解释一下吗?

@IBDesignable
class GroupingMetaDataView: UIView {

    @IBInspectable var strokeColor: UIColor =
        UIColor.blackColor().colorWithAlphaComponent(0.4)

    @IBInspectable var strokeWidth: CGFloat = 2.0


    lazy var subButton: AddButton! = {
        print("frame height: \(self.frame.height)")
        print("bounds height: \(self.bounds.height)")
        let width = self.frame.width * 0.087
        let size = CGSize(width: width, height: width)
        let frame = CGRect(origin: CGPoint(x: self.frame.width * 0.055,
                                           y: self.frame.height * 0.5), size: size)
        let button = AddButton(frame: frame, isAddButton: false)

        return button
    }()

    lazy var addButton: AddButton! = {
        let width = self.frame.width * 0.087
        let size = CGSize(width: width, height: width)
        let frame = CGRect(origin: CGPoint(x: self.frame.width * 0.857,
                                           y: self.frame.height), size: size)
        let button = AddButton(frame: frame, isAddButton: true)

        return button
    }()

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

        self.setupUI()

    }

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

    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
        print("rect height: \(rect.height)")
        print("boundsheight: \(bounds.height)")

        let context = UIGraphicsGetCurrentContext()
        CGContextSetLineWidth(context, strokeWidth)
        strokeColor.setStroke()

        let topBarPath = UIBezierPath()
        topBarPath.moveToPoint(CGPoint(x: 0.0, y: 2.0))
        topBarPath.addLineToPoint(CGPoint(x: rect.width, y: 2.0))

        topBarPath.lineWidth = strokeWidth
        topBarPath.stroke()

        let bottomBarPath = UIBezierPath()
        bottomBarPath.moveToPoint(CGPoint(x: 0.0, y: rect.height-2.0))
        bottomBarPath.addLineToPoint(CGPoint(x: rect.width, y: rect.height-2.0))

        bottomBarPath.lineWidth = strokeWidth
        bottomBarPath.stroke()
    }

    // MARK: Setup

    func setupUI() {
        self.backgroundColor = UIColor.yellowColor()
        addSubview(subButton)
        addSubview(addButton)
    }
}

修改

我从ViewController创建GroupingMetaDataView。 YardageMetaDataView是GroupingMetaDataView的子类。 YardageMetaDataView不会执行任何自定义绘图。

我注意到drawRect参数的文档说明了

  

需要更新视图边界的部分。首先   绘制视图的时间,这个矩形通常是整个   您视图的可见边界。但是,在随后的绘图中   操作时,矩形可以只指定视图的一部分。

何时以及为什么参数rect只指定视图的一部分?在触发setNeedsDisplay之后,将在下一个绘图周期中调用drawRect。 SetNeedsDisplay没有任何参数,所以我假设参数代表整个视图?

ViewController.swift

class ViewController: UIViewController {

// MARK: - Properties

  lazy var yMetaDataView: YardageMetaDataView! = {
      let view = YardageMetaDataView(frame: CGRect(origin: CGPoint(x: 50, y: 100),
                                   size: CGSize(width: self.view.bounds.width * 0.75,
                                                height: self.view.bounds.height * 0.102)))
      view.translatesAutoresizingMaskIntoConstraints = false
      return view
  }()
}

1 个答案:

答案 0 :(得分:0)

还有另一种类似 setNeedsDisplay 的方法,名为 setNeedsDisplay(_:) 。调用后者时,传递给 drawRect(_:) 的参数是您在该调用中传递的参数。该矩形区域标记为无效,需要重新绘制。

我不知道您是否已经调用 setNeedsDisplay(_:)。找出答案是你的工作。祝好运。 :)