假设我有这个 UIView
:
具有这些相对约束:
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *leftMarginConstraint;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *topMarginConstraint;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *widthConstraint;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
好吧,现在让我们假设当用户点击那个 UIButton
按钮应该移动到 View 的右下角。我们可以轻松地使用两个约束来定义按钮和底部布局指南之间的底部空间以及按钮和 View 右边缘之间的右侧空间(尾随空间)。
问题是 UIButton
已经有两个约束(左/上)和两个定义其宽度和高度的约束,所以我们不能添加两个新的约束,因为它们会相互冲突
动画场景的简单和常见情况,但它给我带来了一些问题。想法?
编辑
当用户点击 UIButton
时,我需要该按钮:
- 将其标题更改为“第二”
- 等待 1 秒并移动到右下角(移除顶部和左边距约束并添加底部和右边距约束)
- 将其标题更改为“第三”
- 等待 1 秒并移动到右上角(移除底部边距约束 并添加上边距约束)
我真的一定要使用这些乱七八糟的代码吗?
@implementation ViewController
{
NSLayoutConstraint *_topMarginConstraint;
NSLayoutConstraint *_leftMarginConstraint;
NSLayoutConstraint *_bottomMarginConstraint;
NSLayoutConstraint *_rightMarginConstraint;
}
- (IBAction)buttonPressed:(id)sender
{
UIButton *button = sender;
// 1.
[sender setTitle:@"Second" forState:UIControlStateNormal];
// 2.
double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {
[button removeConstraints:@[self.leftMarginConstraint, self.topMarginConstraint]];
_bottomMarginConstraint = [NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeBottom
relatedBy:0 toItem:button
attribute:NSLayoutAttributeBottom
multiplier:1
constant:20];
[self.view addConstraint:_bottomMarginConstraint];
_rightMarginConstraint = [NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeRight
relatedBy:0 toItem:button
attribute:NSLayoutAttributeRight
multiplier:1
constant:20];
[self.view addConstraint:_rightMarginConstraint];
[UIView animateWithDuration:1 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
// 3.
[sender setTitle:@"Third" forState:UIControlStateNormal];
// 4.
double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {
[button removeConstraint:_bottomMarginConstraint];
_topMarginConstraint = [NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeTop
relatedBy:0 toItem:button
attribute:NSLayoutAttributeTop
multiplier:1
constant:20];
[UIView animateWithDuration:1 animations:^{
[self.view layoutIfNeeded];
}];
});
}];
});
}
严重吗? :D
最佳答案
删除左侧和顶部约束,添加底部和右侧约束,然后在动画 block 中调用 layoutIfNeeded 以动画显示到新位置。第二种方法 move2 中的代码可以嵌入到 move1 的完成 block 中,但我发现如果将两个移动放在不同的方法中会更容易阅读(它确实需要添加另一个属性 bottomCon):
- (IBAction)move1:(UIButton *)sender {
[sender setTitle:@"Second" forState:UIControlStateNormal];
[sender layoutIfNeeded];
[self.view removeConstraints:@[self.leftCon,self.topCon]];
self.bottomCon = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:0 toItem:self.button attribute:NSLayoutAttributeBottom multiplier:1 constant:20];
[self.view addConstraint:self.bottomCon];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeRight relatedBy:0 toItem:self.button attribute:NSLayoutAttributeRight multiplier:1 constant:20]];
[UIView animateWithDuration:1 delay:1.0 options:0 animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
[sender setTitle:@"Third" forState:UIControlStateNormal];
[self move2];
}];
}
-(void)move2 {
[self.view removeConstraint:self.bottomCon];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeTop relatedBy:0 toItem:self.button attribute:NSLayoutAttributeTop multiplier:1 constant:-20]];
[UIView animateWithDuration:1 delay:1.0 options:0 animations:^{
[self.view layoutIfNeeded];
} completion:nil];
}
关于ios - 自动布局、约束和动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19899323/