ios - 我将如何开始复制 Apple 在 iOS 7 中作为默认设置使用的自定义 View Controller 转换?

标签 ios objective-c ios7 uiview uiviewcontroller

使用 iOS 7 的自定义 View Controller 转换,我想在 iOS 7 中实现类似于 Apple 默认 View Controller 转换的视觉效果。

(您可以通过从左向右滑动 View Controller 来将 View Controller 从堆栈中弹出,顶部 View Controller 从另一个顶部滑出并带有阴影并且导航栏移动。)

enter image description here

不过,我在实现这个方面遇到了很大的困难。大多数关于自定义 View Controller 的教程都具有与默认效果截然不同的效果,以展示 API 的功能,但我想复制这个。

在我实现 <UIViewControllerAnimatedTransitioning> 的子类中我有以下交互式动画代码:

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

    [transitionContext.containerView addSubview:toViewController.view];
    [transitionContext.containerView addSubview:fromViewController.view];

    fromViewController.view.layer.shadowOffset = CGSizeMake(0.0, 0.0);
    fromViewController.view.layer.shadowColor = [UIColor blackColor].CGColor;
    fromViewController.view.layer.shadowRadius = 5.0;
    fromViewController.view.layer.shadowOpacity = 0.5;

    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        CGRect newFrame = fromViewController.view.frame;
        newFrame.origin.x = CGRectGetWidth(fromViewController.view.bounds);

        fromViewController.view.frame = newFrame;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:!transitionContext.transitionWasCancelled];
    }];
}

然而影子代码使它非常滞后(即使我使用新的 snapshot 方法)而且我完全不知道如何操作导航栏

有没有人试过做类似的事情并且能够提供示例代码?

如果您愿意,可以使用示例项目进行测试:https://dzwonsemrish7.cloudfront.net/items/43260h1u1T1u010i0V3C/DefaultVCTransition.zip

基础代码归功于 objc.io。

最佳答案

设置 shadowPath 大大提高了这个阴影的性能。

设置阴影属性后,只需将其添加到您的 animateTransition: 方法中。这避免了阴影通常会导致的昂贵的离屏渲染。

[fromViewController.view.layer setShadowPath:[[UIBezierPath bezierPathWithRect:fromViewController.view.bounds] CGPath]];

我下载了您的示例项目并执行了该操作,卡顿现在完全消失了。

有关此功能的一些信息 here .

编辑:

关于操纵导航栏动画的答案是,您似乎做不到。为此,您需要从头开始重新实现您自己的 NavigationController 类。导航栏的过渡动画由容器 View Controller (UINavigationController)在内部完成,不会出现在代码的任何地方。

关于ios - 我将如何开始复制 Apple 在 iOS 7 中作为默认设置使用的自定义 View Controller 转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22624780/

相关文章:

ios - 将 View Controller 从根推送到详细 View Controller (SplitView)

ios - 上传图片(MultipartFormData)请求objctiveC

ios - 使用 iOS ipa 预打包文件

objective-c - 如何在 iOS 中使用 AssistiveTouch UI

ios - 如何获取 youtube 帐户的用户 ID?

ios - 在后台任务中安排通知

ios - 对用于反向地理编码位置的 CLGeocoder 类方法感到困惑

iphone - 表格 View 中的图像显示在模拟器中,但未显示在我的 iPod 设备中

css - iOS 7 iPad Safari Landscape innerHeight/outerHeight 布局问题

首次启动应用程序时不加载 JavaScript