sách gpt4 ai đã đi

ios - UIView 边界/框架与绘制矩形不匹配

In lại 作者:行者123 更新时间:2023-11-30 13:08:23 32 4
mua khóa gpt4 Nike

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

我不明白为什么从drawRect函数调用的边界/框架宽度/高度属性与初始化程序中使用的框架/边界高度和宽度不一致。有人能解释一下吗?

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

biên tập:

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

我注意到drawRect参数的文档指出

The portion of the view's bounds that needs to be updated. The first time your view is drawn, this rectangle is typically the entire visible bounds of your view. However, during subsequent drawing operations, the rectangle may specify only part of your view.

何时以及为何参数 rect 仅指定 View 的一部分?触发 setNeedsDisplay 后,在下一个绘图周期调用drawRect。 SetNeedsDisplay 不接受任何参数,所以我假设该参数代表整个 View ?

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 Câu trả lời

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

不知道你有没有调用setNeedsDisplay(_:)。你的工作就是找出答案。祝你好运。 :)

关于ios - UIView 边界/框架与绘制矩形不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39068158/

32 4 0
Bài viết được đề xuất: c# - C# 中的 Foreach struct 奇怪的编译错误
Bài viết được đề xuất: Javascript 一次获取多个 id
Bài viết được đề xuất: c# - 测量一个函数所用的时间,不明白为什么时差是负的
Bài viết được đề xuất: javascript - 页面更改时更新功能?
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com