2012-01-29 58 views
0

我设法让这些视图互相推开屏幕,只需点击一下按钮。改变方向后视图幻灯片不正确

但是,当设备更改为横向时,视图只能将屏幕向外推一半并粘住。我明白为什么会发生这种情况,但不知道如何解决。

有没有一种方法可以使视图完全在屏幕上按下一个按钮并在纵向和横向模式下单击?这就是我需要它做的。

我的代码读取这样的

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

    } 
} 

我明白任何反馈,样品或链接到类似的问题或教程。

谢谢

回答

0

而不是依靠这些常量值,您可以通过基于视图控制器视图的当前边界设置坐标theviewtheview2来简化此行为[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]; 

这样,你不仅要确保移动完全不在它的父视图的视图也可以确保你的子视图在你将它们移回时(因为你的宽度和高度等于父视图的宽度和高度)。

+0

我看到“self.view.bounds.size.width”是理想的解决方案。有没有一种方法可以让它成为一个负面的,将它从​​屏幕上移开? – 2012-02-02 03:09:53

+0

哦,等等,我只是使用 - =这很好,非常感谢你! – 2012-02-02 04:23:22

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