gpt4 book ai didi

iphone - drawRect、layoutSubViews 和 setNeedsDisplay 方法的重要性

转载 作者:行者123 更新时间:2023-12-03 20:00:48 24 4
gpt4 key购买 nike

drawRect、layoutSubViews 和 setNeedsDisplay 方法的重要性是什么。我们应该在哪些情况下使用它们?

最佳答案

正如文档中所述:

drawRect: Draws the receiver’s image within the passed-in rectangle. 

Parameters

rect : A rectangle defining the area to restrict drawing to.

您可以使用此方法在 View 的特定矩形内绘制 UIView。

  • (void)setNeedsDisplay

By default, geometry changes to a view automatically redisplays the view without needing to invoke the drawRect: method. Therefore, you need to request that a view redraw only when the data or state used for drawing a view changes. In this case, send the view the setNeedsDisplay message. Any UIView objects marked as needing display are automatically redisplayed when the application returns to the run loop.

我认为这个方法可以让你控制是否希望系统自动为你重绘你的UIView。如果你想通过自己的状态来控制流程,例如:如果用户单击此按钮,则重绘;否则,不要。

布置 subview 。

  • (void)layoutSubviews

Overridden by subclasses to layout subviews when layoutIfNeeded is invoked. The default implementation of this method does nothing.

我对这种方法没有太多经验。我可以告诉你的是,你需要重写它来布局你的 subview

关于iphone - drawRect、layoutSubViews 和 setNeedsDisplay 方法的重要性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3544889/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com