添加一个视图 UIView
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
//手动内存管理,
- (void)dealloc
{
// [_window release];
self.window = nil;
//调用此方法的时候,自动对子视图进行release
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//
self.window.rootViewController = [[UIViewController alloc] init];
//添加一个视图
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
//设置视图的背景色
view.backgroundColor = [UIColor blackColor];
//添加到window上
[self.window addSubview:view];
[view release];
//创建3个相同大小的视图,从上往下布局,分别红绿蓝
// NSArray *colors = @[[UIColor redColor], [UIColor greenColor], [UIColor blueColor]];
//RGB颜色
/* 0xrrggbb 0xff ff ff 白色 */
// 40 178 165
// 0~1.0 alpha 表示不透明度 0~1.0
//arc4random_uniform(256) 从0~255中随机获取一个数
// UIColor *myColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1];
//forin
for (int i = 0; i < 3; i++)
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 100 + 100*i, 200, 100)];
// view.backgroundColor = colors[i];
//随机色
UIColor *myColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1];
view.backgroundColor = myColor;
//tag值一定是正整数,并且不能等于0
view.tag = 100+i;
[self.window addSubview:view];
[view release];
}
//获取window的子视图 NSArray
NSLog(@"%@", self.window.subviews);
//设置隐藏 YES 隐藏 NO 不隐藏
view.hidden = NO;
//通过tag值获取对应的视图
UIView *tempView = [self.window viewWithTag:100];
tempView.backgroundColor = [UIColor redColor];
// tempView.frame = CGRectMake(self.window.frame.size.width/2 - tempView.frame.size.width/2, self.window.frame.size.height/2 - tempView.frame.size.height/2, 200, 100);
//设置中心点 CGPoint
//center参考系是相对父视图
tempView.center = CGPointMake(self.window.frame.size.width/2, self.window.frame.size.height/2);
//后面添加的视图,在上层
//获取当前视图的父视图
NSLog(@"%@", tempView.superview);
UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake(80, 450, 150, 150)];
yellowView.backgroundColor = [UIColor yellowColor];
[self.window addSubview:yellowView];
[yellowView release];
//设置阴影效果
// CALayer 主要做图层的渲染
//设置阴影的不透明度
yellowView.layer.shadowOpacity = 1;
//设置阴影的方向和长度
yellowView.layer.shadowOffset = CGSizeMake(-10, -20);
//设置阴影颜色 CGColor core graphy
// yellowView.layer.shadowColor = [UIColor greenColor].CGColor;
//设置圆角半径 ,设置为宽度的一半,变成圆形(视图正方形)
yellowView.layer.cornerRadius = 75;
//设置边框, 随着圆角变化
yellowView.layer.borderWidth = 3;
//边框的颜色
// yellowView.layer.borderColor =
UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 75, 75)];
greenView.backgroundColor = [UIColor greenColor];
[yellowView addSubview:greenView];
[greenView release];
//去掉超出父视图的部分 默认NO
yellowView.clipsToBounds = YES;
//旋转视图 仿射变换 弧度
// 角度 和弧度的换算 a/180 = l/π
// l = a * π / 180
//正值 顺时针旋转 负 逆时针旋转
//参考系 原来的transform
yellowView.transform = CGAffineTransformMakeRotation(45 * M_PI / 180);
//原来基础上累加
// yellowView.transform = CGAffineTransformRotate(yellowView.transform, -45 * M_PI / 180);
//设置视图的不透明度
yellowView.alpha = 0.5;
//对父视图的操作,子视图会跟着变化
//放大或者缩小
//第1个参数,x轴缩放量
// yellowView.transform = CGAffineTransformMakeScale(2, 2);
//累加的伸缩
// yellowView.transform = CGAffineTransformScale(yellowView.transform, 0.5, 0.5);
//添加动画效果
#if 0
//开始动画
[UIView beginAnimations:nil context:nil];
//设置动画持续时间
[UIView setAnimationDuration:5];
//在这里填写动画完成后的效果(最后一帧)
//平移
// yellowView.transform = CGAffineTransformTranslate(yellowView.transform, 100, 0);
yellowView.transform = CGAffineTransformMakeTranslation(100, 0);
//提交动画
[UIView commitAnimations];
#endif
//等于默认的
yellowView.transform = CGAffineTransformIdentity;
//使用Block的方式来实现动画
[UIView animateWithDuration:5 animations:^{
//
CGRect rect = yellowView.frame;
rect.origin.y -= 200;
yellowView.frame = rect;
} completion:^(BOOL finished) {
//前面的动画结束时调用
if (finished)
{
//表示前面的动画已经执行完毕,可以添加其它动画
[UIView animateWithDuration:2 animations:^{
yellowView.alpha = 0;
}];
}
}];
// NSTimer
return YES;
}