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