UIView 用法讲解
//
// ZKAppDelegate.m
// 01-UIViewDemo
//
#import "ZKAppDelegate.h"
#import "ZKBlock.h"
@interface ZKAppDelegate()
@property(nonatomic, strong)NSMutableArray *blocks;
@end
@implementation ZKAppDelegate
//懒加载
-(NSMutableArray *)blocks{
if (_blocks == nil) {
_blocks = [[NSMutableArray alloc]init];
}
return _blocks;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//UIView:简单的说就是屏幕上的一块矩形区域 用过的window,label,button都是继承自UIView。
//1创建
UIView *view = [[UIView alloc]init];
//2设置位置
view.frame = CGRectMake(10, 40, 100, 200);
//3显示
[self.window addSubview:view];
//4.背景色
view.backgroundColor = [UIColor blueColor];
//5.设置中心 当中心改变的时候,视图的位置会随着中心的改变而改变。大小不变
//view.center = CGPointMake(200, 200);
//6.设置视图位置的方法:frame,bounds
//frame:它的对应的坐标系是世界坐标系(参考物是父视图),即以window的原点为原点作参考。(以父视图的原点为原点)
//bounds:它的对应的坐标系是本地坐标系(参考物是自己),以自己的原点为原点作参考
//注意:当只给view设置bounds的时候,view会以屏幕的原点为中心点
//当你在先设置了view的frame之后,再设置bounds,view会以frame的中心点为中心点,向四周伸缩
//bounds的(x,y)默认是0,进行设置也没有意义。不会影响view的位置
//当要获得一个view的大小的时候,使用bounds
//当设置view的位置的时候,用frame
view.bounds = CGRectMake(30, 300, 20, 40);
NSLog(@"%f,%f",view.center.x,view.center.y);
//7.设置可交互性 有些子视图默认可交互,有些不能,要手动设置
view.userInteractionEnabled = YES;
//隐藏方式:透明度为0,hidden属性为YES
//8.透明度
view.alpha = 0.5;
//9.隐藏
view.hidden = NO;
//NSTimer 可以控制一个事件发生的时间及次数
//(NSTimeInterval):间隔的时间
//id:执行方法的对象
//SEL:执行的方法
//userInfo:nil
//BOOL:判断是否重复执行这个方法
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(createBlock:) userInfo:nil repeats:YES];
// //使NSTimer对象失效
// [timer invalidate];
//
// //停止
// [timer setFireDate:[NSDate distantFuture]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
-(void)createBlock:(NSTimer *)timer{
//随机函数,可以获得随机的数
//例如:获得100以内的随机数 arc4random()%100
if ([self.blocks count]<20) {
ZKBlock *view1 = [[ZKBlock alloc]initWithFrame:CGRectMake(arc4random()%320, arc4random()%480, arc4random()%100, arc4random()%100)];
view1.backgroundColor = [UIColor colorWithRed:(double)(arc4random()%255)/255. green:(double)(arc4random()%255)/255. blue:(double)(arc4random()%255)/255. alpha:(double)(arc4random()%10)/10.];
view1.xSpeed += arc4random()%10;
view1.ySpeed += arc4random()%10;
[self.window addSubview:view1];
[self.blocks addObject:view1];
}
for (ZKBlock *view in self.blocks) {
//语法错误
//view.frame.origin.x = 34;
CGRect frame = view.frame;
//设置view的速度
frame.origin.x += view.xSpeed;
frame.origin.y += view.ySpeed;
//当view到达边界的时候返回
if (frame.origin.x <= 0 || frame.origin.x >= 320 - frame.size.width) {
view.xSpeed = -view.xSpeed;
}
if (frame.origin.y <= 0 || frame.origin.y >= 480 - frame.size.height) {
view.ySpeed = -view.ySpeed;
}
view.frame = frame;
}
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
//
// ZKAppDelegate.m
// 04-UIViewTwo
//
#import "ZKAppDelegate.h"
@implementation ZKAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//UIView之间的嵌套 作用:自定义控件
//例子:一个按钮里面左边是一张图片,右边是两个label,分别显示图片的名称和简介
UIImage *image = [UIImage imageNamed:@"defaultHead"];
//常见button
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(10, 30, 300, image.size.height +10);
//设置button的背景
//拉伸图片
// width - leftCapWidth - right cap = 1
//意思:取图片上面的从左开始leftCapWidth,topcapHeight处的像素,进行拉伸,铺满整个的view
UIImage *backImage = [[UIImage imageNamed:@"table_cell_bg"] stretchableImageWithLeftCapWidth:5 topCapHeight:5];
[button setBackgroundImage:backImage forState:UIControlStateNormal];
[self.window addSubview:button];
//添加图片
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
imageView.frame = CGRectMake(5, 5, image.size.width, image.size.height);
[button addSubview:imageView];
//添加label
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame) + 5, 50, 100, 40)];
label1.text = @"长城";
[button addSubview:label1];
//第二个label
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame) + 5, 100, 100, 40)];
label2.text = @"长城真壮观";
[button addSubview:label2];
//UIView的层级关系
//最后添加的子视图在最上面
UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(10, 240, 100, 100)];
view1.backgroundColor = [UIColor redColor];
[self.window addSubview:view1];
UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(80, 200, 80, 110)];
view2.backgroundColor = [UIColor greenColor];
[self.window addSubview:view2];
UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(90, 290, 60, 150)];
view3.backgroundColor = [UIColor blueColor];
[self.window addSubview:view3];
//获取所有的子视图
NSArray *array = [self.window subviews];
NSLog(@"%@",array);
//获取父视图
UIView *suView = [view1 superview];
NSLog(@"%f",suView.bounds.size.width);
//将视图放在最上面
[self.window bringSubviewToFront:view1];
//将视图放在最下面
[self.window sendSubviewToBack:view1];
//交换两个子视图
[self.window exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
//删除子视图
//[view1 removeFromSuperview];
//新增视图
UIView *view4 = [[UIView alloc]initWithFrame:CGRectMake(190, 250, 160, 150)];
view4.backgroundColor = [UIColor blueColor];
[self.window addSubview:view4];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end