在沒有自動布局之前我們都是用Frame
去布局view
驯绎,然后可以對view
做各種動畫
- 大小變化(frame)
- 拉伸變化(bounds)
- 中心位置(center)
- 旋轉(zhuǎn)(transform)
- 透明度(alpha)
- 背景顏色(backgroundColor)
- 拉伸內(nèi)容(contentStretch)
但是有了自動布局之后吨拗,之前的方法稍微有所不同, 具體實(shí)現(xiàn)如下:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView *tview = [[UIView alloc] init];
tview.backgroundColor = [UIColor redColor];
[self.view addSubview:tview];
[tview mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(tview.superview).offset(200);
make.left.equalTo(tview.superview).offset(50);
make.right.equalTo(tview.superview).offset(-100);
make.height.equalTo(80);
}];
self.testView = tview;
[self performSelector:@selector(startAnimation) withObject:nil afterDelay:1];
}
- (void)startAnimation{
//重點(diǎn)
[self.testView mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.testView.superview).offset(300);
}];
[UIView animateWithDuration:0.66 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//重點(diǎn)
[self.testView.superview layoutIfNeeded];
} completion:^(BOOL finished) {
}];
}
從最終效果來看好像沒有什么問題榄鉴,但是注意我們對top
用了mas_updateConstraints
趾徽,但是沒有uninstall
租谈,這樣的話有時(shí)候會有約束沖突奢驯,保險(xiǎn)的是先[self.testViewTop uninstall]
适掰,然后mas_updateConstraints
,具體實(shí)現(xiàn)如下:
@property (nonatomic, strong) MASConstraint *testViewTopConst;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView *tview = [[UIView alloc] init];
tview.backgroundColor = [UIColor redColor];
[self.view addSubview:tview];
[tview mas_makeConstraints:^(MASConstraintMaker *make) {
self.testViewTopConst = make.top.equalTo(tview.superview).offset(200);//重點(diǎn)
make.left.equalTo(tview.superview).offset(50);
make.right.equalTo(tview.superview).offset(-100);
make.height.equalTo(80);
}];
self.testView = tview;
[self performSelector:@selector(startAnimation) withObject:nil afterDelay:1];
}
- (void)startAnimation{
[self.testViewTopConst uninstall];//重點(diǎn)
[self.testView mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.testView.superview).offset(300);
}];
[UIView animateWithDuration:0.66 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
[self.testView.superview layoutIfNeeded];
} completion:^(BOOL finished) {
}];
}
除過這個(gè)方法還有一種更簡單塑煎,安全的沫换,就是我們把我們需要改變的約束用系統(tǒng)的NSLayoutConstraint
來添加約束,最終只要修改constant
值就行轧叽,具體如下:
@property (nonatomic, strong) NSLayoutConstraint *topConst;//重點(diǎn)
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView *tview = [[UIView alloc] init];
tview.backgroundColor = [UIColor redColor];
[self.view addSubview:tview];
[tview mas_makeConstraints:^(MASConstraintMaker *make) {
//make.top.equalTo(tview.superview).offset(200);
make.left.equalTo(tview.superview).offset(50);
make.right.equalTo(tview.superview).offset(-100);
make.height.equalTo(80);
}];
NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:tview
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:tview.superview
attribute:NSLayoutAttributeTop
multiplier:1
constant:50];
[self.view addConstraint:topConstraint];//重點(diǎn)
self.topConst = topConstraint;//重點(diǎn)
self.testView = tview;
[self performSelector:@selector(startAnimation) withObject:nil afterDelay:1];
}
- (void)startAnimation{
self.topConst.constant = 300;//重點(diǎn)
[UIView animateWithDuration:0.66 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
[self.testView.superview layoutIfNeeded];
} completion:^(BOOL finished) {
}];
}
總結(jié):
- 方法一:直接用
mas_updateConstraints
- 方法二:先
uninstall
苗沧,然后mas_updateConstraints
- 方法三刊棕; 用系統(tǒng)的
NSLayoutConstraint
添加約束,然后修改constant
值待逞。
建議使用第三種簡單甥角,安全