我正在iOS 8上构建一个应用程序,并且正在寻找在创建新的电子邮件/消息时复制iOS邮件应用程序的功能.如下所示:组合视图控制器显示在收件箱视图控制器的顶部,但组合vc不占用整个屏幕.有没有更简单的方法来做这个,而不是用视图控制器的框架进行黑客攻击?谢谢!
解决方法
这个效果可以通过在iOS 8中提供的UIPresentationController来实现.苹果有一个关于这个主题的WWDC ’14视频以及这个帖子底部找到的一些有用的示例代码(我在这里发布的原始链接不再有效).
*演示称为“LookInside:演示控制器适应性和自定义动画对象”.苹果代码中有一些错误对应于过时的API使用,可以通过将破坏的方法名称(在多个地方)更改为以下内容来解决:
initWithPresentedViewController:presentingViewController:
您可以在iOS 8邮件应用程序上复制动画.为了达到预期的效果,下载我上面提到的项目,然后你所要做的就是改变一些事情.
首先,转到AAPLOverlayPresentationController.m,并确保已经实现了frameOfPresentedViewInContainerView方法.我看起来像这样:
- (CGRect)frameOfPresentedViewInContainerView { CGRect containerBounds = [[self containerView] bounds]; CGRect presentedViewFrame = CGRectZero; presentedViewFrame.size = CGSizeMake(containerBounds.size.width,containerBounds.size.height-40.0f); presentedViewFrame.origin = CGPointMake(0.0f,40.0f); return presentedViewFrame; }
关键是您希望将presentationViewController的框架从屏幕顶部偏移,以便您可以实现与另一个视图控件重叠的外观(无需完全覆盖presentationViewController).
接下来,在AAPLOverlayTransitioner.m中找到animateTransition:方法,并用下面的代码替换代码.您可能希望根据自己的代码调整几件事情,但总体而言,这似乎是解决方案:
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext { UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; UIView *fromView = [fromVC view]; UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; UIView *toView = [toVC view]; UIView *containerView = [transitionContext containerView]; BOOL isPresentation = [self isPresentation]; if(isPresentation) { [containerView addSubview:toView]; } UIViewController *bottomVC = isPresentation? fromVC : toVC; UIView *bottomPresentingView = [bottomVC view]; UIViewController *topVC = isPresentation? toVC : fromVC; UIView *topPresentedView = [topVC view]; CGRect topPresentedFrame = [transitionContext finalFrameForViewController:topVC]; CGRect topDismissedFrame = topPresentedFrame; topDismissedFrame.origin.y += topDismissedFrame.size.height; CGRect topInitialFrame = isPresentation ? topDismissedFrame : topPresentedFrame; CGRect topFinalFrame = isPresentation ? topPresentedFrame : topDismissedFrame; [topPresentedView setFrame:topInitialFrame]; [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:300.0 initialSpringVelocity:5.0 options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState animations:^{ [topPresentedView setFrame:topFinalFrame]; CGFloat scalingFactor = [self isPresentation] ? 0.92f : 1.0f; //this is the magic right here bottomPresentingView.transform = CGAffineTransformScale(CGAffineTransformIdentity,scalingFactor,scalingFactor); } completion:^(BOOL finished){ if(![self isPresentation]) { [fromView removeFromSuperview]; } [transitionContext completeTransition:YES]; }]; }
在这个时候,我没有在iOS 8之前提供操作系统版本的解决方案,但是如果你想到一个,请随时添加一个答案.谢谢.
更新:
看起来好像上面的链接不再有效.同样的项目可以在这里找到:https://developer.apple.com/library/ios/samplecode/LookInside/LookInsidePresentationControllersAdaptivityandCustomAnimatorObjects.zip