iOSSharing #9 | 2019-05-19

栏目: IOS · 发布时间: 4年前

内容简介:用于更新视图以及其子视图布局,不会立即更新,是一个异步方法,需要在主线程调用。该方法将视图以及其子视图标记,在下一个更新周期执行更新操作,不知道具体更新时间。关于用于更新视图以及其子视图布局,立即更新,不会等待更新周期,从根视图开始更新视图子树,若无待更新的布局,直接退出。

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

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

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

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

关于 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 进行视图刷新。

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

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 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
loadView
viewDidLoad
viewWillAppear
viewDidAppear
viewWillDisappear
viewDidDisappear
viewWillUnload
viewDidUnload
dealloc

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 表示动画是否成功执行完毕。可以将动画执行完成后需要执行的代码写在这里

以上所述就是小编给大家介绍的《iOSSharing #9 | 2019-05-19》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

黑客

黑客

Steven Levy / 赵俐、刁海鹏、田俊静 / 机械工业出版社华章公司 / 2011-10-31 / 69.00元

黑客文化和伦理的奠基之作,计算机专业人士必读。 二十五周年新版,涵盖比尔·盖茨、马克·扎克伯格、理查德·斯托曼、史蒂夫·沃兹尼克等著名黑客的最新资料。 多年前,射击游戏之父、Doom游戏的作者约翰·卡马克由于读到本书,坚定了游戏开发的决心。 谷歌首席信息官本·弗里德也是本书的忠实读者。 探寻黑客文化的本质,体会黑客精神的精髓。一起来看看 《黑客》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器