当前的ChildView控制器

jac*_*rge 2 iphone xcode objective-c ios

我正在尝试在我的viewcontroller上实现SwipeGesture.

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

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

Rob*_*Rob 5

如果您正在谈论自定义容器视图控制器,那么容器控制器的工作就是跟踪它.因此,您可以拥有自己的工具@property来跟踪您所在的工作,并根据transitionFromController滑动进行调整.因此,您可能有一个数字属性,当您向右移动时,您会递增,而当您向左移动时,您会递减.

一般来说(如果你只是想跟踪你传递给哪个"来自"控制器transitionFromViewController),有两种方法.清单14-3在添加View Controller编程指南的子控制器中暗示了一种方法.

在这种情况下,在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];
                            }];
}
Run Code Online (Sandbox Code Playgroud)

我见过的另一个模型是通过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];
    }
}
Run Code Online (Sandbox Code Playgroud)

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

顺便说一句,第一种方法并不排除让你自己的类属性来知道你所使用的控制器,有时这是有用的(例如,如果你使用的动画类型取决于你是否要去"正确" "或者说"左边",但除非是这样,否则你只需要看就可以逃脱[self.childViewControllers lastObject].