我在故事板中有两个视图控制器,由一个segue链接。第一个视图控制器做了一个动画,它将UIImage从屏幕中间滑动到顶部,然后显示UIButton,所有这些都很好。然而,当按钮被触摸时,我执行一个segue,在模态上呈现第二视图控制器。但当第二视图控制器在屏幕上滑动时,我可以看到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正在从情节提要加载第一个视图控制器吗?如何在视图控制器之间来回转换,使图像保持动画后的状态?
这可能是由于自动布局。在启用自动布局的情况下更改框架时,任何其他需要系统重新布局视图的操作都会导致框架恢复为由约束定义的框架。因此,您应该关闭自动布局,或者通过修改布局约束来更改原点。使用自动布局时,不应进行任何框架设置,只应修改约束。