iOS UIView

来源:互联网 发布:笔记本电脑散热器软件 编辑:程序博客网 时间:2024/05/16 19:53

iOS UIView

View Programming Guide for iOS

深入了解UIView,记录下View Programming Guide for iOS中相关内容。

UIView的事件响应顺序

When a touch occurs inside a specific view, the system sends an event object with the touch information directly to that view for handling. However, if the view does not handle a particular touch event, it can pass the event object along to its superview. If the superview does not handle the event, it passes the event object to its superview, and so on up the responder chain. Specific views can also pass the event object to an intervening responder object, such as a view controller. If no object handles the event, it eventually reaches the application object, which generally discards it.

Content Modes
Each view has a content mode that controls how the view recycles its content in response to changes in the view’s geometry(几何结构) and whether it recycles its content at all. When a view is first displayed, it renders its content as usual and the results are captured in an underlying(潜在的) bitmap. After that, changes to the view’s geometry do not always cause the bitmap to be recreated. Instead, the value in the contentMode property determines whether the bitmap should be scaled to fit the new bounds or simply pinned to one corner or edge of the view.

如下的动作会导致content mode被应用:

  • Change the width or height of the view’s frame or bounds rectangles.
  • Assign a transform that includes a scaling factor to the view’s transform property.

contentmode


默认的view的frame超出superview的部分是不会被裁剪的,把clipsToBounds 设为YES可以达到裁剪的结果。裁剪之后,发生在view被裁剪区的触摸事件不会被deliver到view上。


setNeedsLayout vs layoutIfNeeded

参考setNeedsLayout vs layoutIfNeeded Explained

system会标记需要redrawn的view,在update cycle中来redrawing

setNeedsLayout告知system你想要layout并redraw所有的view及其子view,在update cycle的时候。这是一个异步的activity,你不知道update cycle什么时候会到来。

layoutIfNeeded是一个同步的过程,告知system你想要立即layout和redraw这个view及其子view,需要立即做,不要等待update cycle。

所以layoutIfNeeded会立即更新,而setNeedsLayout等待至下一个update cycle


View运行时交互模型(The Runtime Interaction Model for Views)

UIKit interactions with your view objects

  1. user触摸屏幕
  2. 硬件把touch event报告给UIKit
  3. UIKit把touch包装成一UIEvent对象,并把它分派到对应的view。
  4. view的事件处理code来响应event。例如,你的代码可能如下:

    • 改变view或者其subview的属性(frame, bounds, alpha等)
    • 调用setNeedsLayout方法,把view(或者其subview)mark为需要更新布局
    • 调用setNeedsDisplay或者setNeedsDisplayInRect:方法,把view(或者其subview)mark为需要被重绘
    • 通知controller改变部分数据
  5. 如果view的geometry有任何的改变,UIKit按照以下的规则来更新它的subviews:
    a. 如果你配置了autoresizing,UIkit根据这些规则来调整每个view
    b.如果view实现了layoutSubviews方法,UIKit会调用这个方法

  6. 如果view的一部分被mark为需要被redrawn,UIKit会要求view重绘它自身

  7. 更新后的view会与应用的其它可见的content合成,然后给硬件来显示
  8. 硬件把渲染后的内容显示在屏幕上

Windows

一个Window对象有如下的功能:

  • It contains your application’s visible content.
  • It plays a key role in the delivery of touch events to your views and other application objects.
  • It works with your application’s view controllers to facilitate orientation changes.
0 0