Masonry使用的一些场景的总结-2

Masonry最近才开始使用,还是有点不是很熟悉,下面是一些使用的场景总结。

想实现如下图所示的效果:

image

黑色背景为父试图,会随着标题和内容变化而自动适应高度。

代码如下:

+ (void)makeToast:(OSSessionModel*)model dismisBlock:(AlertDismisBlock)block{

    //OSSessionToastView 即为要显示的整个的弹框

    OSSessionToastView*toast = [[OSSessionToastView alloc]init];

    toast.backgroundColor= [UIColor darkGrayColor];

    toast.layer.cornerRadius=3.0;

    toast.layer.masksToBounds=YES;

    toast.backgroundColor= [UIColor blackColor];

    toast.alpha=1.0;

    toast.model= model;

    toast.alertDismisBlock= block;

    //获取当前的window

    UIWindow*myWindow = [OSSessionToastView getApplicationWindow];

    [myWindow addSubview:toast];

    [toast mas_makeConstraints:^(OSMASConstraintMaker*make) {

        //只设置两个约束,屏幕居中显示

        make.centerX.equalTo(myWindow);

        make.centerY.equalTo(myWindow);

}];

    //添加标题和内容

    [toast showToast:model];

}

- (void)showToast:(OSSessionModel*)model{

    self.model= model;

    //添加label

    [self showLabel];

    //动画

    [UIView animateWithDuration:1.0

    delay:0.0

    options:UIViewAnimationOptionCurveEaseOut

    animations:^{

        self.alpha=1.0;

    }completion:^(BOOL finished) {

        [UIView animateWithDuration:0.5

        delay:2.0

        options:UIViewAnimationOptionCurveEaseIn

    animations:^{

        self.alpha=0.0;    

    }completion:^(BOOLfinished) {

            [self removeFromSuperview];

        }];

    }];

}

- (void)showLabel{

    UIWindow*myWindow = [OSSessionToastView getApplicationWindow];

    //标题

    UILabel*titlelabel = [[UILabel alloc] init];

    titlelabel.backgroundColor= [UIColor clearColor];

    titlelabel.textAlignment=NSTextAlignmentCenter;

    titlelabel.font=DevSystemFontOfSize(18);

    titlelabel.textColor= [UIColor whiteColor];

    titlelabel.text=self.model.title;

    titlelabel.numberOfLines=1;

    [selfaddSubview:titlelabel];

    //内容

    UILabel*contentLa = [[UILabel alloc]init];

    contentLa.backgroundColor= [UIColor clearColor];

    contentLa.textAlignment=NSTextAlignmentCenter;

    contentLa.font=DevSystemFontOfSize(15);

    contentLa.textColor= [UIColor whiteColor];

    contentLa.text=self.model.content;

    contentLa.preferredMaxLayoutWidth= myWindow.bounds.size.width;//最大的宽度是屏幕的宽度

    contentLa.numberOfLines=0;

    [self addSubview:contentLa];

    //标题的约束

    [titlelabel mas_makeConstraints:^(OSMASConstraintMaker*make) {

    make.centerX.equalTo(self);//相对于父试图 居中显示

}];

//内容的约束

[contentLamas_makeConstraints:^(OSMASConstraintMaker*make) {

    make.top.equalTo(titlelabel.mas_lastBaseline).offset(15);//标题的下方15

    make.centerX.equalTo(self);//在父试图中居中显示

    make.left.equalTo(self);//左边和父试图对齐

    make.right.equalTo(self);//右边和父试图对齐

}];

//父试图的约束

[self mas_makeConstraints:^(OSMASConstraintMaker*make) {

    make.top.equalTo(titlelabel).offset(-10);//距离标题10

    make.left.equalTo(contentLa);//和内容左对齐

    make.right.equalTo(contentLa);//和内容右对齐

    make.bottom.equalTo(contentLa).offset(10);//底部和内容距离10

}];

}

+ (UIWindow*)getApplicationWindow

{

    UIWindow*window = [[UIApplicationsharedApplication]keyWindow];

    if(nil==window || window.windowLevel!=UIWindowLevelNormal)

{

        NSArray*windows = [[UIApplicationsharedApplication]windows];

        for(UIWindow* tmpWininwindows)

{

if(tmpWin.windowLevel==UIWindowLevelNormal){
    window = tmpWin;
    break;
      }
    }
  }
  returnwindow;
}

你可能感兴趣的:(Masonry使用的一些场景的总结-2)