【问题标题】:Animate to a storyboard state / position动画到故事板状态/位置
【发布时间】:2014-05-19 04:10:20
【问题描述】:

我在 Storyboard 中有一个视图控制器,其中包含一组正确定位的图像、标签和按钮,以便视图在初始动画之后的外观。

有没有一种简单的方法可以将每个元素的原始位置(即它们在情节提要上的位置)保存在 init 上,以便可以将这些元素移出视图并将它们动画到情节提要布局/位置?

【问题讨论】:

    标签: ios iphone objective-c cocoa-touch storyboard


    【解决方案1】:

    是的,这可以通过将所有视图的约束保存在一个数组中,然后删除它们,并用新的屏幕外约束替换它们来完成(我的示例仅在您想移动 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];
        }];
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-26
    • 2011-07-20
    • 2012-02-21
    • 2011-01-28
    • 2021-05-05
    • 2020-05-19
    • 2012-02-17
    相关资源
    最近更新 更多