//
// AppDelegate.m
// UI01_UIView
//
// Created by lanou3g on 17/8/3.
// Copyright © 2017年 lanou3g. All rights reserved.
//
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
//程序加载完成之后
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//单例:整个生命周期内只有一个对象
[UIScreen mainScreen];
self.window.backgroundColor = [UIColor whiteColor];
//让window作为主window并可见
[self.window makeKeyAndVisible];
//创建根视图对象
RootViewController *rootViewController = [[RootViewController alloc] init];
//将window的根视图设置为rootViewController
self.window.rootViewController = rootViewController;
NSLog(@"程序加载完成之后");
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.
NSLog(@"失去活跃状态");
}
//程序进入后台
- (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.
NSLog(@"程序进入后台");
}
//将要进入前台
- (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.
NSLog(@"将要进入前台");
}
//成为活跃状态
- (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.
NSLog(@"成为活跃状态");
}
//程序将要结束时
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
NSLog(@"程序将要结束时");
}
@end
//
// RootViewController.m
// UI01_UIView
//
// Created by lanou3g on 17/8/3.
// Copyright © 2017年 lanou3g. All rights reserved.
//
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
//视图加载完成(自带的view)
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIView *redview = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
redview.backgroundColor = [UIColor redColor];
//将redview添加到self.view上
[self.view addSubview:redview];
UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
greenView.backgroundColor = [UIColor greenColor];
[self.view addSubview:greenView];
//中心点
greenView.center = CGPointMake(100, 100);
NSLog(@"%@",NSStringFromCGRect(greenView.frame));
//hidden 控制视图隐藏
greenView.hidden = NO;
//alpha 视图的透明度 (0是完全透明)
greenView.alpha = 0.5f;
//superview获取本视图的父视图
NSLog(@"superView:%@",greenView.superview);
//superview获取本视图的所有子视图
NSLog(@"subViews:%@",self.view.subviews);
//tag:给视图一个标记,用来找到该视图(系统保留1000以内,为了避免冲突,所以我们tag赋值通常在1000以上)
greenView.tag = 1001;
UIView *tagView = [self.view viewWithTag:1001];
NSLog(@"%@",tagView);
UIView *orangeView = [[UIView alloc] initWithFrame:CGRectMake(25, 25, 100, 100)];
orangeView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:orangeView];
//在指定的index处插入子视图
// [self.view insertSubview:orangeView atIndex:1];
//在指定的视图上面添加子视图
[self.view insertSubview:orangeView aboveSubview:redview];
//从父视图中移除
[orangeView removeFromSuperview];
//bunds会影响子视图的位置 frame会影响父视图的位置
}
//收到内存警告
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
网友评论