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
@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
相关文章推荐
- iOS-AutoLayout中动画使用的细节 和 iOS layout机制
- 关于ios的autoLayout的一些简单介绍以及使用方法
- 【源代码】手把手教你Autolayout如何使用动画(附类似Passbook效果Demo+详细分析实现原理)
- 关于ios的autoLayout的一些简单介绍以及使用方法
- 【iOS开发-113】在storyboard上用AutoLayout,纯代码实现AutoLayout布局方法以及简单动画
- ios Autolayout改变约束, 动画效果处理
- iOS 使用Storyboard + AutoLayout布局,各种型号设备效果预览
- Masonry介绍与使用实践(快速上手Autolayout)-让IOS自动化相对布局更简单
- iOS中autolayout的简单使用
- iOS-AutoLayout中动画使用的细节 和 iOS layout机制
- 关于ios的autoLayout的一些简单介绍以及使用方法
- ios学习之autolayout自动布局的简单理解 即UIKIt dymanic的使用
- 【iOS开发-113】在storyboard上用AutoLayout,纯代码实现AutoLayout布局方法以及简单动画
- iOS AutoLayout Constraint如何实现动画效果
- iOS添加到购物车的简单动画效果
- Core Animation基础介绍、简单使用CALayer以及多种动画效果
- ViewPager使用1-ViewPager多页面滑动切换以及动画效果(ViewPager+Layout+PagerAdapter )
- iOS 动画效果。简单的提示消失
- ios8来了,屏幕更大,准备好使用 iOS Auto Layout了吗?
- iOS 动画效果。简单的提示消失