UIView的一些基础概念

Windows本身没有任何可见的内容,但是为views提供了一个基本的容器。Views定义了window的一部分矩形区域,用来填充需要显示的内容。views负责绘制内容,处理多点触摸事件,管理子视图的布局。大多数情况下,windows不会发生改变,当它被创建之后,只有windows显示的view会发生改变。

View and Window 结构

views和Core Animation layers一起处理视图内容的呈现和动画。每个UIKit中的view都有一个layer对象支持(CALayer),处理视图相关的动画。虽然大多数时候操作是通过UIView接口,但是某些情况下你可以通过layer来控制更多的行为。

Window本身是一个普通的UIView对象,充当一个容器view。每一个view都可以通过其layer属性来访问layer对象。




view的绘制代码应当尽可能少的被调用,当调用时,结果会被Core Animation缓存,以尽可能的在以后被重复使用。复用已显示的内容减少了用来更新view的绘图周期。复用在使用动画时特别重要,复用比创建新内容更加便宜。视图结构和子视图管理


当一个视图包含另外一个视图时,一个parent-child 关系就被建立。如果子视图完全不透明,那么它会遮蔽父视图相应的内容。每个父视图都将其子视图按照一个有序数字来保存,这个数字的顺序同样表示子视图之间的关系。改变父视图的大小会导致子视图的位置和大小改变。

视图层次结构中视图的排列同样决定了程序如何响应时间。当触摸发生在一个特定的视图中,系统会发送一个包含触摸信息的事件对象到该视图。但是如果该视图没有处理该事件,则将该事件传递给其父视图,如果父视图也不响应,则沿着响应链一直向上传递。如果没有对象响应事件,则最终被传递至applicatin对象,一般被丢弃。

视图绘制周期


UIView使用请求式(on-demand)绘制模式来呈现内容。当一个视图初次出现,系统会请求其绘制其内容。系统会截取该内容的快照,用来作为视图的视觉表达。快照图像被很多涉及视图的操作复用。如果你改变了内容,需要通知系统视图内容发生改变,然后系统重复前面的过程。

当视图内容改变时,不需要直接重新绘制,而是通过调用setNeedsDisplay or setNeedsDisplayInRect:通知系统视图的内容发生改变,需要重绘。系统直到当前run loop的最后才初始化绘制操作,所有的改变会同时一起呈现。

对于自定义UIView的子类,重载drawRect:方法来绘制视图内容。

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.当视图初次显示时,结果会被保存在一个底层位图中。之后视图的几何属性发生变化时,contentMode属性决定视图的变化。 

The content mode of a view is applied whenever you do the following:

  • 改变视图frame和bounds的长宽时

  • Assign a transform that includes a scaling factor to the view’s transform property.

默认情况下,contentMode属性为UIViewContentModeScaleToFill


你可以将content mode设置为 UIViewContentModeRedraw value当视图几何属性发生变化时,会调用视图的drawRect:方法。一般来说,应当尽量避免使用该值。

Built-In Animation Support

Many properties of the UIView class are animatable—that is, semiautomatic support exists for animating from one value to another. To perform an animation for one of these animatable properties, all you have to do is:

  1. Tell UIKit that you want to perform an animation.

  2. Change the value of the property.

Among the properties you can animate on a UIView object are the following:

  • frame—Use this to animate position and size changes for the view.

  • bounds—Use this to animate changes to the size of the view.

  • center—Use this to animate the position of the view.

  • transform—Use this to rotate or scale the view.

  • alpha—Use this to change the transparency of the view.

  • backgroundColor—Use this to change the background color of the view.

  • contentStretch—Use this to change how the view’s contents stretch.


视图几何坐标系统


坐标值用浮点数表示。

Frame, Bounds, and Center 属性之间的关系

  • frame rectangle, 指定视图在父视图中的大小和位置.

  • bounds rectangle, 指定自身大小和原点位置.

  • The center property 包含视图中点在父视图坐标系中的位置.

center属性总是有效,即使缩放或旋转因子添加到了视图的transform中,而此时frame属性是无效的。


属性

当 framebounds, and center 属性中某个发生改变时:

  • 当你设置frame属性时,bounds和center也会随之发生变化.

  • 当你改变center属性时,frame属性的origin值会随之变化.

  • 当你改变bounds的大小时,frame的size值也会随之发生变化.

默认情况下,视图的frame不会根据父视图的frame修剪,因此如果视图存在于父视图的frame外时,会全部显示出来。你可以将父视图的clipsToBounds设置为YES来对子视图进行修剪。无论子视图是否被父视图修剪,触摸事件如果发生于父视图frame之外的子视图中时,触摸事件会被丢弃。

坐标系变换

仿射变换是指定点在坐标系间变换的矩阵,如何应用仿射变换取决与上下文:

  • To modify your entire view, modify the affine transform in the transform property of your view.

  • To modify specific pieces of content in your view’s drawRect: method, modify the affine transform associated with the active graphics context.

通常修改视图的transform属性来实现动画,比如你可以创建一个以视图中心旋转的动画


Points Versus Pixels

  • One point does not necessarily correspond to one pixel on the screen.

Runtime Interaction Model For Views



  1. 用户触摸屏幕.

  2. 硬件报告触摸事件给UIKit框架.

  3. UIKit框架打包触摸到UIEvent对象,发送至被触摸的视图

  4. 视图响应触摸事件

  5. 如果视图的几何属性发生变化,UIKit根据以下规则更新子视图:

    1. 根据autoresizing规则修改视图

    2. 如果你的视图实现了layoutSubviews方法,UIKit调用。你可以在你的子类中重载此方法来定位子视图

  6. 如果视图某部分需要重绘,UIKit则让视图重绘自身。如果自定义视图定义了drawRect方法,UIKit则调用此方法来更新视图。

  7. Any updated views are composited with the rest of the application’s visible content and sent to the graphics hardware for display.

  8. The graphics hardware transfers the rendered content to the screen.





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值