UI之UiView和CAlayer基础

来源:互联网 发布:java 八皇后算法 编辑:程序博客网 时间:2024/06/04 19:58

//UIVIew 是所有看见摸得着的控件的基类(直接或者间接的父类),UIView里所有的方法和属性它的子类都可以使用

UIView  像UILabel UIButtonUIImageView等,把他们相似的功能抽离出来,写到一个类里面,这个类作为父类,也就是UIView

UIView 描述的一块矩形区域的视图的样式,通常会把UIView作为某些控件的父试图来使用

//frame bounds center是相关联的,改变其中一个,另外两个也会发生变化(控件的位置会发生变化)

view.transform =CGAffineTransformMakeScale(1.5, -1.5);

    //第一个值是试图横向的变化 第二个值是纵向的变化

    //值为1试图不变形 大于1相当于拉伸 小于1相当于压缩 负数相当于翻转 是0的话试图消失

 view.hidden=NO;//是否隐藏,默认NO

    view.userInteractionEnabled=YES;//与用户交互,默认是NO

    //UILabel UIImageView的userInteractionEnabled这个属性默认是NO,不与用户交互,如果要在label上放button,button是点击不了的

    //UIWindow->UIView

    //如果父试图设置了透明度,子试图也会有透明度

    view.clipsToBounds = YES;//是否裁剪子试图(孙子试图)超出的部分 默认是是NO

    view.autoresizesSubviews = YES; //停靠模式:父试图大小发生变化,子试图也随之变化,默认NO

    subView.autoresizingMask =UIViewAutoresizingFlexibleWidth;//子试图改变大小的模式

whiteView.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin;//whiteView神力放在一个父视图上的中心位置 ,随着父视图的缩小,子视图随之也一起缩小

自动布局

首先禁用自动缩放[yellowView setTranslatesAutoresizingMaskIntoConstraints:NO];

//约束黄色视图的底边和父视图底边对齐

    NSLayoutConstraint* con1=[NSLayoutConstraintconstraintWithItem:yellowView attribute:NSLayoutAttributeBottomrelatedBy:NSLayoutRelationEqualtoItem:self.topViewattribute:NSLayoutAttributeBottommultiplier:1constant:0]; 

    //属性1=multiplier x属性2+constant

    //y=kx+b

    [self.topViewaddConstraint:con1];

    [self.topViewaddConstraint:[NSLayoutConstraintconstraintWithItem:yellowViewattribute:NSLayoutAttributeHeightrelatedBy:NSLayoutRelationEqualtoItem:self.topViewattribute:NSLayoutAttributeHeightmultiplier:0.25constant:0]];

    [self.topViewaddConstraint:[NSLayoutConstraintconstraintWithItem:yellowViewattribute:NSLayoutAttributeRightrelatedBy:NSLayoutRelationEqualtoItem:self.topViewattribute:NSLayoutAttributeRightmultiplier:1constant:0]];

    [self.topViewaddConstraint:[NSLayoutConstraintconstraintWithItem:yellowViewattribute:NSLayoutAttributeWidthrelatedBy:NSLayoutRelationEqualtoItem:self.topViewattribute:NSLayoutAttributeWidthmultiplier:0.25constant:0]];

父子视图的定位,犹如将子视图钉在父视图上面,那么在父子视图上都有一个孔位:子视图的position保存着在父视图的孔位,子视图的anchorPoint保存着自己的孔位,两个孔位永远重合。

          UIView* view1=[[UIViewalloc]initWithFrame:CGRectMake(100,100, 100, 100)];

    CALayer* layer=view1.layer;

    [self.viewaddSubview:view1];

    layer.position=CGPointMake(200,200);

    layer.anchorPoint=CGPointMake(0.5,0.5);

 

 //1.把子试图显示在最前面(最上面)

    [self.windowbringSubviewToFront:greenView];

    //2.把子试图显示在最下面(最后面)

    [self.window sendSubviewToBack:greenView];

    //3.把一个子试图放在另一个子试图的上面(前面)

    [self.window insertSubview:greenViewaboveSubview:redView];

    //4.把一个子试图放在另一个子试图的下面

    [self.window insertSubview:greenViewbelowSubview:redView];

    //5.把子试图显示在Index的位置上(最下层Index值是0,一次递增)

    [self.window insertSubview:greenViewatIndex:2];

    //6.交换两个子试图的位置

    [self.window exchangeSubviewAtIndex:0withSubviewAtIndex:2];

    NSArray *subViewArr =self.window.subviews;//拿到所有的子试图

UIView *superView =redView.superview;//拿到某个试图的父试图

[redViewremoveFromSuperview];//从父试图上移除某个子试图  只有子试图可以从父试图上移除,父试图不能主动把某个子试图移除

BOOL ret = [greenViewisDescendantOfView:self.window];//鉴定试图之间的亲自(血缘)关系,隔代验证

view.layer.cornerRadius=10.0;//设置圆角

    view.layer.borderWidth=22.0;//设置边框宽度,边框占用视图面积

    view.layer.borderColor=[UIColorblueColor].CGColor;//设置边框颜色

    view.layer.masksToBounds=YES;//把子视图超出的部分切掉

弹窗提示

    UIAlertView * alert=[[UIAlertViewalloc]initWithTitle:@"动画执行完毕" message:@"郭文涛"delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];

    [alert show];

 

===============================================================================================

动画

    view.backgroundColor=[UIColor orangeColor];

    [UIView animateWithDuration:3 animations:^{

        view.backgroundColor=[UIColorblueColor];

        view.alpha=0.6;

        view.center=CGPointZero;

    } completion:^(BOOL finished) {

        view.center=CGPointMake(320, 480);

0 0
原创粉丝点击