我有两个视图控制器,在其中我使用常规的Show Segue(从右到左)沿一个方向前进,并使用定制的Segue(从左到右)沿另一个方向前进。我不认为我应该放松一下,因为任何一个VC都不从属于其他VC,并且使用这些设置意味着一个导航控制器可以管理事情。

在两个VC的左上角,我都有一个包含个人资料照片的公共BarButton。

使用常规的从右到左Segue时,条形按钮上的个人资料照片保持不变。当屏幕的其余部分向内移动时,这看起来很棒,但是两者的共同元素是个人资料照片保持在原位。

但是,从另一个方向(从左到右),使用自定义序列,整个VC屏幕(包括导航栏)就会突然进入,您基本上会看到个人资料照片从左边缘进入,然后停留在正常的左栏按钮位置,刚才的照片是同一张。看起来不好

有什么方法可以在自定义segue期间强制导航栏中的公共元素保持在原位,以更好地模仿系统show segue的行为?

在此先感谢您的任何建议。

这是我的自定义序列代码:

#import "customSegue.h"
#import "QuartzCore/QuartzCore.h"

@implementation customSegue

-(void)perform {

    UIViewController *destinationController = (UIViewController*)[self destinationViewController];

    UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
    CGFloat animationDuration = .40;
    transition.duration = animationDuration;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
    transition.type = kCATransitionMoveIn;
    transition.subtype = kCATransitionFromLeft;

    [sourceViewController.navigationController.view.layer addAnimation:transition
                                                                forKey:kCATransition];

    UIColor *previousWindowBackgroundColor = sourceViewController.view.window.backgroundColor;

    sourceViewController.view.window.backgroundColor = destinationController.view.backgroundColor;


    [sourceViewController.navigationController pushViewController:destinationController animated:NO];
    // switch the window color back after the transition duration from above
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(animationDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // make sure we still have a handle on the destination controller
        if (destinationController) {
            destinationController.view.window.backgroundColor = previousWindowBackgroundColor;
        }
    });

}

@end

最佳答案

再次问好@ user6631314

我认为您不会通过使用CATransition进行应用并将其附加到navigationController视图的图层上来获得所需的内容。

相反,我建议将presentingViewController委托为UINavigationController的代表,并为LTR推送滚动您自己的逻辑(导航栏将更加平滑,并且您不必再担心过渡期间的黑栏,因为我先前的响应帮助您解决/解决方法)。

因此,您需要将呈现视图控制器(或某些协调器视图控制器)设置为UINavigationControllerDelegate并实现此方法:

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                  animationControllerForOperation:(UINavigationControllerOperation)operation
                                               fromViewController:(UIViewController*)fromVC
                                                 toViewController:(UIViewController*)toVC


在该方法中,您将要检查操作是否为UINavigationControllerOperationPush,如果是,则返回符合UIViewControllerAnimatedTransitioning协议的NSObject的子类(否则返回nil以使所有其他导航操作成为标准操作)。在该类中,您将处理自定义导航控制器的推送动画覆盖。

LTR推送动画的基本逻辑是,您要在屏幕左侧向左启动toView,然后对其进行动画处理,以使其在动画持续时间(从代码开始为0.4)之后完全在屏幕上显示-因此开始x位置偏移设置为视图宽度的负值(因此完全不在屏幕上),然后在动画过程中将x位置设置为0(或者您可以+ =视图的宽度)。

这是当前自定义segue实现的外观示例(请注意导航栏也会滑过,这是您要在此处发布的问题):

ios - IOS/Objective-C:导航栏上的自定义Segue效果与Show Segue相比-LMLPHP

通过使用自定义动画控制器进行过渡:

ios - IOS/Objective-C:导航栏上的自定义Segue效果与Show Segue相比-LMLPHP

这是完整的代码:

#import "ViewController.h"
#import "LTRPushAnimator.h"

@interface ViewController () < UINavigationControllerDelegate>
@property (strong, nullable) UIView *profileView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.delegate = self;
    self.navigationItem.hidesBackButton = YES;
    self.profileView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    UIImageView *profileImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Homer.jpg"]];
    [profileImageView setFrame:CGRectMake(0, 0, 40, 40)];
    profileImageView.layer.cornerRadius = 20.0f;
    profileImageView.layer.masksToBounds = YES;
    profileImageView.clipsToBounds = YES;
    profileImageView.layer.borderColor = [UIColor blackColor].CGColor;
    profileImageView.layer.borderWidth = 1.0f;
    [self.profileView addSubview:profileImageView];
    UIBarButtonItem *lbi = [[UIBarButtonItem alloc] initWithCustomView:self.profileView];
    self.navigationItem.leftBarButtonItem = lbi;
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (IBAction)pushFakeViewController:(id)sender {
    UIViewController *fakeViewController = [[UIViewController alloc] init];
    fakeViewController.view.backgroundColor = [UIColor redColor];
    UIBarButtonItem *lbi = [[UIBarButtonItem alloc] initWithCustomView:self.profileView];
    fakeViewController.navigationItem.leftBarButtonItem = lbi;
    fakeViewController.navigationItem.hidesBackButton = YES;
    [self.navigationController pushViewController:fakeViewController animated:YES];
    // this is just in here to pop back to the root view controller since we removed the back button, it can be removed obviously
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.navigationController popViewControllerAnimated:YES];
    });
}

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                  animationControllerForOperation:(UINavigationControllerOperation)operation
                                               fromViewController:(UIViewController*)fromVC
                                                 toViewController:(UIViewController*)toVC
{
    if (operation == UINavigationControllerOperationPush) {
        return [[LTRPushAnimator alloc] init];
    }
    // otherwise standard animation
    return nil;
}

@end


LTRPushAnimator.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface LTRPushAnimator : NSObject <UIViewControllerAnimatedTransitioning>

@end

NS_ASSUME_NONNULL_END


LTRPushAnimator.m

#import "LTRPushAnimator.h"

@implementation LTRPushAnimator

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
    return 0.4;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    [self _animatePushByFrameWithContext:transitionContext];
}

- (void)_animatePushByFrameWithContext:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *toViewController   = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    CGRect toVCFrame = toViewController.view.frame;
    CGFloat viewWidth = toVCFrame.size.width;
    toVCFrame.origin.x -= viewWidth;
    [toViewController.view setFrame:toVCFrame];
    [[transitionContext containerView] addSubview:toViewController.view];
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        CGRect finalVCFrame = toViewController.view.frame;
        finalVCFrame.origin.x = 0;
        [toViewController.view setFrame:finalVCFrame];
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
}

@end

10-05 20:22
查看更多