iOS open source project: DYNavigationController

8291 단어 controller
DYNavigationController is a project that implements left and right sliding navigation.
HTTPS://GitHub.com/Dayang/D Y navigation controller
 
First initialize DYNavigationController with the previous follower view

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch.
    RootViewController *rootViewController = [[[RootViewController alloc] init] autorelease];
    DYNavigationController *navigationController = [[[DYNavigationController alloc]
            initWithRootViewController:rootViewController] autorelease];

    self.window.rootViewController = navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}
This method defines the gesture operation of swiping left and right

- (void)setUpGestureRecognizers:(UIViewController *)viewController {
    // Swipe right: pop the current view and go back one level
    UISwipeGestureRecognizer *rightSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(popCurrentViewOut:)];
    rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [viewController.view addGestureRecognizer:rightSwipeGesture];
    [rightSwipeGesture release];

    // Swipe left: push a new view
    UISwipeGestureRecognizer *leftSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(pushNewViewIn:)];
    leftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [viewController.view addGestureRecognizer:leftSwipeGesture];
    [leftSwipeGesture release];
}
 
When swiping right, the current view is moved to the left of the screen and added to the navigation stack

- (void)pushViewController:(UIViewController *)viewController {
    // Place the new view to the right next to the current view
    viewController.view.frame = CGRectOffset(self.view.bounds, self.view.bounds.size.width, 0);

    // Add the new view to our view hierarchy so that it displays on screen.
    [self.view addSubview:viewController.view];

    // Start animation
    [UIView animateWithDuration:ANIMATION_DURATION delay:ANIMATION_DELAY options:UIViewAnimationCurveEaseInOut animations:^{
        [self currentViewController].view.frame = CGRectOffset(self.view.bounds, -self.view.bounds.size.width, 0);
        viewController.view.frame = self.view.bounds;
    }   completion:^(BOOL finished) {
        if (finished) {
            // Connect DYNavigationController to viewController if needed
            [self setNavigatorIfNeeded:viewController];

            // Set up gesture recognizer so that we can respond to swipes
            [self setUpGestureRecognizers:viewController];

            // Add the new controller to our viewControllerStack
            [self.viewControllerStack addObject:viewController];
        }
    }];
}
Here, the class method animateWithDuration of UIView is used to realize the jump animation.
When swiping left, the current view is removed from the navigation stack, and the view on the left is moved to the center of the screen to the right.

- (void)popViewController {
    // Sanity check - We only pop when there are at least two viewControllers in the stack,
    // otherwise there is nothing to pop
    if (self.viewControllerStack.count < 2) return;

    // Start animation
    [UIView animateWithDuration:ANIMATION_DURATION delay:ANIMATION_DELAY options:UIViewAnimationCurveEaseInOut animations:^{
        [self currentViewController].view.frame = CGRectOffset(self.view.bounds, self.view.bounds.size.width, 0);
        [self previousViewController].view.frame = self.view.bounds;
    } completion:^(BOOL finished) {
        if (finished) {
            // Tear down gesture recognizers
            [self tearDownGestureRecognizers:[self currentViewController]];
            
            // Remove current viewController.view from self.
            [[self currentViewController].view removeFromSuperview];
            
            // Remove current viewController from self.
            [[self currentViewController] removeFromParentViewController];

            // Remove current view controller from viewControllerStack
            [self.viewControllerStack removeLastObject];
        }
    }];
}
 
The specific implementation of navigation is in DYNavigationController, DYNavigationControllerDelegate holds a property of DYNavigationController, and is synthesized in RootViewController, RootViewController implements DYNavigationControllerDelegate protocol, DetailViewController also implements DYNavigationControllerDelegate protocol, so DetailViewController can directly use this DYNavigationController reference to realize navigation Function.

좋은 웹페이지 즐겨찾기