在iPhone 程序中,有两个地方最常用于创建视图和子视图, 它们是应用程序委托对象的applicationDidFinishLaunching:方法和视图控制器的loadView 方法。
调用父视图的addSubview:方法来添加视图,该方法将一个视图添加到子视图列表的最后。
调用父视图的insertSubview:...方法可以在父视图的子视图列表中间插入视图。
调用父视图的bringSubviewToFront: 、sendSubviewToBack: 、或
exchangeSubviewAtIndex:withSubviewAtIndex:方法可以对父视图的子视图进行重新排序。使用这些方法比从父视图中移除子视图并再次插入要快一些。
调用子视图(而不是父视图)的removeFromSuperview 方法可以将子视图从父视图中移除。
创建一个带有视图的窗口
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Create the window object and assign it to the
// window instance variable of the application delegate.
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.backgroundColor = [UIColor whiteColor];
// Create a simple red square
CGRect redFrame = CGRectMake(10, 10, 100, 100);
UIView *redView = [[UIView alloc] initWithFrame:redFrame];
redView.backgroundColor = [UIColor redColor];
// Create a simple blue square
CGRect blueFrame = CGRectMake(10, 150, 100, 100);
UIView *blueView = [[UIView alloc] initWithFrame:blueFrame];
blueView.backgroundColor = [UIColor blueColor];
// Add the square views to the window
[window addSubview:redView];
[window addSubview:blueView];
// Once added to the window, release the views to avoid the
// extra retain count on each of them.
[redView release];
[blueView release];
// Show the window.
[window makeKeyAndVisible];
}
当您为某个视图添加子视图时,UIKit 会向相应的父子视图发送几个消息,通知它们当前发生的状态变化。您可以在自己的定制视图中对诸如willMoveToSuperview: 、
willMoveToWindow: 、willRemoveSubview: 、didAddSubview: 、didMoveToSuperview 、和didMoveToWindow 这样的方法进行重载,以便在事件发生的前后进行必要的处理,并根据发生的变化更新视图的状态信息。
在视图层次建立之后,您可以通过视图的superview 属性来取得其父视图,或者通过subviews属性取得视图的子视图。您也可以通过isDescendantOfView:方法来判定一个视图是否在其父视图的视图层中。一个视图层次的根视图没有父视图,因此其superview 属性被设置为nil。对于当前被显示在屏幕上的视图,窗口对象通常是整个视图层次的根视图。
UIView 类定义了下面这些方法,用于在不同的视图本地坐标系统之间进行坐标转换:
convertPoint:fromView:
convertRect:fromView:
convertPoint:toView:
convertRect:toView:
UIWindow 的版本则使用窗口坐标系统。
convertPoint:fromWindow:
convertRect:fromWindow:
convertPoint:toWindow:
convertRect:toWindow:
UIView 类中包含一个tag 属性。借助这个属性,您可以通过一个整数值来标识一个视图对象。您可以通过这个属性来唯一标识视图层次中的视图,以及在运行时进行视图的检索(基于tag 标识的检索比您自行遍历视图层次要快)。tag 属性的缺省值为0。您可以通过UIView 的viewWithTag:方法来检索标识过的视图
动画块从调用UIView 的beginAnimations:context:类方法开始,而以调用commitAnimations类方法作为结束。在这两个调用之间,您可以配置动画的参数和改变希望实行动画的属性值。一旦调用commitAnimations 方法,UIKit 就会开始执行动画,即把给定属性从当前值到新值的变化过程用动画表现出来。动画块可以被嵌套,但是在最外层的动画块提交之前,被嵌套的动画不会被执行。
frame 视图的边框矩形,位于父视图的坐标系中。
bounds 视图的边界矩形,位于视图的坐标系中。
center 边框的中心,位于父视图的坐标系中。
transform 视图上的转换矩阵,相对于视图边界的中心。
alpha 视图的alpha 值,用于确定视图的透明度。
用setAnimationStartDate:方法来设置动画在commitAnimations 方法返回之后的发生日期。缺省行为是使动画立即在动画线程中执行。
用setAnimationDelay:方法来设置实际发生动画和commitAnimations 方法返回的时间点之间的间隔。
用setAnimationDuration:方法来设置动画持续的秒数。
用setAnimationCurve:方法来设置动画过程的相对速度,比如动画可能在启示阶段逐渐加速,而在结束阶段逐渐减速,或者整个过程都保持相同的速度。
用setAnimationRepeatCount:方法来设置动画的重复次数。
用setAnimationRepeatAutoreverses:方法来指定动画在到达目标值时是否自动反向播放。您可以结合使用这个方法和setAnimationRepeatCount:方法,使各个属性在初始值和目标值之间平滑切换一段时间。
您可以通过UIView 的
setAnimationDelegate: 类方法来设置委托, 并通过setAnimationWillStartSelector: 和
setAnimationDidStopSelector:方法来指定接收消息的选择器方法。消息处理方法的形式如下:
- (void)animationWillStart:(NSString *)animationID context:(void *)context;
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void
*)context;
上面两个方法的animationID 和context 参数和动画块开始时传beginAnimations:context:方法的参数相同:
animationID - 应用程序提供的字符串,用于标识一个动画块中的动画。
context - 也是应用程序提供的对象,用于向委托对象传递额外的信息。
setAnimationDidStopSelector:选择器方法还有一个参数—即一个布尔值。如果动画顺利完成,没有被其它动画取消或停止,则该值为YES。