Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

Recipe: Scrubbing Pages in a Page View Controller

Manually flipping from page to page quickly becomes tedious, especially when you're 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:)
        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:)
        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:)];
    [self.view addGestureRecognizer:tap];
}
  • + Share This
  • 🔖 Save To Your Account