[英]UIView bounds/frame not matching up with drawRect
我正在学习如何创建自定义UIView。 我正在制作的此特定视图包含几个按钮。 我注意到,当我从惰性实例化块中调用frame / height属性时,得到的值是128,但是当我对drawRect函数中传递的rect参数以及其边界调用height属性时,得到的值是94。
我不明白为什么drawRect函数调用的bounds / frame width / height属性与初始化程序中使用的frame / bounds高度和宽度不一致。 有人可以解释吗?
@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
}()
}
还有另一种类似于setNeedsDisplay的方法,称为setNeedsDisplay(_ :) 。 调用后者时,传递给drawRect(_ :)的参数就是您在该调用中传递的参数。 该矩形区域被标记为无效,需要重绘。
我不知道您是否致电过setNeedsDisplay(_ :) 。 查找是您的工作。 祝好运。 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.