UIKit的类层次结构
2013-06-02 13:23
274 查看
根据iPhone官方的统计,App Store中游戏类应用是最多的,大概是其它应用总和的1 .5倍,在排行前20的应用中,游戏类应用超过14个。
iPhone窗口系统如下:
ü UIKit.framwork
? UIScreen
? UIWindow
? UIView
ü QuartzCore.framework
? CALayer
? CAEAGLLayer
本文主要讲解UIKit.framework图形框架,其中UIScreen提供了屏幕的基本系统,定义如下:
//// UIScreen.h
// UIKit
//
// Copyright 2007-2009 Apple Inc. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIKitDefines.h>
UIKIT_EXTERN_CLASS @interface UIScreen : NSObject {
@private
CGRect _bounds;
}
+ (UIScreen *)mainScreen;
@property(nonatomic,readonly) CGRect bounds; // Bounds of entire screen in points
@property(nonatomic,readonly) CGRect applicationFrame; // Frame of application screen area in points (i.e. entire screen minus status bar if visible)
@end
这个类提供了屏幕的bounds(即可绘制区域drawable),屏幕的尺寸和位置,而接口mainScreen()获取当前窗口的主屏幕。
UIWindow是UIView的子类,UIView是所有屏幕视图的SuperClass,比如UIButton、UILabel、UIImageView等,UIView类中定义如下:
…
@property(nonatomic,readonly) UIView *superview;
@property(nonatomic,readonly,copy) NSArray *subviews;
@property(nonatomic,readonly) UIWindow *window;
- (void)removeFromSuperview;
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;
- (void)addSubview:(UIView *)view;
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;
- (void)bringSubviewToFront:(UIView *)view;
- (void)sendSubviewToBack:(UIView *)view;
- (void)didAddSubview:(UIView *)subview;
- (void)willRemoveSubview:(UIView *)subview;
- (void)willMoveToSuperview:(UIView *)newSuperview;
- (void)didMoveToSuperview;
- (void)willMoveToWindow:(UIWindow *)newWindow;
- (void)didMoveToWindow;
…
可见,利用UIView类可以获取当前视图的“父视图”、“子视图数组”,还可以添加一个“子视图”到当前的视图、删除“子视图”、插入“子视图”等操作。
它们的继承关系如下图:
UIResponder – UIView – 自定义视图
|
UIButton、UILabel、UIImageView等
本文的主要任务是创建一个全屏显示的自定义窗口。
新建一个“Window-based Application”项目,输入项目名字“CrazyWindGameEngine”,然后Build&Go可以看到一个浅灰色背景的屏幕,这个默认的屏幕存在几个问题:
首先需要隐藏“Status Bar”,这个通过在CrazyWindGameEngine-info.plist中添加“Status bar is initially hidden”并勾选,Phone屏幕的构造如下图:
其次,这个模板程序是通过从CrazyWindGameEngine-info.plist文件的“Main nib file base name”属性中读取MainWindow.xib而生成窗口的,所以需要把模板相应的模块去掉并加入自己生成的窗口,这需要以下几个步骤:
? 删除CrazyWindGameEngine-info.plist中的Main nib file base name属性和相应的值。
? 到Resources目录下删除MainWindow.xib文件。
? 到main.m中修改代码如下:
? int retVal = UIApplicationMain(argc, argv, nil, @"CrazyWindGameEngineAppDelegate");
? CrazyWindGameEngineAppDelegate.h中变量window的IBOutlet关键字去掉(也可以保留,不影响)。
经过以上的步骤,模板生成的窗口就被删除掉了,接下来需要添家自己定义的窗口,代码如下:
#import "CrazyWindGameEngineAppDelegate.h"
@implementation CrazyWindGameEngineAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// setup the main window
CGRect windowRect = [[UIScreen mainScreen] bounds];
window = [[UIWindow alloc] initWithFrame:windowRect];
[window setBackgroundColor:[UIColor redColor]];
…
[window makeKeyAndVisible];
}
上面的代码会产生一个“红色背景的窗口”,在前面讲过UIView类提供了“添加视图”、“插入视图”、“删除视图”等操作,而UIButton、 UILabel、UIImageView等类继承自UIView,所以下面的代码中尝试添加一个UIButton、一个UILabel、一个 UIImageView到新产生的窗口中,如下:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// setup the main window
CGRect windowRect = [[UIScreen mainScreen] bounds];
window = [[UIWindow alloc] initWithFrame:windowRect];
[window setBackgroundColor:[UIColor redColor]];
// Set up the background image
UIImageView *mBgView = [[[UIImageView alloc] initWithImage: [UIImage applicationImageNamed:@"screenshot.png"]] autorelease];
// 旋转UIImageView
float rotateAngle = M_PI/2;
CGAffineTransform transform = CGAffineTransformMakeRotation(rotateAngle);
mBgView.transform = transform;
[window setContentView: mBgView];
[mBgView release];
// add a UILabel
CGRect labelRect = [[UIScreen mainScreen] applicationFrame];
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(labelRect.origin.x+30, labelRect.origin.y+150, labelRect.size.width-60, labelRect.size.height-300)];
label.text = @"CrazyWindGameEngine";
label.backgroundColor = [UIColor blackColor];
label.shadowColor = [UIColor whiteColor];
label.textAlignment = UITextAlignmentCenter;
label.font = [UIFont systemFontOfSize:22.0];
label.textColor = [UIColor grayColor];
[window addSubview:label];
[label release];
// add a UIButton
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0.0f, 0.0f, 80.0f, 30.0f)];
[button setCenter:CGPointMake(160.0f,208.0f)];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button setTitle:@"Start" forState:UIControlStateNormal];
[button addTarget:self action:@selector(playSound:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundColor:[UIColor redColor]];
[button setAlpha:20];
[window addSubview:button];
[button release];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
- (void)dealloc {
[window release];
[super dealloc];
}
@end
其中我们利用了UIImageView类的旋转方法transform属性对图片进行旋转,运行效果如下图:
转自http://blog.csdn.net/dongfengsun/archive/2009/11/14/4811314.aspx
iPhone窗口系统如下:
ü UIKit.framwork
? UIScreen
? UIWindow
? UIView
ü QuartzCore.framework
? CALayer
? CAEAGLLayer
本文主要讲解UIKit.framework图形框架,其中UIScreen提供了屏幕的基本系统,定义如下:
//// UIScreen.h
// UIKit
//
// Copyright 2007-2009 Apple Inc. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIKitDefines.h>
UIKIT_EXTERN_CLASS @interface UIScreen : NSObject {
@private
CGRect _bounds;
}
+ (UIScreen *)mainScreen;
@property(nonatomic,readonly) CGRect bounds; // Bounds of entire screen in points
@property(nonatomic,readonly) CGRect applicationFrame; // Frame of application screen area in points (i.e. entire screen minus status bar if visible)
@end
这个类提供了屏幕的bounds(即可绘制区域drawable),屏幕的尺寸和位置,而接口mainScreen()获取当前窗口的主屏幕。
UIWindow是UIView的子类,UIView是所有屏幕视图的SuperClass,比如UIButton、UILabel、UIImageView等,UIView类中定义如下:
…
@property(nonatomic,readonly) UIView *superview;
@property(nonatomic,readonly,copy) NSArray *subviews;
@property(nonatomic,readonly) UIWindow *window;
- (void)removeFromSuperview;
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;
- (void)addSubview:(UIView *)view;
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;
- (void)bringSubviewToFront:(UIView *)view;
- (void)sendSubviewToBack:(UIView *)view;
- (void)didAddSubview:(UIView *)subview;
- (void)willRemoveSubview:(UIView *)subview;
- (void)willMoveToSuperview:(UIView *)newSuperview;
- (void)didMoveToSuperview;
- (void)willMoveToWindow:(UIWindow *)newWindow;
- (void)didMoveToWindow;
…
可见,利用UIView类可以获取当前视图的“父视图”、“子视图数组”,还可以添加一个“子视图”到当前的视图、删除“子视图”、插入“子视图”等操作。
它们的继承关系如下图:
UIResponder – UIView – 自定义视图
|
UIButton、UILabel、UIImageView等
本文的主要任务是创建一个全屏显示的自定义窗口。
新建一个“Window-based Application”项目,输入项目名字“CrazyWindGameEngine”,然后Build&Go可以看到一个浅灰色背景的屏幕,这个默认的屏幕存在几个问题:
首先需要隐藏“Status Bar”,这个通过在CrazyWindGameEngine-info.plist中添加“Status bar is initially hidden”并勾选,Phone屏幕的构造如下图:
其次,这个模板程序是通过从CrazyWindGameEngine-info.plist文件的“Main nib file base name”属性中读取MainWindow.xib而生成窗口的,所以需要把模板相应的模块去掉并加入自己生成的窗口,这需要以下几个步骤:
? 删除CrazyWindGameEngine-info.plist中的Main nib file base name属性和相应的值。
? 到Resources目录下删除MainWindow.xib文件。
? 到main.m中修改代码如下:
? int retVal = UIApplicationMain(argc, argv, nil, @"CrazyWindGameEngineAppDelegate");
? CrazyWindGameEngineAppDelegate.h中变量window的IBOutlet关键字去掉(也可以保留,不影响)。
经过以上的步骤,模板生成的窗口就被删除掉了,接下来需要添家自己定义的窗口,代码如下:
#import "CrazyWindGameEngineAppDelegate.h"
@implementation CrazyWindGameEngineAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// setup the main window
CGRect windowRect = [[UIScreen mainScreen] bounds];
window = [[UIWindow alloc] initWithFrame:windowRect];
[window setBackgroundColor:[UIColor redColor]];
…
[window makeKeyAndVisible];
}
上面的代码会产生一个“红色背景的窗口”,在前面讲过UIView类提供了“添加视图”、“插入视图”、“删除视图”等操作,而UIButton、 UILabel、UIImageView等类继承自UIView,所以下面的代码中尝试添加一个UIButton、一个UILabel、一个 UIImageView到新产生的窗口中,如下:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// setup the main window
CGRect windowRect = [[UIScreen mainScreen] bounds];
window = [[UIWindow alloc] initWithFrame:windowRect];
[window setBackgroundColor:[UIColor redColor]];
// Set up the background image
UIImageView *mBgView = [[[UIImageView alloc] initWithImage: [UIImage applicationImageNamed:@"screenshot.png"]] autorelease];
// 旋转UIImageView
float rotateAngle = M_PI/2;
CGAffineTransform transform = CGAffineTransformMakeRotation(rotateAngle);
mBgView.transform = transform;
[window setContentView: mBgView];
[mBgView release];
// add a UILabel
CGRect labelRect = [[UIScreen mainScreen] applicationFrame];
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(labelRect.origin.x+30, labelRect.origin.y+150, labelRect.size.width-60, labelRect.size.height-300)];
label.text = @"CrazyWindGameEngine";
label.backgroundColor = [UIColor blackColor];
label.shadowColor = [UIColor whiteColor];
label.textAlignment = UITextAlignmentCenter;
label.font = [UIFont systemFontOfSize:22.0];
label.textColor = [UIColor grayColor];
[window addSubview:label];
[label release];
// add a UIButton
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0.0f, 0.0f, 80.0f, 30.0f)];
[button setCenter:CGPointMake(160.0f,208.0f)];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button setTitle:@"Start" forState:UIControlStateNormal];
[button addTarget:self action:@selector(playSound:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundColor:[UIColor redColor]];
[button setAlpha:20];
[window addSubview:button];
[button release];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
- (void)dealloc {
[window release];
[super dealloc];
}
@end
其中我们利用了UIImageView类的旋转方法transform属性对图片进行旋转,运行效果如下图:
转自http://blog.csdn.net/dongfengsun/archive/2009/11/14/4811314.aspx
相关文章推荐
- UIKit的类层次结构
- OC学习之UIKit的类层次结构图
- UIKit的类层次结构
- iOS UIKit:viewController之层次结构(1)
- 《分析服务从入门到精通读书笔记》第一章、数据分析层次结构(2)
- IIC设备驱动程序(三)————IIC设备驱动程序的层次结构
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 如何实现具有层次结构的 TreeView <二> (WPF/TreeView/Style/Template)
- 类的层次结构
- 二叉树采用二叉链表结构表示。设计并实现如下算法:输入某棵二叉树的广义表形式,建立该二叉树,并按层次遍历该二叉树。
- MDX层次结构在crossjoin函数中调用多次
- 程序员的自我修养-链接、装载与库-1 计算机层次结构
- (先序+中序建树)2137数据结构实验之求二叉树后序遍历和层次遍历
- 个人学习笔记11之--SQL 中的 图 树 层次结构
- GML对象的层次结构
- 集群:(三)微观剖析高可用集群(HA)的层次结构
- FHS 文件系统层次结构标准
- 动画类的层次结构
- SharePoint 【架构系列】-- Sharepoint的服务器端对象模型(Server Object Model) 01.物理对象层次结构
- Java:输入输出流 java.io包的层次结构