setNeedsLayout 调用时会调用 - (void)layoutSubviews
setNeedsLayout 在Doc中的定义为:
Call this method on your application’s main thread when you want to adjust the layout of a view’s subviews. This method makes a note of the request and returns immediately. Because this method does not force an immediate update, but instead waits for the next update cycle, you can use it to invalidate the layout of multiple views before any of those views are updated. This behavior allows you to consolidate all of your layout updates to one update cycle, which is usually better for performance.
- (void)layoutSubviews方法在doc中的定义为:
The default implementation of this method does nothing on iOS 5.1 and earlier. Otherwise, the default implementation uses any constraints you have set to determine the size and position of any subviews.
Subclasses can override this method as needed to perform more precise layout of their subviews. You should override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want. You can use your implementation to set the frame rectangles of your subviews directly.
You should not call this method directly. If you want to force a layout update, call the method instead to do so prior to the next drawing update. If you want to update the layout of your views immediately, call the method.
由上可见,layoutSubviews 方法是用来设定subview的位置的方法, 是不能手动调用的。只能通过调用setNeedsLayout 方法,来让系统自动调用layoutSubviews方法。
setNeedsDisplay 调用时, 系统会自动调用 drawRect 方法。
setNeedsDisplay 在Doc中的定义如下:
You should use this method to request that a view be redrawn only when the content or appearance of the view change. If you simply change the geometry of the view, the view is typically not redrawn. Instead, its existing content is adjusted based on the value in the view’s property. Redisplaying the existing content improves performance by avoiding the need to redraw content that has not changed.