iOSSharing #9 | 2019-05-19

目录

1. setNeedsLayout、layoutIfNeeded与layoutSubviews区别?

2. UIView与CALayer的区别?

3. loadView何时被调用?它有什么做用?默认实现是怎么样的?

4. UIViewController的完整生命周期?

5. UIView动画支持的属性有哪些?

1. setNeedsLayout、layoutIfNeeded与layoutSubviews区别?

(1)、setNeedsLayout

用于更新视图以及其子视图布局,不会当即更新,是一个异步方法,须要在主线程调用。该方法将视图以及其子视图标记,在下一个更新周期执行更新操做,不知道具体更新时间。缓存

关于setNeedsLayout,官方文档描述以下:app

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.异步

(2)、layoutIfNeeded

用于更新视图以及其子视图布局,当即更新,不会等待更新周期,从根视图开始更新视图子树,若无待更新的布局,直接退出。ide

关于layoutIfNeeded,官方文档描述以下:布局

Use this method to force the view to update its layout immediately. When using Auto Layout, the layout engine updates the position of views as needed to satisfy changes in constraints. Using the view that receives the message as the root view, this method lays out the view subtree starting at the root. If no layout updates are pending, this method exits without modifying the layout or calling any layout-related callbacks.动画

(3)、layoutSubviews

通常是在autoresizing或者 constraint-based的状况下重写此方法。经常使用场景是当视图不是使用frame初始化的时候,咱们能够在此方法进行一些精细化布局。如子视图相对于父视图使用约束布局,子视图init时,不具备frame直到约束创建,由于不知道约束创建完成时机,而咱们又确实须要frame进行一些计算,为确保计算时拿到的frame不为空,此时,咱们能够将计算的过程放于此,并配合setNeedsLayout或者layoutIfNeeded进行视图刷新。ui

关于layoutSubviews,官方文档描述以下:this

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.spa

You should not call this method directly. If you want to force a layout update, call the setNeedsLayout method instead to do so prior to the next drawing update. If you want to update the layout of your views immediately, call the layoutIfNeeded method.线程

2. UIView与CALayer的区别?

  • UIView能够响应事件,CALayer不能够响应事件
  • 一个 Layer 的 frame 是由它的 anchorPoint,position,bounds,和 transform 共同决定的,而一个 View 的 frame 只是简单的返回 Layer的 frame
  • UIView主要是对显示内容的管理而 CALayer 主要侧重显示内容的绘制
  • 在作 iOS 动画的时候,修改非 RootLayer的属性(譬如位置、背景色等)会默认产生隐式动画,而修改UIView则不会。

3. loadView何时被调用?它有什么做用?默认实现是怎么样的?

1)、何时被调用?

每次访问UIViewController的view(好比controller.view、self.view)并且view为nil,loadView方法就会被调用。

2)、有什么做用?

loadView方法是用来负责建立UIViewController的view

3)、默认实现是怎样的?

默认实现即[super loadView]里面作了什么事情。

a)、 它会先去查找与UIViewController相关联的xib文件,经过加载xib文件来建立UIViewController的view

  • 若是在初始化UIViewController指定了xib文件名,就会根据传入的xib文件名加载对应的xib文件
[[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
复制代码
  • 若是没有明显地传xib文件名,就会加载跟UIViewController同名的xib文件
[[MyViewController alloc] init]; // 加载MyViewController.xib
复制代码

b) 、若是没有找到相关联的xib文件,就会建立一个空白的UIView,而后赋值给UIViewController的view属性,大体以下

self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];

复制代码

[super loadView]里面就大体完成a)和b)中叙述的内容

Tips:
若要本身重写loadView方法,此时为节省开销,应避免调用[super loadView]方法。

4. UIViewController的完整生命周期?

按照执行顺序排列:

  • init初始化ViewController
  • loadView当view须要被展现而它倒是nil时,viewController会调用该方法。若是代码维护View的话须要重写此方法,使用xib维护View的话不用重写。
  • viewDidLoad执行完loadView后继续执行viewDidLoad,loadView时尚未view,而viewDidLoad时view已经建立好了。
  • viewWillAppear视图将出如今屏幕以前,立刻这个视图就会被展示在屏幕上了;
  • viewDidAppear视图已在屏幕上渲染完成 当一个视图被移除屏幕而且销毁的时候的执行顺序,这个顺序差很少和上面的相反;
  • viewWillDisappear视图将被从屏幕上移除以前执行
  • viewDidDisappear视图已经被从屏幕上移除,用户看不到这个视图了
  • viewWillUnload若是当前有能被释放的view,系统会调用viewWillUnload方法来释放view
  • viewDidUnload当系统内存吃紧的时候会调用该方法,在iOS 3.0以前didReceiveMemoryWarning是释放无用内存的惟一方式,可是iOS 3.0及之后viewDidUnload方法是更好的方式。在该方法中将全部IBOutlet(不管是property仍是实例变量)置为nil(系统release view时已经将其release掉了)。在该方法中释放其余与view有关的对象、其余在运行时建立(但非系统必须)的对象、在viewDidLoad中被建立的对象、缓存数据等。通常认为viewDidUnload是viewDidLoad的镜像,由于当view被从新请求时,viewDidLoad还会从新被执行。
  • dealloc视图被销毁,此处须要对你在init和viewDidLoad中建立的对象进行释放.关于viewDidUnload :在发生内存警告的时候若是本视图不是当前屏幕上正在显示的视图的话,viewDidUnload将会被执行,本视图的全部子视图将被销毁以释放内存,此时开发者须要手动对viewLoad、viewDidLoad中建立的对象释放内存。由于当这个视图再次显示在屏幕上的时候,viewLoad、viewDidLoad 再次被调用,以便再次构造视图。

5. UIView动画支持的属性有哪些?

  • frame: 位置和大小
  • bounds
  • center
  • transform
  • alpha
  • backgroundColor
  • contentStretch

基本用法:

+ (void)animateWithDuration:(NSTimeInterval)duration 
                      delay:(NSTimeInterval)delay 
                    options:(UIViewAnimationOptions)options 
                 animations:(void (^)(void))animations 
                 completion:(void (^)(BOOL finished))completion;
复制代码

参数说明:

  • duration :整个动画持续的时间
  • delay:动画在多久以后开始,值为 0 表示代码执行到这里后动画马上开始
  • options:一些有关动画的设置,例如想要动画刚开始比较快,到快结束时比较慢,都在这里设置。
  • animations:在这个 block 中写入你想要执行的代码便可。block 中对视图的动画属性所作的改变都会生成动画
  • completion:动画完成后会调用,finished 表示动画是否成功执行完毕。能够将动画执行完成后须要执行的代码写在这里
相关文章
相关标签/搜索