旋转后重新初始化UIView视图和子视图

我有一个小的问题,我的意见大小重置后旋转,我无法修复它。

我在我的视图“bottomContainerView”上放一个“滑动”动作,根据方向大小不同!

这里是我的新代码:

-(void)swipeToggle:(UISwipeGestureRecognizer *)sender { if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) { if (sender.direction == UISwipeGestureRecognizerDirectionUp){ NSLog(@"if gesture up - LS"); [UIView animateWithDuration:0.5 delay:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{ topContainerView.frame = CGRectMake(0.0, -160.0, 480.0, 244.0); bottomContainerView.frame = CGRectMake(0.0, 0.0, 480.0, 300.0);} completion:^(BOOL finished){ NSLog(@"%f,%f",bottomContainerView.frame.size.width,bottomContainerView.frame.size.height); }]; } else if (sender.direction == UISwipeGestureRecognizerDirectionDown) { NSLog(@"else if gesture down - LS"); [UIView animateWithDuration:0.5 delay:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{ topContainerView.frame = CGRectMake(0.0, -160.0, 480.0, 244.0); bottomContainerView.frame = CGRectMake(0.0, 84.0, 480.0, 216.0);} completion:^(BOOL finished){ NSLog(@"%f,%f",bottomContainerView.frame.size.width,bottomContainerView.frame.size.height); }]; } } else if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait) { if (sender.direction == UISwipeGestureRecognizerDirectionUp) { NSLog(@"if gesture down - PT"); [UIView animateWithDuration:0.5 delay:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{ topContainerView.frame = CGRectMake(0.0, 0.0, 320.0, 244.0); bottomContainerView.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, 640.0); } completion:^(BOOL finished){ NSLog(@"%f,%f",bottomContainerView.frame.size.width,bottomContainerView.frame.size.height); }]; } else if (sender.direction == UISwipeGestureRecognizerDirectionDown) { NSLog(@"else if gesture up - PT"); [UIView animateWithDuration:0.5 delay:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{ topContainerView.frame = CGRectMake(0.0, 0.0, 320.0, 244.0); bottomContainerView.frame = CGRectMake(0.0, 244.0, self.view.frame.size.width, 216.0); } completion:^(BOOL finished){ NSLog(@"%f,%f",bottomContainerView.frame.size.width,bottomContainerView.frame.size.height); }]; } } 

}

已更新 – 这是我的新代码正确与你教我,并善意解释;-)

看着这个代码,我的眼睛受到了伤害。

在这里,你正试图结合2种声明animation的方法。 如果你正在使用beginAnimations:你不必使用[UIView animateWithDuration:]方法,但必须用[UIView commitAnimations];完成你的animation代码[UIView commitAnimations];

最明显的方法是使用[UIView animateWithDuration:]并将animation代码放入animations:^{}块。

PS:我强烈build议不要在代码中使用幻数,而是使用常量。 SwitchToFullNormalScreen和SwitchToNormalScreenanimation只是copypaasted。 我的意思是来吧,那不好。