在 swift 中以编程方式调用 drawRect()
Programmatically calling drawRect() in swift
我是 swift 的新手,我一直以编程方式完成所有编码。我终于扩展并开始使用 Interface Builder 学习一些很棒的东西,而不是什么。
我在使用 drawRect(rect: CGRect) 函数创建的 UIView class 中创建了一些很酷的自定义绘图,现在我希望能够在 class 中多次调用它在我看来布局的循环。每当我尝试以编程方式实例化视图时,似乎都没有调用 drawRect。我没有收到任何错误,只是没有绘图。
这是我用于布置自定义视图的代码,其中 TesterView 是我执行自定义绘图的自定义 UIView subclass:
func testView() {
let testerView:TesterView = TesterView()
self.view.addSubview(testerView)
testerView.translatesAutoresizingMaskIntoConstraints = false
let height = NSLayoutConstraint(item: testerView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 200)
let width = NSLayoutConstraint(item: testerView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 200)
let centerX = NSLayoutConstraint(item: testerView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
let bottom = NSLayoutConstraint(item: testerView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)
self.view.addConstraints([height, width, centerX, bottom])
}
我们将不胜感激任何帮助。我还没有开始尝试在循环中调用视图,因为我什至无法让它工作一次。我最终需要做的是循环并多次实例化 class。
您没有明确调用 drawRect:
。相反,您将 needsDisplay
属性 设置为 YES
.
来自View Programming Guide: Creating a custom view
The most common way of causing a view to redisplay is to tell it that its image is invalid. [...] NSView
defines two methods for marking a view’s image as invalid: setNeedsDisplay:
, which invalidates the view’s entire bounds rectangle, and setNeedsDisplayInRect:
, which invalidates a portion of the view.
When the actual content of your view changes, it is your responsibility to notify the system that your view needs to be redrawn. You do this by calling your view’s setNeedsDisplay
or setNeedsDisplayInRect:
method of the view.
- (void)drawRect:(CGRect)rect
[...]
You should never call this method directly yourself. To invalidate part of your view, and thus cause that portion to be redrawn, call the setNeedsDisplay
or setNeedsDisplayInRect:
method instead.
您在 drawRect:
中实现绘图,然后在需要重绘视图时调用 [myView setNeedsDisplay:YES]
(例如,对于游戏,在循环中)。
我是 swift 的新手,我一直以编程方式完成所有编码。我终于扩展并开始使用 Interface Builder 学习一些很棒的东西,而不是什么。
我在使用 drawRect(rect: CGRect) 函数创建的 UIView class 中创建了一些很酷的自定义绘图,现在我希望能够在 class 中多次调用它在我看来布局的循环。每当我尝试以编程方式实例化视图时,似乎都没有调用 drawRect。我没有收到任何错误,只是没有绘图。 这是我用于布置自定义视图的代码,其中 TesterView 是我执行自定义绘图的自定义 UIView subclass:
func testView() {
let testerView:TesterView = TesterView()
self.view.addSubview(testerView)
testerView.translatesAutoresizingMaskIntoConstraints = false
let height = NSLayoutConstraint(item: testerView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 200)
let width = NSLayoutConstraint(item: testerView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 200)
let centerX = NSLayoutConstraint(item: testerView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
let bottom = NSLayoutConstraint(item: testerView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)
self.view.addConstraints([height, width, centerX, bottom])
}
我们将不胜感激任何帮助。我还没有开始尝试在循环中调用视图,因为我什至无法让它工作一次。我最终需要做的是循环并多次实例化 class。
您没有明确调用 drawRect:
。相反,您将 needsDisplay
属性 设置为 YES
.
来自View Programming Guide: Creating a custom view
The most common way of causing a view to redisplay is to tell it that its image is invalid. [...]
NSView
defines two methods for marking a view’s image as invalid:setNeedsDisplay:
, which invalidates the view’s entire bounds rectangle, andsetNeedsDisplayInRect:
, which invalidates a portion of the view.
When the actual content of your view changes, it is your responsibility to notify the system that your view needs to be redrawn. You do this by calling your view’s
setNeedsDisplay
orsetNeedsDisplayInRect:
method of the view.
- (void)drawRect:(CGRect)rect
[...]
You should never call this method directly yourself. To invalidate part of your view, and thus cause that portion to be redrawn, call thesetNeedsDisplay
orsetNeedsDisplayInRect:
method instead.
您在 drawRect:
中实现绘图,然后在需要重绘视图时调用 [myView setNeedsDisplay:YES]
(例如,对于游戏,在循环中)。