ios - 动画 Storyboard状态/位置

标签 ios iphone objective-c cocoa-touch storyboard

我在 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/

相关文章:

ios - 如何使用 objective-c 将图像存储在缓存内存中

iphone - 如何将 viewController 放入 UIScrollView 中

ios - 父 View 框架尺寸更改后更新约束

iphone - CGImageDestinationCreateWithData 没有创建 CGImageDestinationRef 对象

ios - 从 iPhone 发送图像作为表单数据的一部分

ios - 如何在越狱的 iOS 5 中启用 ChatKit CKMadridService?

ios - 如何快速移动在 Storyboard中设置了约束的 UI 对象?

iOS - 获取 ARP 表

ios - 看不到标签栏图像

iphone - CLLocationManager 有没有可能被其他应用影响?