我有在故事板视图控制器,有一堆图像,标签和按钮位置正确的观点应该如何初始动画照顾 。
有一个简单的方法来保存每个原来的位置,每一个元素(也就是说,他们所处的故事板的位置)上的init,这样有可能把这些元素,将其移出的观点和它们的动画到故事板布局/职位?
我有在故事板视图控制器,有一堆图像,标签和按钮位置正确的观点应该如何初始动画照顾 。
有一个简单的方法来保存每个原来的位置,每一个元素(也就是说,他们所处的故事板的位置)上的init,这样有可能把这些元素,将其移出的观点和它们的动画到故事板布局/职位?
是的,这可以通过保存的所有视图的限制在一个数组,然后删除它们,并用新的屏幕外的约束来替换(如果你想在self.view移动我的一切的例子只会工作完成 - 如果你只需要移动的一些看法,那么你就需要遍历所有的self.view的限制,并添加只有那些涉及到你想要移动到该阵列)的意见。 如果要移动的意见,他们的故事板定义的位置,删除当前的,重新添加得救的,并呼吁layoutIfNeeded在动画块。
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *bottomButton;
@property (weak, nonatomic) IBOutlet UIButton *topButton;
@property (weak, nonatomic) IBOutlet UIButton *middleButton;
@property (strong,nonatomic) NSArray *finalConstraints;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSDictionary *viewsDict = NSDictionaryOfVariableBindings(_bottomButton,_topButton,_middleButton);
self.finalConstraints = self.view.constraints; // save the storyboard constraints
[self.view removeConstraints:self.view.constraints]; // remove all the storyboard constraints
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[_topButton]-(-30)-|" options:0 metrics:nil views:viewsDict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_topButton]-(-30)-|" options:0 metrics:nil views:viewsDict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[_middleButton]-(-30)-|" options:0 metrics:nil views:viewsDict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_middleButton]-(-30)-|" options:0 metrics:nil views:viewsDict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[_bottomButton]-(-30)-|" options:0 metrics:nil views:viewsDict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_bottomButton]-(-30)-|" options:0 metrics:nil views:viewsDict]];
[self performSelector:@selector(moveToFinalPositions) withObject:nil afterDelay:2];
}
- (void)moveToFinalPositions {
[self.view removeConstraints:self.view.constraints];
[self.view addConstraints:self.finalConstraints];
[UIView animateWithDuration:2 animations:^{
[self.view layoutIfNeeded];
}];
}