gpt4 book ai didi

ios - 动画 Storyboard状态/位置

转载 作者:行者123 更新时间:2023-11-29 12:48:55 24 4
gpt4 key购买 nike

我在 Storyboard 中有一个 View Controller ,它有一堆图像、标签和按钮,这些图像、标签和按钮正确定位,以显示 View 在初始动画后的外观。

有没有一种简单的方法可以在 init 上保存每个元素的原始位置(也就是说它们在 Storyboard上的位置),以便可以获取这些元素,将它们移出 View 并将它们动画化到 Storyboard 布局/位置?

最佳答案

是的,这可以通过将所有 View 的约束保存在一个数组中,然后删除它们,并用新的屏幕外约束替换它们来完成(我的示例只有在您想移动 self.view 中的所有内容时才有效) -- 如果你只想移动一些 View ,那么你需要遍历所有 self.view 的约束,并只添加那些与你想移动到这个数组的 View 有关的约束)。当您想将 View 移动到 Storyboard定义的位置时,删除当前 View ,重新添加已保存的 View ,然后在动画 block 中调用 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];
}];
}

关于ios - 动画 Storyboard状态/位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22903571/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com