方向更改后视图滑动不正确

时间:2012-01-29 19:07:35

标签: xcode

我已设法通过点击按钮使这些视图相互推离屏幕。

但是,当设备更改为横向时,视图仅会将屏幕的一半推离并粘住。我理解为什么会这样,但不知道如何修复它。

在纵向和方向模式下,是否有办法在按钮单击时使视图完全脱离屏幕?这就是我需要它做的全部。

我的代码读起来像这样

.h文件

@interface AnimationBlocksViewController : UIViewController{
    IBOutlet UIView *theview;
    IBOutlet UIView *theview2;

    BOOL isAnimated;
    BOOL switchback;
}

-(IBAction)animate:(id)sender;
-(IBAction)change:(id)sender;

@end

.m文件

- (void)viewDidLoad
{

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    isAnimated = NO;
}

-(IBAction)animate:(id)sender;{
    if (isAnimated) {

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        [UIView setAnimationDuration:0.5];
        [theview setFrame:CGRectMake(0, 0, 320, 460)];
        [theview2 setFrame:CGRectMake(320, 0, 320, 460)];
        [UIView commitAnimations];
        isAnimated=YES;

    }


    else{

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        [UIView setAnimationDuration:0.5];
        [theview setFrame:CGRectMake(-320, 0, 320, 460)];
        [theview2 setFrame:CGRectMake(0, 0, 320, 460)];
        [UIView commitAnimations];
        isAnimated=NO;

    }
}

-(IBAction)change:(id)sender;{
    if (switchback) {

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        [UIView setAnimationDuration:0.5];
        [theview2 setFrame:CGRectMake(0, 0, 320, 460)];
        [UIView commitAnimations];
        switchback=NO;

    }


    else{

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        [UIView setAnimationDuration:0.5];
        [theview2 setFrame:CGRectMake(320, 0, 320, 460)];
        [theview setFrame:CGRectMake(0, 0, 320, 460)];
        [UIView commitAnimations];
        switchback=NO;

    }
}

我感谢任何反馈,样本或类似问题或教程的链接。

谢谢

2 个答案:

答案 0 :(得分:1)

我发现了解决问题的方法!我创建了一个与我的屏幕方向相关联的变量,它根据纵向或横向方向更改宽度和高度值。这有效,但仍有一点调试。如果有更好的解决方案,请告诉我。以下是需要放在我的.h和.m文件中的附加代码

.h代码

int scrnWdth;
int scrnHght;

@end

.m代码

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
        toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        scrnHght = 320;
        scrnWdth = 480;


    }
    else
    {
        scrnHght = 480;
        scrnWdth = 320;
    }
}

答案 1 :(得分:0)

您可以根据视图控制器视图的当前边界theview设置theview2[theview2 setFrame:CGRectMake(self.view.bounds.size.width, 0, 320, 460)];的坐标,而不是依赖这些常量值,而是可以简化此行为。这样您就不需要依赖于跟踪固定大小(例如,如果您的应用在用户通话时运行,如果您稍后选择隐藏状态栏,或者您放置此视图,则会更改导航或标签栏控制器内的控制器。)

我们可以将这些视图的大小完全基于视图控制器视图的边界:

CGRect frameForView2 = self.view.bounds;
frameForView2.origin.x = frameForView2.size.width;
[theview2 setFrame:frameForView2];

这样,您不仅可以确保将视图完全移出其父视图,还可以确保在移动视图时子视图填充其父视图(因为您将其宽度和高度设置为等于父视图的宽度)和身高)。