UIView

来源:互联网 发布:省流量的软件 编辑:程序博客网 时间:2024/06/15 09:19

创建viewController的视图UI

手动创建

    // 创建window    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];    //设置self.window的背景颜色    self.window.backgroundColor = [UIColor whiteColor];    //设置self.window为keywindow并显示    [self.window makeKeyAndVisible];    //创建viewController    UIViewController * viewController = [[UIViewController alloc] init];    //设置window的根控制器为viewController    self.window.rootViewController = viewController;

xib文件创建

    // 创建window    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];    //设置self.window的背景颜色    self.window.backgroundColor = [UIColor whiteColor];    //设置根控制器    UIViewController * viewController = [[UIViewController alloc] init];    self.window.rootViewController = viewController;    //设置self.window为keywindows并显示    [self.window makeKeyAndVisible];    //创建NSBundle对象    NSBundle * bundle = [NSBundle mainBundle];    //直接载入Nib文件,以前xib名为Nib    NSArray * array = [bundle loadNibNamed:@"view" owner:self options:nil];    //只有一个元素    UIView *myView = [array lastObject];    viewController.view = myView;

属性

    //设置背景颜色    view.backgroundColor = [UIColor yellowColor];    //透明度(父视图的透明度影响子视图)    view.alpha = 1;    self.window.alpha = 1;    //隐藏    view.hidden = NO;    //中心位置    view.center = self.window.center;    //标签(NSInteger)tag    view.tag = 100;

方法

多个视图之间位置的移动

    //把视图移动到最上面    [self.window bringSubviewToFront:view];    //把视图移动到最下面    [self.window sendSubviewToBack:view];    //交换视图的位置    [self.window exchangeSubviewAtIndex:0 withSubviewAtIndex:4];

视图变换

在原本视图上变换

    //放大缩小(width,height变换的倍数)    view.transform = CGAffineTransformMakeScale(0.5, 0.5);    view.transform = CGAffineTransformMakeScale(2, 2);    // 旋转    view.transform = CGAffineTransformMakeRotation(M_PI_4);    // 平移    view.transform = CGAffineTransformMakeTranslation(100, 100);

在上一次变换的基础上再次变换

    //旋转    view.transform = CGAffineTransformRotate(view.transform, M_PI_4);    //平移    view.transform = CGAffineTransformTranslate(view.transform, 50, 50);    //缩放(width,height变换的倍数)    view.transform = CGAffineTransformScale(view.transform, 1, 1);    //还原    view.transform = CGAffineTransformIdentity;

动画

常规动画

    //1 第一个参数:动画的名字 第二个参数:上下文    [UIView beginAnimations:@"平移动画" context:nil];    //2:设定动画属性    //持续时间    [UIView setAnimationDuration:2];    //延迟    [UIView setAnimationDelay:1];    //3:要执行的动画    view.transform = CGAffineTransformRotate(_view.transform, M_PI_4);    //4:提交动画    [UIView commitAnimations];    //设置代理,设置完代理以后就可以使用@interface UIView(UIViewAnimation)里的方法    [UIView setAnimationDelegate:self];    //动画开始调用方法default = NULL;    [UIView setAnimationWillStartSelector:@selector(selector)];    //如果UIView没有调用或者方法为空,自动调用    -animationWillStart:(NSString *)animationID context:(void *)context    //动画结束调用方法default = NULL;    [UIView setAnimationDidStopSelector:@selector(selector)];    //如果UIView没有调用或者方法为空,自动调用    -animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context

Block动画

    [UIView animateWithDuration:0.5                     animations:^{                         //调用后执行的操作                         _view.transform = CGAffineTransformRotate(_view.transform, M_PI);                     } completion:^(BOOL finished) {                         //完成后执行的操作                     }];
0 0
原创粉丝点击