美文网首页
UiView 的用法

UiView 的用法

作者: MR_CZWang | 来源:发表于2016-09-05 21:42 被阅读0次

    //1.初始化视图  确定矩形的坐标(x,y) 宽高

    self.myView=[[UIView alloc] initWithFrame:CGRectMake(100, 50, 200, 400)];

    //2.设置背景色

    self.myView.backgroundColor=[UIColor blackColor];

    //3.添加子视图到view(父视图)上

    [self.view addSubview:self.myView];

    //添加父视图(view)背景颜色

    self.view.backgroundColor=[UIColor purpleColor];

    //4.判断设备屏幕大小

    CGRect rectView=self.view.frame;

    NSLog(@"%@",NSStringFromCGRect(rectView));

    //5.fram相对于父视图的坐标位置 能知道坐标和大小

    NSLog(@"myView.fram : %@",NSStringFromCGRect(self.myView.frame));

    //6.bounds 只是显示当前视图的大小 不知道坐标

    NSLog(@"myView.bounds: %@",NSStringFromCGRect(self.myView.bounds));

    //7.center 控件相对于父视图的中心坐标

    NSLog(@"%@",NSStringFromCGPoint(self.myView.center));

    //8.设置视图中心点坐标

    self.myView.center=CGPointMake(300, 350);

    //9.改变视图边界

    self.myView.bounds=CGRectMake(0, 0, 50, 50);

    //10.水平方向平移200个点

    self.myView.transform=CGAffineTransformMakeTranslation(200, 0);

    //11.垂直方向平移200个点

    self.myView.transform=CGAffineTransformMakeTranslation(0, 200);

    //12.斜方向平移

    self.myView.transform=CGAffineTransformMakeTranslation(10, 200);

    //13.设置隐藏视图

    self.myView.hidden=YES;

    //14.如果子视图超出父视图范围是否裁剪子视图  默认是NO

    self.myView.clipsToBounds=YES;

    //15.是否可以和用户进行交互,默认为YES

    self.myView.userInteractionEnabled=YES;

    //初始化视图aView ,bView ,cView

    self.aView=[[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];

    self.aView.backgroundColor=[UIColor redColor];

    self.bView=[[UIView alloc] initWithFrame:CGRectMake(150, 150, 200, 200)];

    self.bView.backgroundColor=[UIColor purpleColor];

    self.cView=[[UIView alloc] initWithFrame:CGRectMake(200, 200, 200, 200)];

    self.cView.backgroundColor=[UIColor blackColor];

    //1.添加子视图

    [self.view addSubview:self.aView];

    [self.view addSubview:self.bView];

    [self.view addSubview:self.cView];

    //插入指定视图 在xxx视图的下面

    [self.view insertSubview:self.bView aboveSubview:self.aView];

    //在xxx视图上面

    [self.view insertSubview:self.bView belowSubview:self.aView];

    //在指定索引位置插入视图

    [self.view insertSubview:self.bView atIndex:0];

    //交换子视图的索引位置

    [self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:3];

    //将子视图放到最后

    [self.view sendSubviewToBack:self.cView];

    //将子视图放到最前

    [self.view bringSubviewToFront:self.aView];

    UIview 的动画

    //这个动画执行的时间为1.5S

    //这个动画会在1.0S后再执行 (延迟时间为1.0)

    //UIViewAnimationOptionCurveLinear 表示动画执行的方式(匀速执行动画)

    [UIView animateWithDuration:1.5 delay:1.0 options:UIViewAnimationOptionCurveLinear animations:^{

    //动画的代码 例如frame  alpha

    } completion:^(BOOL finished) {

    if(finished){

    //当某个控件透明度变味0 在吧此控件从UIview中删除

    //  [控件 removefromesupeview]

    }

    }];

    Transform 属性:

    UIView有个transform的属性,通过设置该属性,我们可以实现调整该view在其superView中的大小和位置,具体来说,Transform(变化矩阵)是一种3×3的矩阵,通过这个矩阵我们可以对一个坐标系统进行缩放,平移,旋转以及这两者的任意组着操作。而且矩阵的操作不具备交换律,即矩阵的操作的顺序不同会导致不同的结果。

    常用的三种实现选中的方式:

    view.transform=CGAffineTransformScale(view.transform, 0.5, 0.5); // 实现的是放大和缩小 view.transform=CGAffineTransformRotate(view.transform, 0.2); //实现的是旋转 view.transform=CGAffineTransformTranslate(view.transform, 20, 20); //实现的是平移

    由此可以发现屏幕旋转其实就是通过view的矩阵变化实现,当设备监测到旋转的时候,会通知当前程序,当前程序再通知程序中的window,window会通知它的rootViewController的,rootViewController对其view的transform进行设置,最终完成旋转。

    相关文章

      网友评论

          本文标题:UiView 的用法

          本文链接:https://www.haomeiwen.com/subject/jaacettx.html