gpt4 book ai didi

IOS/objective-C : Custom Segue Effect on Navigation Bar Compared with Show Segue

转载 作者:行者123 更新时间:2023-11-28 23:48:11 24 4
gpt4 key购买 nike

我有两个 View Controller ,我使用常规显示转场(从右到左)朝一个方向前进,使用自定义转场(从左到右)朝另一个方向前进。我不认为我应该放松,因为两个 VC 都不从属于另一个 VC,并且使用这些 Segues 意味着一个导航 Controller 可以管理事物。

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

当使用常规的从右到左 Segue 时,栏按钮上的个人资料照片保持不变。随着屏幕的其余部分移入,这看起来很棒,但两者共有的元素,个人资料照片保留在原位。

然而,在另一个方向(从左到右),使用自定义转场,包括导航栏在内的整个 VC 屏幕都会猛冲过来,您基本上会看到个人资料照片从左边缘进入,然后停留在正常的左栏中同一张照片之前的按钮位置。这看起来很糟糕。

有没有办法强制导航栏中的通用元素在自定义转场期间保持在原位,以更好地模仿系统显示转场的行为?

提前感谢您的任何建议。

这是我的自定义转场代码:

#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 View 的层来获得您想要的东西。

相反,我建议让 presentingViewController 成为 UINavigationController 的委托(delegate),并为 LTR 推送滚动你自己的逻辑(导航栏会更顺畅,你不应该再担心过渡期间的黑条我之前的回复帮助您解决/解决了问题)。

为此,您需要将呈现 View Controller (或某些协调器 View Controller )设置为 UINavigationControllerDelegate 并实现此方法:

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

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

LTR 推送动画的基本逻辑是,您希望从屏幕左侧开始 toView,然后对其进行动画处理,使其在动画持续时间(代码中的 0.4)后完全显示在屏幕上——因此启动x 位置偏移到 View 宽度的负值(因此它完全在屏幕外),然后在动画期间将 x 位置设置为 0(或者您可以只 += View 的宽度)。

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

enter image description here

以及使用自定义动画 Controller 的过渡:

enter image description here

完整代码如下:

#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

关于IOS/objective-C : Custom Segue Effect on Navigation Bar Compared with Show Segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52266511/

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