重新初始化旋转后的UIView视图和子视图 [英] Re-initialize UIView view and subview after rotation

查看:140
本文介绍了重新初始化旋转后的UIView视图和子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我把我的观点bottomContainerView一个刷卡动作和大小根据不同的方位!

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

这是我的新的code:

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

    }
}        

}

公布更新后 - 这是我的新code正确与你教会了我什么,并亲切地解释; - )

POST UPDATED - Here is my new code correct with what you taught me and kindly explain ;-)

推荐答案

看着这个code时,我的眼睛受到伤害。

My eyes take damage when looking at this code.

在这里,你正试图宣布动画2种方法结合起来。
如果您在使用 beginAnimations:您不必使用 [UIView的animateWithDuration:] 方法,但必须完成动画code。与 [UIView的commitAnimations];

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];

最明显的方法是使用 [UIView的animateWithDuration:] ,并把动画code 动画里:^ {} 块。

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

P.S:我强烈建议不要在你的code使用幻数和使用常量来代替。 SwitchToFullNormalScreen和SwitchToNormalScreen动画只是copypasted。我的意思是,来吧,这是不好的。

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.

这篇关于重新初始化旋转后的UIView视图和子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆