动画到故事板状态/位置 [英] Animate to a storyboard state / position

查看:119
本文介绍了动画到故事板状态/位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Storyboard中有一个视图控制器,其中有一堆图片,标签和按钮正确放置在 之后。

I've got a view controller in Storyboard that has a bunch of images, labels and buttons correctly positioned for how the view is supposed to look after initial animations.

有一个简单的方法来保存每个元素的原始位置(也就是说,他们在故事板上的位置)在init,以便可以采取这些元素,

Is there a simple way to save the original position of each and every element (that is to say the position that they're in on the Storyboard) on init so that it's possible to take those elements, move them out of view and animate them to the Storyboard layout / positions?

推荐答案

是的,可以通过保存所有的视图数组中的约束,然后删除它们,并用新的离屏约束替换它们(例如,如果你想移动self.view中的所有内容,我将只能工作 - 如果你只想移动一些视图,那么你需要循环遍历所有的self.view的约束,并只添加那些属于你想移动到这个数组的视图。当你想将视图移动到他们的storyboard定义的位置,删除当前的,重新添加保存的,并在动画块中调用layoutIfNeeded。

Yes this can be done by saving all the view's constraints in an array, then removing them, and replacing them with the new off-screen constraints (the example I have will only work if you want to move everything in self.view -- if you only want to move some of the views, then you need to loop through all of self.view's constraints, and add only those that pertain to the views you want to move to this array). When you want to move the views to their storyboard defined positions, remove the current ones, re-add the saved ones, and call layoutIfNeeded in an animation block.

@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];
    }];
}

这篇关于动画到故事板状态/位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆