您的位置:首页 > 移动开发 > IOS开发

窗口、视图、动画(16.5.20)

2016-05-20 11:08 477 查看
UIWindow

1.特点

一个窗口包含多个视图,APP只有一个UIWindow,UIWindow是APP的的根容器;

通过UIApplication获取当前window,UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;

窗口的大小是手机屏幕的大小;

自定义窗口:

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

[self.window makeKeyAndVisible]//显示窗口

Xcode6.0以后工程自动创建了窗口。

视图属性:

视图的位置和大小是用frame和bound来表示;

frame以其父视图为起点,得出自己在父视图的大小和位置;

bound以自身为起点,指定自身大小和原点位置,坐标永远是(0,0),它是结构体,和frame一样。

center以父视图为起点,得出自己的中点在父视图坐标系中的位置,它也是结构体。有两个成员x、y轴。

frame改变时,bouds和center也改变;

center改变时,frame的origin改变。

  //创建一个视图并把它贴到根视图控制器的view上

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

    aView.backgroundColor = [UIColor
yellowColor];

    [self.view
addSubview:aView];//添加子视图

    [aView release];

    

//    return;

    

   
//创建一个视图并把它贴到根视图控制器的view上

    UIView *aView3 = [[UIView
alloc] initWithFrame:CGRectMake(0, 0, 120, 120)];

    aView3.backgroundColor = [UIColor
redColor];

    [self.view
addSubview:aView3];

    [self.view
insertSubview:aView aboveSubview:aView3];//aView插入aView3之上

    [aView3 release];

    

//    return;

    

   
//创建一个视图并把它贴到根视图控制器的view上

    UIView *aView4 = [[UIView
alloc] initWithFrame:CGRectMake(0, 0, 160, 160)];

    aView4.backgroundColor = [UIColor
greenColor];

    [self.view
addSubview:aView4];

    [self.view
insertSubview:aView4 belowSubview:aView3];//aView4插入aView3之下

    [aView4 release];

    

//    return;

    

   
//建一个视图并把它贴到根视图控制器的view上

    UIView *aView5 = [[UIView
alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];

    aView5.backgroundColor = [UIColor
grayColor];

    [self.view
addSubview:aView5];

    [self.view
sendSubviewToBack:aView5];//把aView5移动到所有子视图下面

    [aView5 release];

    

//    return;

    

    [self.view
bringSubviewToFront:aView3];//把aView3移动到所有子视图上面

    

    [aView5 removeFromSuperview];//视图aView5从父视图self.view中移除

    

UIView *aView = [[UIView
alloc] initWithFrame:CGRectMake(10, 100, 200, 200)];

    [self.view
addSubview:aView];

    [aView release];

    

    aView.backgroundColor = [UIColor
yellowColor];//背景色,
默认值为白色

//    aView.alpha = 0.5;//透明度,默认值为1.0

//    aView.hidden = NO;//是否隐藏,默认值为NO

//    if (aView.superview == self.view)

//    {

//        NSLog(@"视图的父视图是self.view");

//    }

//

//    //视图tag的使用

//    aView.tag = 666; //0~100已被系统占用,为避免与系统的tag冲突,最好600以上

//    UIView *subView = [self.view viewWithTag:666];//通过tag值获取子视图

//    if (subView)

//    {

//        subView.backgroundColor = [UIColor redColor];

//    }

//

    

//    aView.clipsToBounds = YES; //越界裁剪属性,
默认值为NO

    

//    UIView *aView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 100)];

//    aView2.backgroundColor = [UIColor greenColor];

//    [aView addSubview:aView2];

//    [aView2 release];

    

//    aView.userInteractionEnabled = YES; //是否接收用户触摸事件,默认值为YES

    

    NSArray *array =
self.view.subviews;

    NSLog(@"%@", array);

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

    aView.center =
self.view.center;//窗口中心显示

    aView.backgroundColor = [UIColor
redColor];

    [self.view
addSubview:aView];

    [aView release];

    

    

    //
动画形式1

#if 0

    [UIView beginAnimations:@"curl" context:nil];//开启动画

    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//设置动画展现速度

    [UIView setAnimationDuration:1.0];//设置动画持续时间

//    [UIView setAnimationDelay:2];//延缓2秒后执行动画(默认不延缓)

    [UIView setAnimationDelegate:self];//设置动画委托

    [UIView setAnimationDidStopSelector:@selector(animationFinished
102e9
)];//设置动画结束后的回调(与委托delegate绑定使用)

//    CGRect rect = aView.frame;

    aView.frame = CGRectMake(0, 0, 30, 30);

    aView.center = self.view.center;//窗口中心显示

//    aView.frame = CGRectMake(rect.origin.x, 250, rect.size.width, rect.size.height);

//    aView.alpha = 0.0;

    aView.backgroundColor = [UIColor yellowColor];

    [UIView commitAnimations];// 提交动画

    

#endif

    

    

     //动画形式2

#if 0

    [UIView animateWithDuration:2.0 animations:^{

//        CGRect rect = aView.frame;

//        aView.frame = CGRectMake(rect.origin.x, rect.origin.y, 30, 30);

//        aView.frame = CGRectMake(rect.origin.x, 250, rect.size.width, rect.size.height);

//        aView.alpha = 0.0;

//        aView.backgroundColor = [UIColor yellowColor];

    }];

#endif

    

    

    //动画形式3

#if 0

    [UIView animateWithDuration:2.0 animations:^{

//        CGRect rect = aView.frame;

//        aView.frame = CGRectMake(rect.origin.x, rect.origin.y, 30, 30);

//        aView.frame = CGRectMake(rect.origin.x, 250, rect.size.width, rect.size.height);

//        aView.alpha = 0.0;

        aView.backgroundColor = [UIColor yellowColor];

    } completion:^(BOOL finished) {

        NSLog(@"动画结束了");

    }];

#endif

    

    

    

    //动画形式4

#if 0

    [UIView animateWithDuration:2.0 delay:2.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{

//        CGRect rect = aView.frame;

//        aView.frame = CGRectMake(rect.origin.x, rect.origin.y, 30, 30);

//        aView.frame = CGRectMake(rect.origin.x, 250, rect.size.width, rect.size.height);

//        aView.alpha = 0.0;

        aView.backgroundColor = [UIColor yellowColor];

    } completion:^(BOOL finished) {

        NSLog(@"动画结束了");

    }];

#endif

    

    

   
//视图的仿射变换

#if 1

    [UIView
animateWithDuration:2.0 animations:^{

//        aView.transform = CGAffineTransformMakeScale(1.0, 0.5);//对视图进行比例缩放(比如:缩小到原来的0.5倍)

        aView.transform =
CGAffineTransformMakeRotation(M_PI/4);//对视图做变焦旋转,
旋转45度

//        aView.transform = CGAffineTransformMakeTranslation(0, 100);//对视图在原来的位置上做平移

        

    } completion:^(BOOL finished) {

        NSLog(@"~~~~~动画结束了~~~~~");

    }];

#endif

    

    

    

    //组合动画

#if 1

    [UIView
animateWithDuration:2.0 animations:^{

        CGAffineTransform newTransform =
CGAffineTransformMakeScale(1.5, 1.5);

        [aView setTransform:newTransform];

    } completion:^(BOOL finished) {

        NSLog(@"动画1结束了,开始动画2");

        [UIView
animateWithDuration:2.0 animations:^{

            CGAffineTransform newTransform =
CGAffineTransformMakeScale(0.5, 0.5);

            [aView setTransform:newTransform];

        } completion:^(BOOL finished) {

            NSLog(@"动画2结束了,开始动画3");

            [UIView
animateWithDuration:2.0 animations:^{

                aView.backgroundColor = [UIColor
brownColor];

            } completion:^(BOOL finished) {

                NSLog(@"动画3结束了,开始动画4");

                [UIView
animateWithDuration:2.0 animations:^{

                    aView.alpha = 0.0;

                } completion:^(BOOL finished) {

                    NSLog(@"动画4结束了,开始动画5");

                    [UIView
animateWithDuration:2.0 animations:^{

                        aView.alpha = 1.0;

                    } completion:^(BOOL finished) {

                        NSLog(@"动画5结束了,开始动画6");

                        [UIView
animateWithDuration:2.0 animations:^{

                            aView.transform =
CGAffineTransformMakeRotation(M_PI/2);

                        } completion:^(BOOL finished) {

                            NSLog(@"动画6结束了,开始动画7");

                            [UIView
animateWithDuration:2.0 animations:^{

                                aView.transform =
CGAffineTransformMakeRotation(M_PI);

                            } completion:^(BOOL finished) {

                                NSLog(@"动画7结束了,完了");

                            }];

                        }];

                    }];

                }];

            }];

        }];

    }];

#endif

//动画结束后的回调方法

- (void)animationFinished

{

    NSLog(@"动画结束了");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息