IOS autoLayout 简单的使用动画效果



#import "AutoLayoutViewController.h"


@interface AutoLayoutViewController (){


    UIButton *_btn;

    UIImageView *_img;

    UILabel *_label;

    NSArray *imag;



}



@end


@implementation AutoLayoutViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    self.view.backgroundColor = [UIColor redColor];

    [self initaa];

    

    [self initbb];

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


-(void)initaa{



    

    _btn = [UIButton buttonWithType:UIButtonTypeCustom];

    [_btn setTranslatesAutoresizingMaskIntoConstraints:NO];

    [_btn setTitle:@"dianji " forState:UIControlStateNormal];

    [_btn setBackgroundColor:[UIColor darkGrayColor]];

    [_btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_btn];

    

    

    _label = [[UILabel alloc] init];

    [_label setTranslatesAutoresizingMaskIntoConstraints:NO];

    //[_label setBackgroundColor:[UIColor whiteColor]];

    _label.lineBreakMode = NSLineBreakByWordWrapping;

    _label.numberOfLines = 0;

    _label.preferredMaxLayoutWidth = self.view.frame.size.width - 50;

    [_label setText:@"喝水啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊"];

    [self.view addSubview:_label];

    

    _img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"5_hzeximg67.png"]];

    [_img setTranslatesAutoresizingMaskIntoConstraints:NO];

    [self.view addSubview:_img];

    

    

}



-(void)initbb{

    NSDictionary *dic1 = NSDictionaryOfVariableBindings(_img,_label,_btn);

    

    NSDictionary *metrices = @{@"hPadding":@10, @"vPadding":@5, @"imageEdg":@150.0};

    

    //设置图片横向居中

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_img]-|" options:0 metrics:metrices views:dic1]];

    //设置图片距离顶部25间距和高度

    imag = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-25-[_img(<=150)]" options:0 metrics:metrices views:dic1];

    [self.view addConstraints:imag];

    

    //设置label的垂直位置距离imageView 10 px

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_img]-10-[_label]" options:0 metrics:metrices views:dic1]];

    //设置label的水平位置距离

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_label(_img)]-|" options:0 metrics:metrices views:dic1]];

    

    //设置按钮的位置 离屏幕底部5 px

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_label]-(>=20)-[_btn(20)]-5-|" options:0 metrics:metrices views:dic1]];

    

    //设置按钮的水平位置

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[_btn]" options:0 metrics:metrices views:dic1]];

    

    

}




-(void)click{


    [UIView animateWithDuration:2.5 animations:^{

        

        [self.view removeConstraints:imag];

        

        

          [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[_img]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_img)]];

        

        

        [self.view layoutIfNeeded];//这句话是执行动画必须加的否则不会执行动画效果 只会一瞬间执行


    }];

    

    

    

  



}







@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值