导航UIViewControllers与iOS的手势(Navigating UIViewContro

2019-08-18 05:29发布

我有嵌入在一个UINavigationController几个视图控制器(一些模态,一些推),并使用滑动手势作为这样通过它们正在导航:

// Gesture recognizers
UISwipeGestureRecognizer *downGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(dismissButton)];
downGesture.direction = UISwipeGestureRecognizerDirectionDown;
[downGesture setCancelsTouchesInView:NO];
[self.view addGestureRecognizer:downGesture];

这工作得很好,但我希望用户能够物理拖动模态呈现视图控制器,例如,向下和关闭屏幕,而不只是轻弹和动画做休息,或在屏幕右侧拖动和捕捉到以前的观点,而不是叩击背部按钮。

我已经试过这使用移动手势的观点,但当然以前的视图控制器是不是背后可见,它必须执行。 这是如何影响正常实现? 随着视图控制器遏制? 如果这样会怎样的工作力推几个视图控制器堆栈是什么时候? 导航我谈论的类型的例子可以在凸版应用程序被发现。

谢谢。

Answer 1:

是的,自定义的容器视图是要走的路(iOS 5及以上)。 你基本上写自己的自定义容器,使用内置childViewControllers属性来跟踪所有子视图控制器的。 您可能希望自己的财产,说currentChildIndex ,跟踪你目前使用的子控制器:

@property (nonatomic) NSInteger currentChildIndex;

你的父母也许控制器应该具有非刷卡与导航相关,比如一些push和pop方法:

- (void)pushChildViewController:(UIViewController *)newChildController
{
    // remove any other children that we've popped off, but are still lingering about

    for (NSInteger index = [self.childViewControllers count] - 1; index > self.currentChildIndex; index--)
    {
        UIViewController *childController = self.childViewControllers[index];
        [childController willMoveToParentViewController:nil];
        [childController.view removeFromSuperview];
        [childController removeFromParentViewController];
    }

    // get reference to the current child controller

    UIViewController *currentChildController = self.childViewControllers[self.currentChildIndex];

    // set new child to be off to the right

    CGRect frame = self.containerView.bounds;
    frame.origin.x += frame.size.width;
    newChildController.view.frame = frame;

    // add the new child

    [self addChildViewController:newChildController];
    [self.containerView addSubview:newChildController.view];
    [newChildController didMoveToParentViewController:self];

    [UIView animateWithDuration:0.5
                     animations:^{
                         CGRect frame = self.containerView.bounds;
                         newChildController.view.frame = frame;
                         frame.origin.x -= frame.size.width;
                         currentChildController.view.frame = frame;
                     }];

    self.currentChildIndex++;
}

- (void)popChildViewController
{
    if (self.currentChildIndex == 0)
        return;

    UIViewController *currentChildController = self.childViewControllers[self.currentChildIndex];
    self.currentChildIndex--;
    UIViewController *previousChildController = self.childViewControllers[self.currentChildIndex];

    CGRect onScreenFrame = self.containerView.bounds;

    CGRect offToTheRightFrame = self.containerView.bounds;
    offToTheRightFrame.origin.x += offToTheRightFrame.size.width;

    [UIView animateWithDuration:0.5
                     animations:^{
                         currentChildController.view.frame = offToTheRightFrame;
                         previousChildController.view.frame = onScreenFrame;
                     }];
}

就个人而言,我有这两种方法定义的协议,并确保我的父母控制器配置为符合该协议:

@protocol ParentControllerDelegate <NSObject>

- (void)pushChildViewController:(UIViewController *)newChildController;
- (void)popChildViewController;

@end

@interface ParentViewController : UIViewController <ParentControllerDelegate>
...
@end

然后,当孩子想推一个新的子上,它可以像这样:

ChildViewController *controller = ... // instantiate and configure your next controller however you want to do that

id<ParentControllerDelegate> parent = (id)self.parentViewController;
NSAssert([parent conformsToProtocol:@protocol(ParentControllerDelegate)], @"Parent must conform to ParentControllerDelegate");

[parent pushChildViewController:controller];

当孩子想跳出自行关闭,它可以像这样:

id<ParentControllerDelegate> parent = (id)self.parentViewController;
NSAssert([parent conformsToProtocol:@protocol(ParentControllerDelegate)], @"Parent must conform to ParentControllerDelegate");

[parent popChildViewController];

然后父视图控制器具有平移手势设置,处理用户平移从一个孩子到另一个:

- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
    static UIView *currentView;
    static UIView *previousView;
    static UIView *nextView;

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        // identify previous view (if any)

        if (self.currentChildIndex > 0)
        {
            UIViewController *previous = self.childViewControllers[self.currentChildIndex - 1];
            previousView = previous.view;
        }
        else
        {
            previousView = nil;
        }

        // identify next view (if any)

        if (self.currentChildIndex < ([self.childViewControllers count] - 1))
        {
            UIViewController *next = self.childViewControllers[self.currentChildIndex + 1];
            nextView = next.view;
        }
        else
        {
            nextView = nil;
        }

        // identify current view

        UIViewController *current = self.childViewControllers[self.currentChildIndex];
        currentView = current.view;
    }

    // if we're in the middle of a pan, let's adjust the center of the views accordingly

    CGPoint translation = [gesture translationInView:gesture.view.superview];

    previousView.transform = CGAffineTransformMakeTranslation(translation.x, 0.0);
    currentView.transform = CGAffineTransformMakeTranslation(translation.x, 0.0);
    nextView.transform = CGAffineTransformMakeTranslation(translation.x, 0.0);

    // if we're all done, let's animate the completion (or if we didn't move far enough,
    // the reversal) of the pan gesture

    if (gesture.state == UIGestureRecognizerStateEnded ||
        gesture.state == UIGestureRecognizerStateCancelled ||
        gesture.state == UIGestureRecognizerStateFailed)
    {

        CGPoint center = currentView.center;
        CGPoint currentCenter = CGPointMake(center.x + translation.x, center.y);
        CGPoint offRight = CGPointMake(center.x + currentView.frame.size.width, center.y);
        CGPoint offLeft = CGPointMake(center.x - currentView.frame.size.width, center.y);

        CGPoint velocity = [gesture velocityInView:gesture.view.superview];

        if ((translation.x + velocity.x * 0.5) < (-self.containerView.frame.size.width / 2.0) && nextView)
        {
            // if we finished pan to left, reset transforms

            previousView.transform = CGAffineTransformIdentity;
            currentView.transform = CGAffineTransformIdentity;
            nextView.transform = CGAffineTransformIdentity;

            // set the starting point of the animation to pick up from where
            // we had previously transformed the views

            CGPoint nextCenter = CGPointMake(nextView.center.x + translation.x, nextView.center.y);
            currentView.center = currentCenter;
            nextView.center = nextCenter;

            // and animate the moving of the views to their final resting points,
            // adjusting the currentChildIndex appropriately

            [UIView animateWithDuration:0.25
                                  delay:0.0
                                options:UIViewAnimationOptionCurveEaseOut
                             animations:^{
                                 currentView.center = offLeft;
                                 nextView.center = center;
                                 self.currentChildIndex++;
                             }
                             completion:NULL];
        }
        else if ((translation.x + velocity.x * 0.5) > (self.containerView.frame.size.width / 2.0) && previousView)
        {
            // if we finished pan to right, reset transforms

            previousView.transform = CGAffineTransformIdentity;
            currentView.transform = CGAffineTransformIdentity;
            nextView.transform = CGAffineTransformIdentity;

            // set the starting point of the animation to pick up from where
            // we had previously transformed the views

            CGPoint previousCenter = CGPointMake(previousView.center.x + translation.x, previousView.center.y);
            currentView.center = currentCenter;
            previousView.center = previousCenter;

            // and animate the moving of the views to their final resting points,
            // adjusting the currentChildIndex appropriately

            [UIView animateWithDuration:0.25
                                  delay:0.0
                                options:UIViewAnimationOptionCurveEaseOut
                             animations:^{
                                 currentView.center = offRight;
                                 previousView.center = center;
                                 self.currentChildIndex--;
                             }
                             completion:NULL];
        }
        else
        {
            [UIView animateWithDuration:0.25
                                  delay:0.0
                                options:UIViewAnimationOptionCurveEaseInOut
                             animations:^{
                                 previousView.transform = CGAffineTransformIdentity;
                                 currentView.transform = CGAffineTransformIdentity;
                                 nextView.transform = CGAffineTransformIdentity;
                             }
                             completion:NULL];
        }
    }
}

它看起来像你做上下平移,而不是左右摇动,我上面使用,但希望你得到的基本思路。


顺便说一句,在iOS 6中,用户界面你问(使用手势视图之间的滑动),也许可以更有效地利用内置容器控制器,来完成UIPageViewController 。 只需使用的过渡风格UIPageViewControllerTransitionStyleScroll和的导航定位UIPageViewControllerNavigationOrientationHorizontal 。 不幸的是,iOS 5中仅允许页面卷曲的转变,而苹果只介绍您在iOS 6中要滚动的转变,但如果这就是你所需要的, UIPageViewController得到比我在前面已经制定了(你不更有效地完成工作“T做任何定制容器调用,没有文字手势识别的,等等)。

例如,您可以拖动“页面视图控制器”到你的故事板,创建一个UIPageViewController子类,然后在viewDidLoad ,你需要配置的第一页:

UIViewController *firstPage = [self.storyboard instantiateViewControllerWithIdentifier:@"1"]; // use whatever storyboard id your left page uses
self.viewControllerStack = [NSMutableArray arrayWithObject:firstPage];

[self setViewControllers:@[firstPage]
               direction:UIPageViewControllerNavigationDirectionForward
                animated:NO
              completion:NULL];

self.dataSource = self;

然后,你需要定义以下UIPageViewControllerDataSource方法:

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    if ([viewController isKindOfClass:[LeftViewController class]])
        return [self.storyboard instantiateViewControllerWithIdentifier:@"2"];

    return nil;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    if ([viewController isKindOfClass:[RightViewController class]])
        return [self.storyboard instantiateViewControllerWithIdentifier:@"1"];

    return nil;
}

您的实现将改变(至少是不同的类名和不同的故事板标识;我也让页面视图控制器实例在下页的控制器,当用户需要它因为我没有任何保留强引用他们,当我做过渡到其他页面the'll发布...你可以只交替包括启动,然后这些实例beforeafter套路显然不能实例,而是看看他们在一个数组),但希望你的想法。

但关键的问题是,我没有任何动作代码,没有自定义容器视图控制器代码等简单得多。



文章来源: Navigating UIViewControllers with gestures in iOS