我正在使用自定义控制器进行转换(由于项目中的固有周期,无法使用导航控制器,这将允许导航控制器堆栈无限增长[我认为会导致内存问题])。我正在模拟导航控制器滑动动画(当转换到新屏幕时)使用UIView animateWithDuration:动画:完成:
按下触发转换的按钮时,我要转换到的新视图的帧被设置为屏幕外位置。在过渡动画(UIView animateWithDuration:animations:completion :)中,当前屏幕上的视图将其框架设置为屏幕外位置,并将新视图设置为屏幕上位置。
这是我的自定义控制器中的转换:
CGFloat windowWidth = self.mainView.frame.size.width;
CGFloat windowHeight = self.mainView.frame.size.height;
CGRect offScreenLeft = CGRectMake(-1*windowWidth, 0.0, windowWidth, windowHeight);
CGRect onScreen = self.mainView.frame;
CGRect offScreenRight = CGRectMake(windowWidth, 0.0, windowWidth, windowHeight);
if (direction == TransitionDirectionForward)
{
if (dragBackgroundOnscreen){
[self.mainView addSubview:self.backgroundView];
[self.mainView sendSubviewToBack:self.backgroundView];
self.backgroundView.frame = offScreenRight;
}
self.currentViewController.view.frame = offScreenRight;
[UIView animateWithDuration:0.65
animations:^{
oldViewController.view.frame = offScreenLeft;
if (dragBackgroundOffscreen)
self.backgroundView.frame = offScreenLeft;
else if (dragBackgroundOnscreen)
self.backgroundView.frame = onScreen;
self.currentViewController.view.frame = onScreen;
}
completion:^(BOOL finished){
[oldViewController.view removeFromSuperview];
if (dragBackgroundOffscreen)
[self.backgroundView removeFromSuperview];
[oldViewController willMoveToParentViewController:nil];
[oldViewController removeFromParentViewController];
[self.currentViewController didMoveToParentViewController:self];
}];
}
else if (direction == TransitionDirectionBackward)
{
if (dragBackgroundOnscreen){
[self.mainView addSubview:self.backgroundView];
[self.mainView sendSubviewToBack:self.backgroundView];
self.backgroundView.frame = offScreenLeft;
}
self.currentViewController.view.frame = offScreenLeft;
[UIView animateWithDuration:0.65
animations:^{
oldViewController.view.frame = offScreenRight;
if (dragBackgroundOffscreen)
self.backgroundView.frame = offScreenRight;
else if (dragBackgroundOnscreen)
self.backgroundView.frame = onScreen;
self.currentViewController.view.frame = onScreen;
}
completion:^(BOOL finished){
[oldViewController.view removeFromSuperview];
if (dragBackgroundOffscreen)
[self.backgroundView removeFromSuperview];
[oldViewController willMoveToParentViewController:nil];
[oldViewController removeFromParentViewController];
[self.currentViewController didMoveToParentViewController:self];
}];
}
我还希望背景(self.backgroundView)保持静态,除非移动到具有自己背景的屏幕(即如果新视图背景是相同背景,我不希望背景滑动)。
我正在使用TransitionDirectionBackward和TransitionDirectionForward来区分向左滑动和向右滑动。
一切都很有效,除非转换涉及GMGridView。当Gridviews框架设置为屏幕外框架时,它确实将其当前所选页面的框架设置为该屏幕外框架时的问题。 gridview的其他页面不受此框架限制,因此即使在转换之前它们也可以出现在屏幕上。我尝试在GridView的viewcontroller视图中设置frame和bounds属性,但我仍然可以在转换动画之前在屏幕上显示gridview页面。
有人看到解决方案吗?我试图在转换过程中找到一种剪切GridView视图的方法,这样除了当前选择的页面之外,页面不会出现,但是没有找到任何有用的东西。
更新:我找到了一个可能的修复方法,通过为可见但不应该的单元格设置alpha = 0.0(稍后在转换动画完成时设置alpha = 1.0)。但是,我需要知道要执行此操作的单元格。我需要一种方法来访问GMGridView当前所在的页面,这样我就可以将相邻页面的单元格设置为0.0的alpha值。
更新:通过使用myGridView convertPoint找到一种方法让它工作:(通过试验和错误找到的一个cgpoint,位于页面的第一个单元格上。)fromView:myGridView.window。注意:我需要一个if / else if来检查我是否处于左侧或右侧,因为当旋转设备时窗口坐标不会旋转。有了这个,我能够获得我指定的屏幕上的单元格的索引,然后将上一页设置为透明,直到过渡动画之后。
我仍然想知道是否有一种方法可以剪切" gridview,如果细胞可能是不透明的,但从未显示....?
答案 0 :(得分:2)
我认为我过分复杂了这个问题。我一直在寻找UIView的clipsToBounds
方法(虽然我可以发誓之前我曾尝试过)。无论如何,它现在正在工作!