UIWindow:
一、打印屏幕地址和大小:
NSLog
(
@"self.window is %@"
,
self
.
window
);
NSLog(@"[UIApplication sharedApplication].keyWindow is %@", [UIApplication sharedApplication].keyWindow);
//UIScreen
:
屏幕,
是一个单例,
只有一个屏幕
UIScreen *screen = [ UIScreen mainScreen ];
CGRect bounds = screen. bounds ;
CGFloat width = bounds. size . width ;
CGFloat height = bounds. size . height ;
UIScreen *screen = [ UIScreen mainScreen ];
CGRect bounds = screen. bounds ;
CGFloat width = bounds. size . width ;
CGFloat height = bounds. size . height ;
NSLog(@"width is %.2f, height is %.2f", width, height);
二、新建的另一个window,要想使其显示出来,要把它置为Delegate的一个属性:
{
UIWindow *window2;
}
.m文件中:
window2
= [[
UIWindow
alloc
]
initWithFrame
:
CGRectMake
(
0
,
100
,
100
,
100
)];
//hidden 的作用:隐藏 。 window 的这一属性默认为 yes ,比较特殊
window2 . windowLevel = UIWindowLevelAlert ;
window2 . backgroundColor = [ UIColor redColor ];
UIViewController *vc2 = [[UIViewController alloc] init];
window2.rootViewController = vc2;
//hidden 的作用:隐藏 。 window 的这一属性默认为 yes ,比较特殊
window2 . windowLevel = UIWindowLevelAlert ;
window2 . backgroundColor = [ UIColor redColor ];
UIViewController *vc2 = [[UIViewController alloc] init];
window2.rootViewController = vc2;
window2.hidden = NO;
UIView:
三、Frame
和
Bounds ****************************
UIView
*view1 = [[
UIView
alloc
]
initWithFrame
:
CGRectMake
(
100
,
100
,
200
,
200
)];
// 扩展:修改 View 的 bouns ,一般都不去修改它
view1. bounds = CGRectMake ( 50 , 50 , 200 , 200 );
NSLog ( @"view1 frame is %@" , NSStringFromCGRect (view1. frame ));
NSLog ( @"view1 bounds is %@" , NSStringFromCGRect (view1. bounds ));
NSLog ( @"view1 center is %@" , NSStringFromCGPoint (view1. center ));
view1. backgroundColor = [ UIColor redColor ];
[ self . window addSubview :view1];
UIView *view2 = [[ UIView alloc ] initWithFrame : CGRectMake ( 100 , 100 , 100 , 100 )];
UIView *view2 = [[UIView alloc]initWithFrame:view1.frame];
UIView *view2 = [[UIView alloc]initWithFrame:view1.bounds];
view2. backgroundColor = [ UIColor greenColor ];
// 修改透明度
// view2.alpha = 0.3;
[view1 addSubview :view2];
// 修改 View1 的 frame
// 方法 1 :
// view1.frame = CGRectMake(100, 150, 200, 200);
// 方法 2:
CGRect frame = view1. frame ;
frame. origin . y += 100 ;
// 扩展:修改 View 的 bouns ,一般都不去修改它
view1. bounds = CGRectMake ( 50 , 50 , 200 , 200 );
NSLog ( @"view1 frame is %@" , NSStringFromCGRect (view1. frame ));
NSLog ( @"view1 bounds is %@" , NSStringFromCGRect (view1. bounds ));
NSLog ( @"view1 center is %@" , NSStringFromCGPoint (view1. center ));
view1. backgroundColor = [ UIColor redColor ];
[ self . window addSubview :view1];
UIView *view2 = [[ UIView alloc ] initWithFrame : CGRectMake ( 100 , 100 , 100 , 100 )];
UIView *view2 = [[UIView alloc]initWithFrame:view1.frame];
UIView *view2 = [[UIView alloc]initWithFrame:view1.bounds];
view2. backgroundColor = [ UIColor greenColor ];
// 修改透明度
// view2.alpha = 0.3;
[view1 addSubview :view2];
// 修改 View1 的 frame
// 方法 1 :
// view1.frame = CGRectMake(100, 150, 200, 200);
// 方法 2:
CGRect frame = view1. frame ;
frame. origin . y += 100 ;
view1.frame = frame;
四、
视图的层次结构
***************************
//
创建父视图
superView = [[ UIView alloc ] initWithFrame : CGRectMake ( 20 , 120 , 300 , 200 )];
superView . backgroundColor = [ UIColor cyanColor ];
[ self . window addSubview : superView ];
// 创建子视图
UIView *view1 = [[ UIView alloc ] initWithFrame : CGRectMake ( 20 , 20 , 100 , 100 )];
// 设置 tag 值
//tag 值是 View 在父视图上的唯一标识,可自己设定数值
view1. tag = 2015 ;
view1. backgroundColor = [ UIColor greenColor ];
[ superView addSubview :view1];
UIView *view2 = [[ UIView alloc ] initWithFrame : CGRectMake ( 90 , 20 , 100 , 100 )];
view2. tag = 2016 ;
view2. backgroundColor = [ UIColor redColor ];
[ superView addSubview :view2];
// 通过数组的结构管理子视图
NSArray *subViews = superView . subviews ;
for ( UIView *view in subViews) {
NSLog ( @"view is %@" , view);
superView = [[ UIView alloc ] initWithFrame : CGRectMake ( 20 , 120 , 300 , 200 )];
superView . backgroundColor = [ UIColor cyanColor ];
[ self . window addSubview : superView ];
// 创建子视图
UIView *view1 = [[ UIView alloc ] initWithFrame : CGRectMake ( 20 , 20 , 100 , 100 )];
// 设置 tag 值
//tag 值是 View 在父视图上的唯一标识,可自己设定数值
view1. tag = 2015 ;
view1. backgroundColor = [ UIColor greenColor ];
[ superView addSubview :view1];
UIView *view2 = [[ UIView alloc ] initWithFrame : CGRectMake ( 90 , 20 , 100 , 100 )];
view2. tag = 2016 ;
view2. backgroundColor = [ UIColor redColor ];
[ superView addSubview :view2];
// 通过数组的结构管理子视图
NSArray *subViews = superView . subviews ;
for ( UIView *view in subViews) {
NSLog ( @"view is %@" , view);
}
五、视图的层级显示:
注:视图没有级别属性
//
从父视图中通过
tag
取到
view1
UIView *view1 = [ superView viewWithTag : 2015 ];
UIView *view2 = [ superView viewWithTag : 2016 ];
// 将 view2 插入到 view1 的下层显示
//insertSubview: 如果插入的视图已经存在视图数组中,则只是交换视图的下标位置,如果不存在视图数组中,则把视图添加到数组中来,并且持有这块空间
[ superView insertSubview :view2 belowSubview :view1];
// 将 view1 插入到 view2 的上层显示
[ superView insertSubview :view1 aboveSubview :view2];
UIView *view = superView . subviews [ 0 ];
// 将 view 插入到子视图的指定位置
[ superView insertSubview :view atIndex : 1 ];
// 把视图移动到最顶层
[ superView bringSubviewToFront :view1];
// 把视图移动到最底层
[ superView sendSubviewToBack :view2];
// 把两个索引对应的视图调换位置
[ superView exchangeSubviewAtIndex : 0 withSubviewAtIndex : 1 ];
//把视图从父视图中移除
UIView *view1 = [ superView viewWithTag : 2015 ];
UIView *view2 = [ superView viewWithTag : 2016 ];
// 将 view2 插入到 view1 的下层显示
//insertSubview: 如果插入的视图已经存在视图数组中,则只是交换视图的下标位置,如果不存在视图数组中,则把视图添加到数组中来,并且持有这块空间
[ superView insertSubview :view2 belowSubview :view1];
// 将 view1 插入到 view2 的上层显示
[ superView insertSubview :view1 aboveSubview :view2];
UIView *view = superView . subviews [ 0 ];
// 将 view 插入到子视图的指定位置
[ superView insertSubview :view atIndex : 1 ];
// 把视图移动到最顶层
[ superView bringSubviewToFront :view1];
// 把视图移动到最底层
[ superView sendSubviewToBack :view2];
// 把两个索引对应的视图调换位置
[ superView exchangeSubviewAtIndex : 0 withSubviewAtIndex : 1 ];
//把视图从父视图中移除
[view1 removeFromSuperview];
六、视图的缩放、旋转、平移
//
创建一个
button
UIButton *btn1 = [ UIButton buttonWithType : UIButtonTypeContactAdd ];
btn1. frame = CGRectMake ( 100 , 400 , 50 , 50 );
// 给 btn1 添加一个点击事件
//Target----Action
[btn1 addTarget : self action : @selector (buttonClick:) forControlEvents : UIControlEventTouchUpInside ];
[ self . window addSubview :btn1];
UIButton *btn2 = [ UIButton buttonWithType : UIButtonTypeInfoDark ];
btn2. frame = CGRectMake ( 150 , 400 , 50 , 50 );
// 给 btn1 添加一个点击事件
//Target----Action
[btn2 addTarget : self action : @selector (huanyuan:) forControlEvents : UIControlEventTouchUpInside ];
UIButton *btn1 = [ UIButton buttonWithType : UIButtonTypeContactAdd ];
btn1. frame = CGRectMake ( 100 , 400 , 50 , 50 );
// 给 btn1 添加一个点击事件
//Target----Action
[btn1 addTarget : self action : @selector (buttonClick:) forControlEvents : UIControlEventTouchUpInside ];
[ self . window addSubview :btn1];
UIButton *btn2 = [ UIButton buttonWithType : UIButtonTypeInfoDark ];
btn2. frame = CGRectMake ( 150 , 400 , 50 , 50 );
// 给 btn1 添加一个点击事件
//Target----Action
[btn2 addTarget : self action : @selector (huanyuan:) forControlEvents : UIControlEventTouchUpInside ];
[self.window addSubview:btn2];
#pragma mark-btnAction
- ( void )buttonClick:( UIButton *)button{
UIView *view = [ self . window viewWithTag : 100 ];
CGAffineTransform t = view. transform ;
//缩放
// 在原来基础上进行缩放 ----> 每次都会缩放
view. transform = CGAffineTransformScale (t, 0.5 , 0.5 );
// 在原始的基础上进行缩放 ----> 只会缩放一次
view. transform = CGAffineTransformMakeScale ( 0.5 , 0.5 );
//旋转 ---- 旋转的是弧度
view. transform = CGAffineTransformRotate (t, M_PI / 4 );
view. transform = CGAffineTransformMakeRotation ( M_PI );
//平移
view. transform = CGAffineTransformTranslate (t, 50 , 50 );
view. transform = CGAffineTransformMakeTranslation ( 50 , 50 );
}
- ( void )huanyuan:( UIButton *)button{
UIView *view = [ self . window viewWithTag : 100 ];
// 还原为最初的 transform
view. transform = CGAffineTransformIdentity ;
- ( void )buttonClick:( UIButton *)button{
UIView *view = [ self . window viewWithTag : 100 ];
CGAffineTransform t = view. transform ;
//缩放
// 在原来基础上进行缩放 ----> 每次都会缩放
view. transform = CGAffineTransformScale (t, 0.5 , 0.5 );
// 在原始的基础上进行缩放 ----> 只会缩放一次
view. transform = CGAffineTransformMakeScale ( 0.5 , 0.5 );
//旋转 ---- 旋转的是弧度
view. transform = CGAffineTransformRotate (t, M_PI / 4 );
view. transform = CGAffineTransformMakeRotation ( M_PI );
//平移
view. transform = CGAffineTransformTranslate (t, 50 , 50 );
view. transform = CGAffineTransformMakeTranslation ( 50 , 50 );
}
- ( void )huanyuan:( UIButton *)button{
UIView *view = [ self . window viewWithTag : 100 ];
// 还原为最初的 transform
view. transform = CGAffineTransformIdentity ;
}
七、View动画
动画制作一:
UIView *view = [self.window viewWithTag:100
];
// 开始动画
[ UIView beginAnimations : @"hhhhh" context : NULL ];
// 设置动画的属性
// 设置动画持续的时间
[ UIView setAnimationDuration : 2 ];
// 设置动画的代理
[ UIView setAnimationDelegate : self ];
// 设置动画的加速方式
[ UIView setAnimationCurve : UIViewAnimationCurveEaseInOut ];
// 设置动画的重复次数
[ UIView setAnimationRepeatCount : 2 ];
// 设置动画是否反方向执行
[ UIView setAnimationRepeatAutoreverses : YES ];
// 延迟调用动画
// [UIView setAnimationDelay:2];
// 动画值的修改
view. alpha = 0 ;
// 改变 frame
// CGRect frame = view.frame;
// frame.origin.y = 350;
// view.frame = frame;
// 改变背景颜色
// view.backgroundColor = [UIColor redColor];
// 改变 transform
view. transform = CGAffineTransformRotate (view. transform , M_PI / 4 );
// 提交动画
[ UIView commitAnimations ];
// 开始动画
[ UIView beginAnimations : @"hhhhh" context : NULL ];
// 设置动画的属性
// 设置动画持续的时间
[ UIView setAnimationDuration : 2 ];
// 设置动画的代理
[ UIView setAnimationDelegate : self ];
// 设置动画的加速方式
[ UIView setAnimationCurve : UIViewAnimationCurveEaseInOut ];
// 设置动画的重复次数
[ UIView setAnimationRepeatCount : 2 ];
// 设置动画是否反方向执行
[ UIView setAnimationRepeatAutoreverses : YES ];
// 延迟调用动画
// [UIView setAnimationDelay:2];
// 动画值的修改
view. alpha = 0 ;
// 改变 frame
// CGRect frame = view.frame;
// frame.origin.y = 350;
// view.frame = frame;
// 改变背景颜色
// view.backgroundColor = [UIColor redColor];
// 改变 transform
view. transform = CGAffineTransformRotate (view. transform , M_PI / 4 );
// 提交动画
[ UIView commitAnimations ];
动画制作二:
//block
动画
[ UIView animateWithDuration : 2
animations :^{
view. transform = CGAffineTransformRotate (view. transform , M_PI / 4 );
}];
[ UIView animateWithDuration : 2
animations :^{
view. transform = CGAffineTransformRotate (view. transform , M_PI / 4 );
}
completion :^( BOOL finished) {
[ UIView animateWithDuration : 2
animations :^{
view. transform = CGAffineTransformIdentity ;
}];
}];
[ UIView animateWithDuration : 2
animations :^{
view. transform = CGAffineTransformRotate (view. transform , M_PI / 4 );
}];
[ UIView animateWithDuration : 2
animations :^{
view. transform = CGAffineTransformRotate (view. transform , M_PI / 4 );
}
completion :^( BOOL finished) {
[ UIView animateWithDuration : 2
animations :^{
view. transform = CGAffineTransformIdentity ;
}];
}];