2

我正在尝试在我的视图控制器上实现 SwipeGesture。

我目前面临的问题是我无法确定当前显示的子视图控制器是什么。

swipeGesture 被添加到容器视图控制器中。然后必须确定当前显示的VC是什么父子关系,然后向右或向左移动。

4

2 回答 2

5

如果您正在谈论自定义容器视图控制器,那么容器控制器的工作就是跟踪这一点。因此,您可能拥有自己的@property设备来跟踪您正在使用的设备,并根据您transitionFromController的滑动进行调整。因此,您可能有一个数字属性,当您向右移动时会增加,向左移动时会减少。

一般来说(如果你只是想跟踪你传递给哪个“来自”控制器transitionFromViewController),有两种方法。View Controller Programming Guide的添加子控制器中的清单 14-3 暗示了一种方法。

在这种情况下,在viewDidLoad您执行addChildViewController第一个视图控制器时。不过,在这种情况下,此时您不需要加载其他子视图控制器addChildViewController,而是让执行转换的方法来处理它(如清单 14-3 所示)。

如果你这样做,你可以抓住[self.childViewControllers lastObject],这将是你的“当前”子控制器。所以,这可能看起来像:

@interface ViewController ()

@property (nonatomic) NSInteger currentIndex;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIViewController *initialController = ... // set your initial controller

    [self addChildViewController:initialController];
    initialController.view.frame = self.containerView.bounds;
    [self.containerView addSubview:initialController.view];
    [initialController didMoveToParentViewController:self];

    UISwipeGestureRecognizer *gesture;

    gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    gesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self.containerView addGestureRecognizer:gesture];

    gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    gesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.containerView addGestureRecognizer:gesture];

}

- (void) handleSwipe:(UISwipeGestureRecognizer *)gesture
{
    if (gesture.direction == UISwipeGestureRecognizerDirectionLeft && self.currentIndex < (kMaxIndex - 1))
    {
        self.currentIndex++;

        UIViewController *newController = ... // set your new controller
        UIViewController *oldController = [self.childViewControllers lastObject];

        [self cycleFromViewController:oldController
                     toViewController:newController
                            direction:gesture.direction];

    }
    else if (gesture.direction == UISwipeGestureRecognizerDirectionRight && self.currentIndex > 0)
    {
        self.currentIndex--;

        UIViewController *newController = ... // set your new controller
        UIViewController *oldController = [self.childViewControllers lastObject];

        [self cycleFromViewController:oldController
                     toViewController:newController
                            direction:gesture.direction];
    }
}

- (void) cycleFromViewController:(UIViewController*) oldController
                toViewController:(UIViewController*) newController
                       direction:(UISwipeGestureRecognizerDirection)direction
{
    [oldController willMoveToParentViewController:nil];
    [self addChildViewController:newController];

    newController.view.frame = oldController.view.frame;

    UIViewAnimationOptions options;

    if (direction == UISwipeGestureRecognizerDirectionRight)
        options = UIViewAnimationOptionTransitionFlipFromLeft;
    else if (direction == UISwipeGestureRecognizerDirectionLeft)
        options = UIViewAnimationOptionTransitionFlipFromRight;

    [self transitionFromViewController:oldController
                      toViewController:newController
                              duration:0.33
                               options:options
                            animations:^{
                                [oldController removeFromParentViewController];
                            }
                            completion:^(BOOL finished) {
                                [newController didMoveToParentViewController:self];
                            }];
}

我见过一些人做的另一个模型是通过addChildViewControllerin加载所有潜在的子控制器viewDidLoad。我个人不喜欢这种方法,但我知道人们这样做并且效果很好。

但是如果你这样做,你就不能再依赖于childViewControllers知道哪个控制器是当前的控制器了。在这种情况下,您必须为currentChildController. 所以它可能看起来像:

@interface ViewController ()

@property (nonatomic, strong) UIViewController *currentChildController;
@property (nonatomic) NSInteger currentIndex;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"ChildOne"]];
    [self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"ChildTwo"]];
    [self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"ChildThree"]];

    self.currentChildController = self.childViewControllers[0];

    self.currentChildController.view.frame = self.containerView.bounds;
    [self.containerView addSubview:self.currentChildController.view];

    for (UIViewController *controller in self.childViewControllers)
        [controller didMoveToParentViewController:self];

    UISwipeGestureRecognizer *gesture;

    gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    gesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self.containerView addGestureRecognizer:gesture];

    gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    gesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.containerView addGestureRecognizer:gesture];
}

- (void) cycleFromViewController:(UIViewController*) oldController
                toViewController:(UIViewController*) newController
                       direction:(UISwipeGestureRecognizerDirection) direction
{
    self.currentChildController = newController;

    newController.view.frame = oldController.view.frame;

    UIViewAnimationOptions options;

    if (direction == UISwipeGestureRecognizerDirectionRight)
        options = UIViewAnimationOptionTransitionFlipFromLeft;
    else if (direction == UISwipeGestureRecognizerDirectionLeft)
        options = UIViewAnimationOptionTransitionFlipFromRight;

    [self transitionFromViewController:oldController
                      toViewController:newController
                              duration:0.33
                               options:options
                            animations:^{
                            }
                            completion:nil];
}

- (void) handleSwipe:(UISwipeGestureRecognizer *)gesture
{
    if (gesture.direction == UISwipeGestureRecognizerDirectionLeft && self.currentIndex < (kMaxIndex - 1))
    {
        self.currentIndex++;

        UIViewController *oldController = self.currentChildController;
        UIViewController *newController = self.childViewControllers[self.currentIndex];

        [self cycleFromViewController:oldController
                     toViewController:newController
                            direction:gesture.direction];

    }
    else if (gesture.direction == UISwipeGestureRecognizerDirectionRight && self.currentIndex > 0)
    {
        self.currentIndex--;

        UIViewController *oldController = self.currentChildController;
        UIViewController *newController = self.childViewControllers[self.currentIndex];

        [self cycleFromViewController:oldController
                     toViewController:newController
                            direction:gesture.direction];
    }
}

这是两种合乎逻辑的方法。

顺便说一句,第一种方法并不排除拥有自己的类属性来知道你在哪个控制器上,有时这很有用(例如,如果你使用的动画类型取决于你是否要去“正确的”或“左”),但除非是这种情况,否则您只需查看[self.childViewControllers lastObject].

于 2013-01-19T05:48:35.383 回答
0

在我正在处理的项目中,我有许多视图控制器,每个视图控制器都将自己的导航控制器添加到父视图控制器中。我知道最初的条目孩子,但我执行以下操作来查找当前显示的孩子的孩子:

[myKnownViewController.childViewControllers objectAtIndex:0].navigationController.topViewController
于 2013-02-01T00:01:16.447 回答