LessonUIView

//

//  AppDelegate.m

//  LessonUIView

//

//  Created by lanouhn on 15/3/16.

//  Copyright (c) 2015年 lanouhn. All rights reserved.

//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (void)dealloc {

[_window release];

[super dealloc];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

//UIWindow:UI中的窗口类, 用于显示界面, 一般一个应用至少要有一个window

//ios中的控件都是矩形, 决定这个空间位置及大小,用结构体CGrect

//CGPoint,结构体, 用来存储x, y轴坐标

CGPoint point = CGPointMake(10, 20);

NSLog(@"%.2lf", point.x);

NSLog(@"%.2lf", point.y);

NSLog(@"%@", NSStringFromCGPoint(point));

//CGSize, 结构体, 用于存储宽度和高度

CGSize size = CGSizeMake(200, 300);

NSLog(@"%.2lf", size.width);

NSLog(@"%.2lf", size.height);

NSLog(@"%@", NSStringFromCGSize(size));

//CGRect, 结构体, 用于存储矩形的位置和大小

CGRect rest = CGRectMake(10, 20, 200, 300);

NSLog(@"%.2f %.2f %.2f %.2f", rest.origin.x, rest.origin.y, rest.size.width, rest.size.height);

NSLog(@"%@", NSStringFromCGRect(rest));

//[UIScreen mainScreen], 获取当前手机的屏幕

//[[UIScreen mainScreen] bounds], 获取当前手机屏幕大小

NSLog(@"%@", NSStringFromCGRect([[UIScreen mainScreen] bounds]));

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

// Override point for customization after application launch.

//设置窗口背景色

self.window.backgroundColor = [UIColor whiteColor];

//设置为主窗口, 并显示

[self.window makeKeyAndVisible];

//UIView:UI中的视图类, 在应用中显示的全部都是UIView或者是UIView的子类

//创建一个UIView

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(30, 40, 50, 100)];

//修改颜色

view.backgroundColor = [UIColor blackColor];

//添加视图(添加到子视图数组中]

[self.window addSubview:view];

//数组会保留一份, 所以可以直接release

[view release];

//父视图

NSLog(@"%@", view.superview);

//一个视图可以有多个子视图

//子视图数组

NSLog(@"%@", view.subviews);

//添加一个视图的步骤

//1.创建视图

//2.设置视图属性(背景颜色)

//3.添加的父视图

//4.释放内存

//获取视图的位置和大小

NSLog(@"%@", NSStringFromCGRect(self.window.frame));

UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(30, 667-40 - 100, 50, 100)] ;

view1.backgroundColor = [UIColor blackColor];

[self.window addSubview:view1];

[view1 release];

UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(375 - 30 -50, 40, 50, 100)] ;

view2.backgroundColor = [UIColor redColor];

[self.window addSubview:view2];

[view2 release];

UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(375 - 30 -50, 667 - 40 -100, 50, 100)] ;

view3.backgroundColor = [UIColor redColor];

[self.window addSubview:view3];

[view3 release];

UIView *view5 = [[UIView alloc] initWithFrame:CGRectMake(100, 150, 375 - 2 * 100, 667 - 2 * 150)];

view5.backgroundColor = [UIColor greenColor];

[self.window addSubview:view5];

[view5 release];

//从父视图上移除视图

[view5 removeFromSuperview];

NSLog(@"%@", self.window.subviews);

//遍历子视图

for (UIView *newView in self.window.subviews) {

[newView removeFromSuperview];

}

//红色视图

UIView *view6 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];

view6.backgroundColor = [UIColor redColor];

[self.window addSubview:view6];

[view6 release];

//绿色视图

UIView *view7 = [[UIView alloc] initWithFrame:CGRectMake(140, 140, 100, 100)];

view7.backgroundColor = [UIColor greenColor];

[self.window addSubview:view7];

[view7 release];

//红色视图提前

[self.window bringSubviewToFront:view6];

//红色视图放后边

//    [self.window sendSubviewToBack:view6];

//蓝色视图

UIView *view8 = [[UIView alloc] initWithFrame:CGRectMake(120, 120, 100, 100)];

view8.backgroundColor = [UIColor blueColor];

//插入红色和绿色中间的三种方法

[self.window insertSubview:view8 atIndex:1];

[self.window insertSubview:view8 aboveSubview:view7];

[self.window insertSubview:view8 belowSubview:view6];

[view8 release];

//UIColor, 颜色类

//UIView不设置背景色, 默认为clearColor

//自定义颜色

UIColor *customColor = [UIColor colorWithRed:100 / 250. green:20 / 255. blue:30 / 255. alpha:1.000];

self.window.backgroundColor = customColor;

//创建一个随机颜色的视图

UIColor *tempColor = [UIColor colorWithRed:(arc4random() % 256) / 255. green:(arc4random() % 256) / 255. blue: (arc4random() / 256) / 255. alpha:1.000];

UIView *view9 = [[UIView alloc] initWithFrame:CGRectMake(100, 300, 100, 100)];

[self.window addSubview:view9];

view9.backgroundColor = tempColor;

[view9 release];

//全部移除子视图

for (UIView *aView in self.window.subviews) {

[aView removeFromSuperview];

}

//

for (int i = 0; i < 31; i++) {

UIView *view10 = [[UIView alloc] initWithFrame:CGRectMake(20 + 10* i, 20 + 10 * i, 50, 50)];

view10.backgroundColor = [UIColor colorWithRed:(arc4random() % 256) / 255. green:(arc4random() % 256) / 255. blue: (arc4random() / 256) / 255. alpha:1.000];

[self.window addSubview:view10];

[view10 release];

}

self.window.backgroundColor = [UIColor whiteColor];

for (UIView *bView in self.window.subviews) {

[bView removeFromSuperview];

}

//UIView的属性值

UIView *centerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];

//设置背景色

centerView.backgroundColor = [UIColor cyanColor];

//设置是否隐藏

centerView.hidden = NO;

//设置不透明度(0 - 1.0)

centerView.alpha = 0.5;

//设置frame

centerView.frame = CGRectMake(0, 100, 300, 200);

//注:一个视图的frame只能够整体修改

CGRect tempRect = centerView.frame;

tempRect.size.height = 300;

centerView.frame = tempRect;

//frame的坐标系是父视图的坐标系

UIView *view11 = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];

view11.backgroundColor = [UIColor redColor];

[centerView addSubview:view11];

[view11 release];

//设置中心点

centerView.center = CGPointMake(self.window.frame.size.width / 2.0, self.window.frame.size.height / 2.0);

centerView.center = self.window.center;

//中心点得坐标系是父视图的坐标系

//设置bounds, 是这个视图在自身坐标系的位置和大小, 是基于自身坐标系

NSLog(@"%@", NSStringFromCGRect(centerView.bounds));

//frame, center, bounds关系

//1. frame修改, 会影响center和bounds

//2. center修改, 会影响frame, bounds不发生变化

//2.bounds修改, 会影响frame和center

[self.window addSubview:centerView];

[centerView release];

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

时间: 2024-10-10 06:57:09

LessonUIView的相关文章