Re-initialize UIView view and subview after rotati

2019-08-17 15:55发布

问题:

I have a little problem with size reset of my views after rotation and i can't fix it.

I put an "swipe" action on my view "bottomContainerView" and the size is different depending the orientation!

here is my new code:

-(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 - Here is my new code correct with what you taught me and kindly explain ;-)

回答1:

My eyes take damage when looking at this code.

Here you are trying to combine 2 ways of declaring animation. If you are using beginAnimations: you don't have to use [UIView animateWithDuration:] method, but have to finish your animation code with [UIView commitAnimations];

The clearest way is to use [UIView animateWithDuration:] and put animation code inside animations:^{} block.

P.S: I strongly recommend not to use magic numbers in your code and use constants instead. SwitchToFullNormalScreen and SwitchToNormalScreen animations are just copypasted. I mean come on, that's not good.