程序结构:
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (retain, nonatomic) UIWindow *window;
// 在刚创建好的工程中, 要把属性的strong修饰词改为retain
@endAppDelegate 继承了 UIResponder 类(响应者),
其中有一个属性: window, 我们用它来显示界面,
AppDelegate 作为 UIApplication 的委托, 实现 UIApplicationDelegate 协议中的方法. 其中的方法都为optional的.
而方法:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions NS_AVAILABLE_IOS(3_0);是程序的入口, 一旦第一次打开程序, 就必定走这个方法. 在这个方法中, 要初始化window属性, 并创建一个window:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // 初始化 _window 成员变量
// 此时 _window 所指向内存空间引用计数为2, alloc一次+1, self.window 调用了一次setter方法+1
[self.window setBackgroundColor:[UIColor whiteColor]];
[self.window makeKeyAndVisible]; // 将这个window设置为主显示window, 一个程序可能有多个window, 因此, 需要标注谁是用来主显示的window
// 此步使引用计数再+1
[self.window release]; // 不要忘记release
另: AppDelegate.m 中, 不需要写便利构造器, 初始化方法, 要写dealloc:
- (void)dealloc{
[self.window release];
[super dealloc];
}UIView:
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIView : UIResponder <NSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem, UITraitEnvironment, UICoordinateSpace>UIView 继承自 UIResponder. *UIResponder is responsible for receiving events.
UIView 中有初始化方法:
- (instancetype)initWithFrame:(CGRect)frame;UIView在屏幕中定义了一个长方形的区域, 因此有CGRect 类型的 frame属性, 确定其顶点坐标, 长, 宽.
UIView有以下常用属性:
@property(nonatomic) CGRect frame;
@property(nonatomic) CGRect bounds; // default bounds is zero origin, frame size. animatable
@property(nonatomic) CGPoint center; // center is center of frame. animatable
@property(nonatomic,copy) UIColor *backgroundColor UI_APPEARANCE_SELECTOR;
@property(nonatomic,readonly) UIView *superview;
@property(nonatomic,readonly,copy) NSArray *subviews; // 该数组装有以当前UIView对象为父视图的直接子视图, 不包括孙子视图
@property(nonatomic,getter=isHidden) BOOL hidden; // 控制视图的显隐, YES为隐藏, NO为显示, 默认为NO
@property(nonatomic) CGFloat alpha; // 控制视图的透明度, 取值范围:0~1, 父视图改变, 子视图也一起改变, 孙子视图也一起改变
@property(nonatomic) NSInteger tag; // 给视图添加标记, 被加完标记的视图可以使用viewWithTag:方法取得
- (void)addSubview:(UIView *)view; // 此步使引用计数+1
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;
- (void)bringSubviewToFront:(UIView *)view;
- (void)sendSubviewToBack:(UIView *)view;
- (void)removeFromSuperview;
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index; // 插入到当前第index个元素之前
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;
- (void)sizeToFit; // Resizes and moves the receiver view so it just encloses its subviews. UILabel添加文字时,会让文字不受UILabel高度的影响,但会受高度的影响每添加一个子视图, 则会在父视图的 subviews 数组中追加该子视图, 同时, 最后添加的子视图会显示在最上面. 也就是说, 数组最后面的视图显示在最上面, 最前面的元素显示在最下面.
对子视图的移动, 子视图的子视图也会随着该子视图移动.
UIWindow:
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIWindow : UIViewUIWindow 继承自 UIView, UIWindow 没有自己的初始化方法, 因此, 初始化 UIWindow 对象时调用父类 UIView 的初始化方法, 如:
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];UIScreen定义了和基于硬件的显示相关的属性( A
UIScreen object
defines the properties associated with a hardware-based display).mainScreen是UIScreen类中的类方法, 返回值是一个UIScreen对象,
bounds 是一个CGRect类型的属性: The bounding rectangle of the screen, measured in points. (read-only)
专属UIWindow的主要方法:
- (void)makeKeyAndVisible; Makes the receiver the key window and visible.
CGRect, CGSize, CGPoint:
CGRect, CGSize, CGPoint 都是 CGGeometry 类中定义的结构体:
CGPoint 规定了坐标:
struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;CGSize规定了宽和高:
struct CGSize {
CGFloat width;
CGFloat height;
};
typedef struct CGSize CGSize;CGRect 整合了 CGSize 和 CGPoint:
struct CGRect {
CGPoint origin;
CGSize size;
};
typedef struct CGRect CGRect;例: 获取一个UIView对象的宽和高:
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)]; // CGRectMake函数见下
NSLog(@"%g, %g", view1.frame.size.width, view1.frame.size.height);CGGeometry中常用的函数有:
CG_INLINE CGRect
CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
CGRect rect;
rect.origin.x = x; rect.origin.y = y;
rect.size.width = width; rect.size.height = height;
return rect;
}用来生成一个CGRect对象.
UIKIT_EXTERN NSString *NSStringFromCGRect(CGRect rect);用来将一个CGRect对象转换为字符串.
关于center, frame和bounce的讨论:
这是API文档的说法:
The frame rectangle, which describes the view’s location and size in its superview’s coordinate system.
描述了视图在父视图的坐标系统中的位置和尺寸
The bounds rectangle, which describes the view’s location and size in its own coordinate system.
描述了视图在自己坐标系统中的位置和尺寸
关于 frame 和 bounds, 以下为 frame 和 bounds 的 get 方法:
-(CGRect)frame{
return CGRectMake(self.frame.origin.x,self.frame.origin.y,self.frame.size.width,self.frame.size.height);
}
-(CGRect)bounds{
return CGRectMake(0,0,self.frame.size.width,self.frame.size.height);
}Demo:
Before:
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
view1.backgroundColor = [UIColor redColor];
[self.window addSubview:view1];
[view1 release];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view2.backgroundColor = [UIColor yellowColor];
[view1 addSubview:view2];
[view2 release];After:
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
view1.backgroundColor = [UIColor redColor];
[self.window addSubview:view1];
[view1 release];
NSLog(@"view1 before: %@", NSStringFromCGRect(view1.frame));
打印结果: view1 before: {{50, 50}, {200, 200}}
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (retain, nonatomic) UIWindow *window;
// 在刚创建好的工程中, 要把属性的strong修饰词改为retain
@end
view2.backgroundColor = [UIColor yellowColor];
[view1 addSubview:view2];
[view2 release];
NSLog(@"view2 before: %@", NSStringFromCGRect(view2.frame));
// 打印结果: view2 before: {{0, 0}, {100, 100}}
view1.bounds = CGRectMake(-50, -50, 200, 200);
NSLog(@"view1 after: %@", NSStringFromCGRect(view1.frame));
// 打印结果: view1 after: {{50, 50}, {200, 200}}
NSLog(@"view2 after: %@", NSStringFromCGRect(view2.frame));
// 打印结果: view2 after: {{0, 0}, {100, 100}}当view1将顶点坐标设定为(-50, -50)后, view2若还想在父视图view1中(0, 0)的位置, 则需要向右, 向下各移动50, 便定位到了view1的(0, 0)坐标位置. 当子视图添加到父视图时, 会根据父视图的bounds指定的原点(0, 0)计算frame, 而非左上角.
关于center属性
center.x = frame.origin.x + frame.size.width/2;
center.y = frame.origin.y + frame.size.height/2;
***转载请注明出处
本文详细解析了iOS应用启动过程中的关键组件,包括AppDelegate、UIWindow、UIView等,并深入探讨了这些组件之间的交互方式及各自的作用。此外,还介绍了如何初始化窗口、设置视图属性以及理解frame、bounds和center等概念。
3657

被折叠的 条评论
为什么被折叠?



