ios - 在 iOS 中,动画 View 在手动调用 segue 后返回到其原始状态

标签 ios animation storyboard state segue

我在 Storyboard 中有两个 View Controller,通过 segue 链接。第一个 View Controller 执行一个动画,它将一个 UIImage 从屏幕中间滑动到顶部,然后显示一个 UIButton,一切都很好。然而,当按钮被触摸时,我执行一个 segue,在模态上呈现第二个 View Controller 。但是当第二个 View Controller 向上滑动屏幕时,我可以看到 UIImage 返回到屏幕中间,就像它被动画化之前一样。

就是这样:

- (void)viewDidLoad{
    [super viewDidLoad];
    self.willAnimate=YES;
}

- (void)viewDidAppear:(BOOL)animated{
    if (self.willAnimate) [self animate];
}
- (void) animate{
    if (self.willAnimate){
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1.8];
            //hide button first
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.1];
            self.myButton.alpha=0;
            [UIView commitAnimations];

        //move background
        CGRect imgRect = self.myImageView.frame;
        imgRect.origin.y-=200;
        self.myImageView.frame=backgroundRect;

        //restore button
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.5];
            [UIView setAnimationDelay:1.8];
            self.myButton.alpha=1;
            [UIView commitAnimations];
        [UIView commitAnimations];
        self.willAnimate=NO;
    }
}

- (IBAction)myButtonPressed {
    [self performSegueWithIdentifier:@"mySegue" sender:self ];
}

发生这种情况是因为 segue 正在从 Storyboard 中加载第一个 View Controller 吗?我怎样才能在 View Controller 之间来回转换,使图像保持动画结束后的状态?

最佳答案

这可能是由于自动布局。当您更改启用自动布局的框架时,任何其他需要系统重新布局 View 的操作都会导致框架恢复为由约束定义的框架。因此,您应该关闭自动布局,或者通过修改布局约束来更改原点。当你使用自动布局时,你不应该做任何框架的设置,只能修改约束。

关于ios - 在 iOS 中,动画 View 在手动调用 segue 后返回到其原始状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18008927/

相关文章:

ios - 以编程方式将 stackview 与屏幕顶部对齐,不使用硬编码的 CGRect 和最小数量的约束

Android动画TextView从页面中间到顶部

ios - UITableView 单元格行为不当,显示没有披露等

ios7 - 自动布局 CGAffineTransform iOS7 iOS8

iphone - 如何在 AVCapture 视频上添加 CALayer 矩形?

ios - 在框架提供的 View Controller 上重写 didSelectRowAtIndexPath?

iOS8 + UIDeviceOrientationDidChangeNotification 没有被调用

android - 如何在android中为progressBar设置流畅的动画

javascript - 将动画函数作为参数传递

iphone - 有没有办法让应用程序代理运行条件,然后选择 Storyboard入口点?