您的位置:首页 > 移动开发 > IOS开发

IOS autoLayout 简单的使用动画效果

2014-09-17 14:57 751 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息