Layoutsubviews Method explain the Layoutsubviews method according to the official Apple Help document:
This method is used to redefine the position and size of child elements. When subclasses override this method, they are used to implement a more precise layout of UI elements. If you want the layout to refresh again, call setneedslayout, which means that the Setneedslayout method defaults to the Layoutsubviews method.
Many times the system automatically calls the Layoutsubviews method:
1. Initialization does not trigger Layoutsubviews, but is triggered when a frame that is not Cgrectzero is set.
2.addSubview will trigger Layoutsubviews
3. Setting the frame of the view will trigger Layoutsubviews, of course, if the frame value has changed before and after the setting
4. Scrolling a uiscrollview will trigger layoutsubviews
5. Rotate screen to trigger the Layoutsubviews event on the parent UIView
6. Changing the size of a uiview also triggers the Layoutsubviews event on the parent UIView
Note: The Setneedslayout method does not immediately refresh, and immediate refresh requires calling the Layoutsubviews method.
Setneedsdisplay MethodThe method similar to the Setneedslayout method is the Setneedsdisplay method. The DrawRect method is called automatically when the method is called. The DrawRect method is mainly used for drawing.
Summary
So, when you need to refresh the layout, use the Setneedslayout method, and when you need to re-paint, call the Setneedsdisplay method. Extension:
When we customize UI controls, we need to override some methods:
The UIView control is just a blank area of a rectangle and has no content. Other UI controls for iOS apps inherit UIView These UI controls draw skins on empty areas provided by UIView.
Implementation of UI-based controls developers can develop custom controls for projects--developers can inherit UIView to derive customized controls when the UI controls provided by the iOS system are not enough to meet the project's needs.
When the developer intends to derive his own UI control, first define a subclass that inherits the view base class and then override one or more methods of the view class that can often be overridden by the user as follows.
initWithFrame: The program created UI control is often called to perform initialization, so if you need to perform some extra initialization on the UI control, you can override the method to implement it.
Initwithcoder: The method is called automatically by the program after the control is loaded in the nib file. Therefore, if the program needs to perform a custom initialization after the control is loaded in the nib file, it can be implemented by overriding the method.
DrawRect: If the program needs to draw the contents of the control itself, it can be implemented by overriding the method.
Layoutsubviews If a program needs more precise control over the layout of the child controls contained by the control, it can be implemented by overriding the method.
Didaddsubview: The method is fired when the control adds a child control to completion.
Willremovesubview: This method fires when the control is about to delete a child control.
Willmovetosuperview: This method is fired when the control is about to be added to its parent control.
Didmovetosuperview This method is fired when the control is added to the parent control when it is finished.
Willmovetowindow: This method is fired when the control is about to be added to the window.
Didmovetowindow This method is fired when the control is added to the window to complete.
Touchesbegan:withevent: The method is fired when the user's finger starts to touch the control.
Touchesmoved:withevent: The method is fired when the user's finger moves on the control.
Touchesended:withevent: The method is fired when the user's finger finishes touching the control.
Touchescancelled:withevent: The method is fired when the user cancels the control touch.
When you need to develop a custom view, the developer does not need to rewrite all of the methods listed above but rewrite the above-mentioned methods based on business needs. For example, the following example of a small ball that follows the finger movement only overrides the DrawRect: method.
#import "FKCustomView.h"
@implementation FKCustomView
// 定义两个变量记录当前触碰点的坐标
int
curX;
int
curY;
- (
void
) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// 获取触碰事件的UITouch事件
UITouch *touch = [touches anyObject];
// 得到触碰事件在当前组件上的触碰点
CGPoint lastTouch = [touch locationInView:self];
// 获取触碰点的坐标
curX = lastTouch.x;
curY = lastTouch.y;
// 通知该组件重绘
[self setNeedsDisplay];
}
// 重写该方法来绘制该UI控件
- (
void
)drawRect:(CGRect)rect
{
// 获取绘图上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 设置填充颜色
CGContextSetFillColorWithColor(ctx, [[UIColor redColor] CGColor]);
// 以触碰点为圆心绘制一个圆形
CGContextFillEllipseInRect(ctx, CGRectMake(curX - 10, curY - 10, 20, 20));
}
@end
Reference: Http://blog.sina.com.cn/s/blog_7b9d64af0101ae7q.html
http://www.aiuxian.com/article/p-2244871.html
http://www.jianshu.com/p/eb2c4bb4e3f1
iOS development: Setneedslayout and Setneedsdisplay differences