- Developing with Navigation Controllers and Split Views
- Recipe: Building a Simple Two-Item Menu
- Recipe: Adding a Segmented Control
- Recipe: Navigating Between View Controllers
- Recipe: Presenting a Custom Modal Information View
- Recipe: Page View Controllers
- Recipe: Scrubbing Pages in a Page View Controller
- Recipe: Tab Bars
- Recipe: Remembering Tab State
- Recipe: Building Split View Controllers
- Recipe: Creating Universal Split View - Navigation Apps
- Recipe: Custom Containers and Segues
- One More Thing: Interface Builder and Tab Bar Controllers
- Summary
Recipe: Scrubbing Pages in a Page View Controller
Manually flipping from page to page quickly becomes tedious, especially when working with a presentation of dozens or hundreds of virtual pages. To address this, you can add a slider to your books. Recipe 5-6 creates a slider that appears when the background is tapped and that fades away after a few seconds if not used.
A custom tap gesture recognizer starts the timer, which is reset whenever the user interacts with the slider. Once the timer fires, the slider overview animates away and the user is left with the full screen page presentation. This approach, using a tap-based overlay, is common to many of Apple's own applications such as the Photos app.
Recipe 5-6 Adding an auto-hiding slider to a page view controller
// Slider callback resets the timer, moves to the new page - (void) moveToPage: (UISlider *) theSlider { [hiderTimer invalidate]; hiderTimer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(hideSlider:) userInfo:nil repeats:NO]; [bookController moveToPage:(int) theSlider.value]; } // BookController Delegate method allows slider value update - (void) bookControllerDidTurnToPage: (NSNumber *) pageNumber { pageSlider.value = pageNumber.intValue; } // Hide the slider after the timer fires - (void) hideSlider: (NSTimer *) aTimer { [UIView animateWithDuration:0.3f animations:^(void){ pageSlider.alpha = 0.0f;}]; [hiderTimer invalidate]; hiderTimer = nil; } // Present the slider when tapped - (void) handleTap: (UIGestureRecognizer *) recognizer { [UIView animateWithDuration:0.3f animations:^(void){ pageSlider.alpha = 1.0f;}]; [hiderTimer invalidate]; hiderTimer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(hideSlider:) userInfo:nil repeats:NO]; } - (void) viewDidLoad { [super viewDidLoad]; // Add page view controller as a child view, and do housekeeping [self addChildViewController:bookController]; [self.view addSubview:bookController.view]; [bookController didMoveToParentViewController:self]; [self.view addSubview:pageSlider]; } - (void) loadView { [super loadView]; CGRect appRect = [[UIScreen mainScreen] applicationFrame]; self.view = [[UIView alloc] initWithFrame: appRect]; self.view.backgroundColor = [UIColor whiteColor]; self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; // Establish the page view controller bookController = [BookController bookWithDelegate:self]; bookController.view.frame = (CGRect){.size = appRect.size}; // Set the tap to reveal the hidden slider UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; [self.view addGestureRecognizer:tap]; }