- (IBAction)StartGame:(id)sender
{
MainGameDisplay *secondPage = [[MainGameDisplay alloc] initWithNibName:nil bundle:nil];
[self.view addSubview:secondPage.view];
secondPage.view.frame = CGRectMake(568, 0, 568, 320);//(N = horizontal, N = vertical)
[UIView animateWithDuration:1.0f
animations:^{
//actual frame needed with an animation.
secondPage.view.frame = CGRectMake(0, 0, 568, 320);
}
completion:^(BOOL finished) {
//ENTER HERE ANYTHING TO RUN AFTER ANIMATION IS COMPLETED:
[self presentViewController: secondPage animated:NO completion:NULL];
//This will make the next page load correctly after the transition, otherwise you cannot. interact with anything.
}];
}
这非常适合进入 View 。如果没有 [selfpresentViewController: secondaryPageanimated:NOcompletion:NULL];
,您将无法与新页面交互,单击任何按钮都会导致其崩溃。
如果我返回到第一页,正常执行,那就没问题,但如果我再次单击 StartGame,程序会更改 View ,然后崩溃。问题出在行 [self.view addSubview:secondPage.view];
上,如果我注释掉这一行,程序不会崩溃,但当它更改 View 时,它不会转换并立即出现.
在横向模式下,过渡也相当滞后/故障,但在纵向模式下则完美。
最佳答案
这里您将在 currentView 上添加 secondPage
作为 subview 。再次你的介绍。
我认为它导致一些与内存相关的事情可能是其他事情(如果你在问题中放一些崩溃日志
,就会很清楚)。但我建议您无需再次呈现 secondPage
。这是您的代码,我在此处进行了单行更改。
- (IBAction)StartGame:(id)sender
{
MainGameDisplay *secondPage = [[MainGameDisplay alloc] initWithNibName:nil bundle:nil];
[self.view addSubview:secondPage.view];
secondPage.view.frame = CGRectMake(568, 0, 568, 320);//(N = horizontal, N = vertical)
[UIView animateWithDuration:1.0f
animations:^{
//actual frame needed with an animation.
secondPage.view.frame = CGRectMake(0, 0, 568, 320);
}
completion:^(BOOL finished) {
//ENTER HERE ANYTHING TO RUN AFTER ANIMATION IS COMPLETED:
您应该在sinlge下面发表评论,因为您已经将secondPage添加为currentView上的 subview 。因此无需再次呈现它。
// [self presentViewController: secondPage animated:NO completion:NULL];
//This will make the next page load correctly after the transition, otherwise you cannot. interact with anything.
}];
编辑:对于横向模式,您应该在设置框架 secondPage
View 时更改坐标。我认为您正在为 4 英寸设备创建此 View 。
在这里你可以通过设置一些检查来做到这一点
if(checkIfDeviceIsInlandscae)
{
secondPage.view.frame = CGRectMake(568, 0, 568, 320);
}
else
{
secondPage.view.frame = CGRectMake(320, 0, 320, 568);
}
在动画
block 内根据方向更改框架,就像我在拖线上方所做的那样。
关于iphone - UIView 转换导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13295626/