代码之家  ›  专栏  ›  技术社区  ›  user6631314

IOS/OBJECTIVE-C:导航栏上的自定义segue效果与显示segue相比

  •  0
  • user6631314  · 技术社区  · 6 年前

    我有两个视图控制器,我使用一个常规的Show Segue(从右到左)向一个方向移动,使用一个自定义的Segue(从左到右)向另一个方向移动。我不认为我应该做一个放松,因为两个VC都不是从属于另一个,使用这些段意味着一个导航控制器管理事情。

    在这两个VCS的左上角,我都有一个包含简介照片的公共按钮。

    当使用常规的从右到左的segue时,条形按钮上的配置文件照片保持不变。这看起来很好,因为其他的屏幕移动,但共同的元素,这两个配置文件照片留在原地。

    然而,在另一个方向(从左到右),使用自定义segue,包括导航栏在内的整个vc屏幕会突然进入,您基本上可以看到轮廓照片从左边缘进入,然后停留在正常的左栏按钮位置,在此位置之前,相同的照片会出现。这看起来很糟糕。

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

    提前谢谢你的建议。

    这是我的自定义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
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   R4N    6 年前

    再次您好@user6631314

    我不认为通过使用catTransition并将其附加到NavigationController视图的层,您将获得想要的结果。

    相反,我建议让PresentingView控制器成为uinavigationController的代理,并滚动您自己的LTR推送逻辑(导航栏会更平滑,在我以前的响应帮助您解决/解决问题的过渡期间,您不必再担心黑条)。

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

    -(id<uiviewControllerAnimatedTransitioning>)导航控制器:(uinavigationController*)导航控制器
    操作的动画控制器:(uinavigationcontrollerOperation)操作
    fromviewcontroller:(uiviewcontroller*)fromvc
    toviewcontroller:(uiviewcontroller*)tovc
    < /代码> 
    
    

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

    LTR Push动画的基本逻辑是,您要从屏幕左侧开始视图,然后将其动画化,使其在动画持续时间(代码中的0.4)之后完全在屏幕上,因此,在动画播放期间,将X位置偏移量设置为视图宽度的负值(因此它完全在屏幕外),然后在动画播放期间操作将X位置设置为0(或者您可以只加上=视图的宽度)。

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

    以及使用自定义动画控制器进行的转换:

    以下是完整的代码:

    import“viewcontroller.h”
    #导入“ltrpush animator.h”
    
    @接口视图控制器()<uinavigationControllerDelegate>
    @属性(强,可以为空)uiview*profileview;
    @结束
    
    @实现视图控制器
    
    -(空)视图加载{
    [超级视图加载];
    self.navigationcontroller.delegate=self;
    self.navigationitem.hidesbackbutton=是;
    self.profileview=[[uiview alloc]initWithFrame:cDirectMake(0,0,40,40)】;
    uiImageView*profileImageView=[[uiImageView alloc]initWithImage:[uiImageImageNamed:@“homer.jpg”];
    [ProfileImageView设置帧:cDirectmake(0,0,40,40)]
    profileimageview.layer.cornerradius=20.0f;
    profileImageView.layer.maskstobounds=是;
    profileImageView.clipsToBounds=是;
    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;
    //在加载视图后执行任何其他设置,通常是从NIB加载。
    }
    
    -(void)视图显示:(bool)动画{
    [超级视图显示:动画];
    }
    
    -(IBaction)PushFakeViewController:(ID)发送方{
    uiviewController*fakeviewController=[[uiviewController alloc]init];
    fakeviewcontroller.view.backgroundcolor=[uicolor redcolor];
    uibarbuttonitem*lbi=[[uibarbuttonitem alloc]initWithCustomView:self.profileView];
    fakeViewController.NavigationItem.LeftBarButtonItem=lbi;
    fakeviewcontroller.navigationitem.hidesbackbutton=是;
    [self.navigationcontroller pushviewcontroller:fakeviewcontroller animated:yes];
    //这只是在这里弹出到根视图控制器,因为我们删除了back按钮,它显然可以被删除。
    调度后(调度时间(立即调度时间)(int64 t)(1.0f*nsec_/sec)),调度获取主_队列(),^{
    [self.navigationcontroller popviewcontrolleranimated:是];
    (});
    }
    
    -(ID<uiviewControllerAnimatedTransitioning>)导航控制器:(uinavigationController*)导航控制器
    操作的动画控制器:(uinavigationcontrollerOperation)操作
    fromviewcontroller:(uiviewcontroller*)fromvc
    toviewcontroller:(uiviewcontroller*)tovc
    {
    if(operation==uinavigationControllerOperationPush){
    return[[ltrpushanimator alloc]init];
    }
    //否则为标准动画
    返回零;
    }
    
    @结束
    < /代码> 
    
    

    ltrpush动画师.h

    >代码>输入/基础;基金会/基金会。 #导入<uikit/uikit.h> ns_假设\u非空\u开始 @界面ltrpushAnimator:nsObject<uiviewControllerAnimatedTransitioning> @结束 ns_假设\u非空\u结束 < /代码>

    ltrpush动画师.m

    import“ltrpushanimator.h”
    
    @实现LTRPushAnimator
    
    -(nsTimeInterval)转换持续时间:(id<uiviewControllerContextTransitioning>)TransitionContext
    {
    返回0.4;
    }
    
    -(void)动画敏感度:(id<uiviewControllerContextTransitioning>)TransitionContext
    {
    [自我动画按框架与上下文:transitionContext];
    }
    
    -(void)_animatepushbyframewithcontext:(id<uiviewcontrollercontexttransitioning>)transitiononContext{
    uiviewController*toviewController=[TransitionContext ViewControllerForkey:uiTransitionContextToviewControllerKey];
    cgrct tovcframe=toviewcontroller.view.frame;
    cgfloat viewwidth=tovcframe.size.width;
    tovcframe.origin.x-=视图宽度;
    [toviewcontroller.view setframe:tovcframe];
    [[TransitionContext ContainerView]AddSubView:ToViewController.View];
    [uiview animatewithduration:[自我过渡:过渡上下文]动画:.^{
    cgrect finalvcframe=toviewcontroller.view.frame;
    finalvcframe.origin.x=0;
    [toviewcontroller.view setframe:finalvcframe];
    }完成:^(bool finished){
    [过渡上下文复杂敏感:![TransitionContext Transition已取消]];
    };
    }
    
    @结束
    < /代码> 
    滚轴视图的图层。

    相反,我建议将PresentingView控制器作为uinavigationController的代理,并滚动您自己的LTR推送逻辑(导航栏将更平滑,在我以前的响应帮助您解决/解决问题的过渡期间,您不必再担心黑条)。

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

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

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

    LTR Push动画的基本逻辑是,您希望从屏幕左侧开始视图,然后将其动画化,使其在动画持续时间(代码中的0.4)之后完全在屏幕上,因此,在动画期间,将X位置偏移量设置为视图宽度的负值(因此它完全在屏幕外)。n将x的位置设置为0(或者您可以只设置+=视图的宽度)。

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

    enter image description here

    以及使用自定义动画控制器进行的转换:

    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
    

    ltrpush动画师.h

    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface LTRPushAnimator : NSObject <UIViewControllerAnimatedTransitioning>
    
    @end
    
    NS_ASSUME_NONNULL_END
    

    ltrpush动画师.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