Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode 方向更改后视图滑动不正确_Xcode - Fatal编程技术网

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

Xcode 方向更改后视图滑动不正确,xcode,Xcode,我已经设法使这些视图通过点击按钮将彼此推离屏幕 但是,当设备更改为横向时,视图只会离开屏幕一半并粘住。我理解为什么会发生这种情况,但不知道如何解决 在纵向和定向模式下,有没有办法让点击按钮时的视图完全滑出屏幕?这就是我所需要做的 我的代码是这样的 .h文件 @interface AnimationBlocksViewController : UIViewController{ IBOutlet UIView *theview; IBOutlet UIView *theview2;

我已经设法使这些视图通过点击按钮将彼此推离屏幕

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

在纵向和定向模式下,有没有办法让点击按钮时的视图完全滑出屏幕?这就是我所需要做的

我的代码是这样的

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

    }
}
我非常感谢任何反馈、示例或类似问题或教程的链接


谢谢

我找到了解决问题的办法!我创建了一个与屏幕方向相关的变量,该变量根据纵向或横向方向更改宽度和高度值。这是可行的,但还有一点调试工作要做。如果有更好的解决方案,请告诉我。下面是需要放在my.h和.m文件中的附加代码

.h代码

int scrnWdth;
int scrnHght;
@结束

.m代码

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


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

不依赖这些常量值,您可以通过基于视图控制器视图的当前边界设置视图和视图的坐标来简化此行为,即
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];
这样,您不仅可以确保将视图完全移到其父视图之外,还可以确保将子视图移回时填充其父视图(因为您将其宽度和高度设置为该父视图的宽度和高度)。

我认为“self.view.bounds.size.width”是理想的解决方案。有没有办法让这张底片离开屏幕?