gpt4 book ai didi

ios - UIViewController 之间的自定义转换

转载 作者:行者123 更新时间:2023-11-28 22:29:46 26 4
gpt4 key购买 nike

我刚刚将我的应用程序从 TabView 驱动更改为 CollectionView 驱动,因为我的应用程序的部分太多以至于 TabView 不可行。当您启动该应用程序时,您会看到 CollectionView 中的多个项目,选择这些项目中的任何一个都会将您带到该应用程序的相关部分。

在 XCode 中, Collection View 存在于它自己的 Storyboard中,应用程序的每个部分都有自己的 Storyboard。

在CollectionView的didSelectItemAtIndexPath中,我启动相关的右舷如下;

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"relevant_storyboard" bundle:nil];
UIViewController* vc = [storyboard instantiateInitialViewController];
[self presentViewController:vc animated:YES completion:nil];

现在,没有一个内置的过渡动画真的适合从 CollectionView 启动,所以我真的很喜欢自定义效果,例如放大。但是,我正在努力寻找适合我的任何像样的示例创建任何类型的自定义过渡。我试过 [UIView transitionFromView],但我认为这不适合在 UIViewController 之间进行转换。我试过 transitionFromViewController:toViewController: 但我认为我没有正确设置 View 层次结构。我也尝试过使用 CATransition 但没有成功。

我考虑过使用自定义 segue 来实现它,但是,由于我的 CollectionView 在它自己的 Storyboard中,并且我的应用程序的每个部分都有单独的 Storyboard,所以我看不出如何做到这一点。至少在没有将应用程序的所有部分都放在一个 Storyboard中的情况下,这会使 Storyboard变得庞大且难以管理。

那么,任何人都可以给我任何代码示例或指导,告诉我如何解决这个问题吗?

最佳答案

在我的应用程序中,我使用类似的效果从 Collection View 单元格中的缩略图放大到占据整个屏幕的 subview Controller 。可以想象,您也可以为导航 Controller 推送做同样的事情。

在我的代码中,我在要放大到全屏的单元格子类上有一个 scoreView 属性。在您的情况下,您可能希望使用带有新 View 屏幕截图的 UIImageView。或者,您可以向新 View Controller 显示旧 View Controller 的屏幕截图,然后从那里制作动画。

//Instantiate the view controller to be animated in...

//If the cell is not completely inside the collection view's frame, a dissolve animation might be more graceful.
BOOL dissolveAnimation = !CGRectContainsRect(CGRectMake(0, 0, self.collectionView.frame.size.width, self.collectionView.frame.size.height), cellRect);

//Get the frame of the cell in self.view coordinates, then the frame of the thumbnail view
CGRect cellRect = [self.collectionView layoutAttributesForItemAtIndexPath:indexPath].frame;
cellRect = CGRectOffset(cellRect, 0.0, -self.collectionView.contentOffset.y);
VSScoreCell *scoreCell = (VSScoreCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
CGRect scoreRect = dissolveAnimation ? CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height) : CGRectMake(cellRect.origin.x + scoreCell.scoreView.frame.origin.x, cellRect.origin.y + scoreCell.scoreView.frame.origin.y, scoreCell.scoreView.frame.size.width, scoreCell.scoreView.frame.size.height);
VSScoreView *scoreView = [[VSScoreView alloc] initWithFrame:scoreRect];

//Initialize the view that will be animated up (in this case scoreView)...

if (dissolveAnimation)
scoreView.alpha = 0.0;

[self.view addSubview:scoreView];

[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
if (dissolveAnimation)
scoreView.alpha = 1.0;
else
scoreView.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height);
} completion:^(BOOL finished) {
if (finished)
{
//Add scoreDisplayController as a child view controller or present it without animation
[scoreView removeFromSuperview];
}
}];

当然,新的 iOS 可能会使这更容易(我的嘴唇密封),但我希望这对您的情况有所帮助!

关于ios - UIViewController 之间的自定义转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17776229/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com