轮换后重新初始化UIView视图和子视图

时间:2013-07-06 18:49:07

标签: ios animation uiview rotation

旋转后我的视图大小重置有点问题,我无法修复它。

我在“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); 
                         }];

    }
}        

}

POST UPDATED - 这是我的新代码,正如你所教导的那样,并且善意解释; - )

1 个答案:

答案 0 :(得分:0)

查看此代码时,我的眼睛会受到伤害。

在这里,您尝试组合两种声明动画的方式。 如果您使用beginAnimations:,则不必使用[UIView animateWithDuration:]方法,但必须使用[UIView commitAnimations];完成动画代码

最明确的方法是使用[UIView animateWithDuration:]并将动画代码放在animations:^{}块中。

P.S:我强烈建议不要在代码中使用幻数,而是使用常量。 SwitchToFullNormalScreen和SwitchToNormalScreen动画只是复制。我的意思是来,这不好。