iOS development: Setneedslayout and Setneedsdisplay differences

Source: Internet
Author: User

1. Layoutsubviews method
    • 1.1 Explanation of 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.
    • 1.2 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
    • 1.3 Note: The Setneedslayout method does not flush immediately, and the layoutifneeded method needs to be called immediately.
2. Setneedsdisplay method
  • The 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.
  • 2.1 Summary
    • So, when you need to refresh the layout, use the Setneedslayout method, and when you need to re-paint, call the Setneedsdisplay method.
  • 2.2 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:             前面已经见到程序创建UI控件时常常会调用该方法执行初始化因此如果你需要对UI控件执行一些额外的初始化即可通过重写该方法来实现。 initWithCoder:             程序通过在nib文件中加载完该控件后会自动调用该方法。因此如果程序需要在nib文件中加载该控件后执行自定义初始化则可通过重写该方法来实现。 drawRect:                  如果程序需要自行绘制该控件的内容则可通过重写该方法来实现。 layoutSubviews             如果程序需要对该控件所包含的子控件布局进行更精确的控制可通过重写该方法来实现。 didAddSubview:             当该控件添加子控件完成时将会激发该方法。 willRemoveSubview:         当该控件将要删除子控件时将会激发该方法。 willMoveToSuperview:       当该控件将要添加到其父控件中时将会激发该方法。 didMoveToSuperview         当把该控件添加到父控件完成时将会激发该方法。 willMoveToWindow:          当该控件将要添加到窗口中时将会激发该方法。 didMoveToWindow            当把该控件添加到窗口完成时将会激发该方法。 touchesBegan:withEvent:    当用户手指开始触碰该控件时将会激发该方法。 touchesMoved:withEvent:    当用户手指在该控件上移动时将会激发该方法。 touchesEnded:withEvent:    当用户手指结束触碰该控件时将会激发该方法。  touchesCancelled:withEvent:用户取消触碰该控件时将会激发该方法。
      • 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

iOS development: Setneedslayout and Setneedsdisplay differences

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.