//
// AppDelegate.h
// UI01_UIView
//
// Created by t on 15/8/31.
// Copyright (c) 2015年 . All rights reserved.
//
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
//
// AppDelegate.m
// UI01_UIView
//
// Created by t on 15/8/31.
// Copyright (c) 2015年 . All rights reserved.
//
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
//重写dealloc
- (void)dealloc{
[_window release];
[super dealloc];
}
//该方法是加载成功之后执行,方法内部,建立了一个window,以及相关的子视图.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/*
一. UIWindow
UIWindow 是UIView的子类,作用是加载图片,以及传递触摸事件给内部的视图.一般一个App 只有一个window.
我们可以把window认为是画板,其内部的view认为是画布,我们一般不会直接操作window,而是操作其内部的view.
*/
//创建一个window,等屏大小
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
//设置window的背景颜色
self.window.backgroundColor = [UIColor whiteColor];
//把seif.window设置为主窗口且可见.
[self.window makeKeyAndVisible];
//window 是一个视图容器,我们今后在window上添加视图,才可以显示出来,凡是添加到window上面的视图,视图的属性window不为空.
//二. UIView
//1.创建view,并且初始化
UIView *redView = [[UIView alloc] initWithFrame:(CGRectMake(50, 10, 100, 20))];
UIView *blueView = [[UIView alloc] initWithFrame:(CGRectMake(50, 30, 100, 20)) ];
UIView *blackView = [[UIView alloc] initWithFrame:(CGRectMake(50,50 , 100, 20))];
UIView *purpleView = [[UIView alloc] initWithFrame:(CGRectMake(50, 70, 100, 20))];
UIView *yellowView = [[UIView alloc] initWithFrame:(CGRectMake(50, 90, 100, 20))];
UIView *grayView = [[UIView alloc] initWithFrame:(CGRectMake(50, 110, 100, 20))];
UIView *greenView = [[UIView alloc]initWithFrame:(CGRectMake(50, 130, 100, 20))];
// 第二种方式,init和frame赋值分开来写.
// UIView *redView1 = [[UIView alloc]init];
// redView1.frame = CGRectMake(150, 150, 150, 150);
/*
frame
frame 本身是一个结构体类型,CGRect(CGPoint(x, y), CGSize(width, height))
CGRect 结构体的成员是两个结构体
CGPoint(x, y)控制view的坐标
CGSize(Width, height)控制view的大小
创建CGRect 类型变量,通过CGRectMake()函数
注意:frame是子视图在父视图坐标系中的位置.frame.origin 是子视图的左上角距离父视图原点的距离
在ios中,一个视图的坐标原点,默认为左上角.
*/
UIView *orangeView = [[UIView alloc] initWithFrame:(CGRectMake(50, 150, 100, 20))];
orangeView.backgroundColor = [UIColor orangeColor];
[self.window addSubview:orangeView];
[orangeView release];
CGRect rec = orangeView.frame; //封装,方便以后用.
rec.origin.y = 200;
orangeView.frame = rec;
//// blackView.frame.origin.x = 50; //错误的,产生歧义!
CGRect rec1 = blackView.frame;
rec1.origin.x = 200;
blackView.frame = rec1; //赋值
//2.设置属性
redView.backgroundColor = [UIColor redColor];//给view的backgroundColor 背景颜色设置为红色
blueView.backgroundColor = [UIColor blueColor];
blackView.backgroundColor = [UIColor blackColor];
purpleView.backgroundColor = [UIColor purpleColor];
yellowView.backgroundColor = [UIColor yellowColor];
grayView.backgroundColor = [UIColor grayColor];
greenView.backgroundColor = [UIColor greenColor];
//3.添加视图到window上
[self.window addSubview:redView];
[self.window addSubview:blackView];
[self.window addSubview:blueView];
[self.window addSubview:purpleView];
[self.window addSubview:yellowView];
[self.window addSubview:grayView];
[self.window addSubview:greenView];
//4.释放
[redView release];
[blueView release];
[blackView release];
[purpleView release];
[yellowView release];
[grayView release];
[greenView release];
/*
view 的center 属性
center 为视图的中心点,是基于frame的origin 和 size 决定的
center.x = frame.origin.x + size.width / 2
center.y = frame.origin.y + size.height / 2
*/
//**********************************************************
/*
bounds
bounds 是view的一个重要的属性,代表的是实际显示区域.
bounds 参考的是view 自己的坐标系.
bounds.origin 为 视图的左上角 距离 本身坐标系的位置. (视图本身坐标系原点默认为左上角)因此bounds.origin默认为(0, 0)
*/
UIView *view1 = [[UIView alloc] initWithFrame:(CGRectMake(100, 100, 200, 200))];
view1.backgroundColor = [UIColor magentaColor];
[self.window addSubview:view1];
//给view1 添加一个子视图
UIView *subView = [[UIView alloc] initWithFrame:(CGRectMake(100, 100, 100, 100))];
subView.backgroundColor = [UIColor blueColor];
[view1 addSubview:subView];
//取到view1.bounds
CGRect rect = view1.bounds;
//改变bounds的origin
rect.origin.x = 100;//view1视图左上角,距离原点为100
view1.bounds = rect;
//改变bounds的size
CGRect rec2 = view1.bounds;
rec2.size = CGSizeMake(300, 300);
view1.bounds = rec2;
NSLog(@"%f %f", view1.bounds.origin.x, view1.bounds.origin.y);
[view1 release];
// frame bounds 总结
//frame 是父视图左上角 在父视图坐标系中的位置. 参考系为父视图坐标系.改变frame的origin,改变的是子视图在父视图中的位置
//bounds 是子视图左上角 在本地坐标系中的位置.参考系为本地坐标系.改变 bounds的origin.改变的是本视图的坐标系,视图的原本位置不会发生变化,只是相对于原点坐标的数值发生了改变.
//改变bounds 不会改变自身的位置,会改变子视图的位置.
//bounds 是视图的实际显示区域.如果bounds的size和frame 的size 不同,会基于,view的中心点发生变化.
//********************************************************
UIView *red1View = [[UIView alloc] initWithFrame:(CGRectMake(100, 100, 100, 100))];
redView.backgroundColor = [UIColor redColor];
[self.window addSubview:red1View];
[red1View release];
UIView *orange1View = [[UIView alloc] initWithFrame:(CGRectMake(150, 150, 100, 100))];
orange1View.backgroundColor = [UIColor orangeColor];
[self.window insertSubview:orange1View atIndex:1];
[orange1View release];
UIView *yellow1View = [[UIView alloc] initWithFrame:(CGRectMake(200, 200, 100, 100))];
yellow1View.backgroundColor = [UIColor yellowColor];
[self.window insertSubview:yellow1View atIndex:2];
[yellow1View release];
UIView *blue1View = [[UIView alloc] initWithFrame:(CGRectMake(250, 250, 100, 100))];
blue1View.backgroundColor = [UIColor blueColor];
[self.window insertSubview:blue1View aboveSubview:yellow1View];
[blue1View release];
////insertSubView:bView belowSubView:aView 在aView 视图的下面添加视图bView . subView数组a对象index的前面.
UIView *black1View = [[UIView alloc] initWithFrame:(CGRectMake(225, 225, 100, 100))];
black1View.backgroundColor = [UIColor blackColor];
[self.window insertSubview:black1View belowSubview:blue1View];
[black1View release];
UIView *green1View = [[UIView alloc] initWithFrame:(CGRectMake(50, 50, 100, 100))];
green1View.backgroundColor = [UIColor greenColor];
[self.window addSubview:green1View];
[green1View release];
//管理视图层次的方法,都只是针对于该子视图的父视图来说的,一个子视图,只能有一个父视图.
for (UIView *view in self.window.subviews) {
NSLog(@"%lu %p", [self.window.subviews count], self.window.subviews);
[view removeFromSuperview];
}
//****************************************
//练习要求:新建一个工程,创建5个视图(aView、bView、cView、dView、eView)
UIView *aView = [[UIView alloc] initWithFrame:(CGRectMake(100, 100, 100, 100))];
aView.backgroundColor = [UIColor redColor];
[self.window addSubview:aView];
[aView release];
UIView *bView = [[UIView alloc] initWithFrame:(CGRectMake(125, 125, 100, 100))];
bView.backgroundColor = [UIColor blueColor];
[self.window insertSubview:bView atIndex:1];
[bView release];
UIView *cView = [[UIView alloc] initWithFrame:(CGRectMake(150, 150, 100, 100))];
cView.backgroundColor = [UIColor blackColor];
[self.window insertSubview:cView atIndex:2];
[cView release];
UIView *dView = [[UIView alloc] initWithFrame:(CGRectMake(175, 175, 100, 100))];
dView.backgroundColor = [UIColor purpleColor];
[self.window insertSubview:dView atIndex:3];
[dView release];
UIView *eView = [[UIView alloc] initWithFrame:(CGRectMake(200, 200, 100, 100))];
eView.backgroundColor = [UIColor orangeColor];
[self.window insertSubview:eView atIndex:4];
[eView release];
[self.window bringSubviewToFront:aView];
[self.window sendSubviewToBack:cView];
[self.window exchangeSubviewAtIndex:3 withSubviewAtIndex:4];
[cView removeFromSuperview];
//***********************************
//// //view的常用属性
UIView *tagView = [[UIView alloc] initWithFrame:(CGRectMake(100, 100, 200, 200))];
tagView.backgroundColor = [UIColor redColor];
tagView.tag = 10;
[self.window addSubview:tagView];
[tagView release];
UIView *view = [self.window viewWithTag:10];
view.backgroundColor = [UIColor yellowColor];
tagView.hidden = NO;
tagView.alpha = 0.5;
UIView *superView = [tagView superview];
superView.backgroundColor = [UIColor yellowColor];
for (UIView *view in self.window.subviews) {
if (view.tag == 10) {
UIView *tagView = [self.window viewWithTag:10];
tagView.backgroundColor = [UIColor yellowColor];
}
}
UIView *black2View = [[UIView alloc] initWithFrame:(CGRectMake(100, 100, 100, 100))];
black2View.backgroundColor = [UIColor blackColor];
[tagView addSubview:blackView];
[black2View release];
UIView *orange2View = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, 100, 100))];
orange2View.backgroundColor = [UIColor orangeColor];
[tagView addSubview:orange2View];
[orange2View release];
//给视图添加标记
black2View.tag = 11;
orange2View.tag = 12;
//获取本视图的所有子视图
NSArray *subViews = [tagView subviews];
for (UIView *view in subViews) {
if (view.tag == 11) {
// view = [tagView viewWithTag:11];
view.backgroundColor = [UIColor blueColor];
}else if (view.tag == 12){
view.backgroundColor = [UIColor cyanColor];
}
}
return YES;
}
@end
iOS编程-------UIView
最新推荐文章于 2025-06-29 09:00:00 发布
本文详细介绍了使用iOS开发中如何创建并管理多个UIView,包括使用窗口和视图容器进行布局,设置视图属性如背景色、坐标、大小等,并通过实例演示了如何在主窗口上添加和排列不同颜色的视图。此外,文章还讲解了如何通过`addSubview`方法添加视图,如何使用`removeFromSuperview`方法移除视图,以及如何使用`insertSubview`方法插入视图到指定位置。最后,文章提供了创建5个视图的实践案例,帮助开发者更好地理解视图层次管理和标记属性的应用。
354

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



