|
| 1 | +// |
| 2 | +// AXPagedViewController.m |
| 3 | +// ReadingContent |
| 4 | +// |
| 5 | +// Created by Doug Russell on 9/12/13. |
| 6 | +// Copyright (c) 2013 Doug Russell. All rights reserved. |
| 7 | +// |
| 8 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +// you may not use this file except in compliance with the License. |
| 10 | +// You may obtain a copy of the License at |
| 11 | +// |
| 12 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +// |
| 14 | +// Unless required by applicable law or agreed to in writing, software |
| 15 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +// See the License for the specific language governing permissions and |
| 18 | +// limitations under the License. |
| 19 | +// |
| 20 | + |
| 21 | +#import "AXPagedViewController.h" |
| 22 | +#import "AXTextViewController.h" |
| 23 | + |
| 24 | +@interface AXPagedViewController () <UIPageViewControllerDataSource, UIPageViewControllerDelegate> |
| 25 | +@property (nonatomic) UIPageViewController *pageViewController; |
| 26 | +@property (nonatomic) NSArray *viewControllers; |
| 27 | +@end |
| 28 | + |
| 29 | +@implementation AXPagedViewController |
| 30 | + |
| 31 | +#pragma mark - Segue |
| 32 | + |
| 33 | +- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender |
| 34 | +{ |
| 35 | + [super prepareForSegue:segue sender:sender]; |
| 36 | + |
| 37 | + if ([segue.identifier isEqualToString:@"PageViewControllerSegue"]) |
| 38 | + { |
| 39 | + self.pageViewController = segue.destinationViewController; |
| 40 | + |
| 41 | + UIStoryboard *storyboard = self.storyboard; |
| 42 | + // Store our view controller pages |
| 43 | + self.viewControllers = |
| 44 | + @[ |
| 45 | + [storyboard instantiateViewControllerWithIdentifier:@"ReadingContentTextViewController"], |
| 46 | + [storyboard instantiateViewControllerWithIdentifier:@"ReadingContentTextViewController"], |
| 47 | + [storyboard instantiateViewControllerWithIdentifier:@"ReadingContentTextViewController"], |
| 48 | + ]; |
| 49 | + [[self.viewControllers lastObject] setIsLastPage:YES]; |
| 50 | + |
| 51 | + self.pageViewController.dataSource = self; |
| 52 | + self.pageViewController.delegate = self; |
| 53 | + |
| 54 | + self.pageViewController.view.backgroundColor = [UIColor lightGrayColor]; |
| 55 | + |
| 56 | + [self.pageViewController setViewControllers:@[self.viewControllers[0]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL]; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +#pragma mark - UIPageViewControllerDataSource |
| 61 | + |
| 62 | +- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController |
| 63 | +{ |
| 64 | + UIViewController *viewControllerBefore = nil; |
| 65 | + NSUInteger index = [self.viewControllers indexOfObject:viewController]; |
| 66 | + if (index != NSNotFound) |
| 67 | + { |
| 68 | + index--; |
| 69 | + if (index != NSUIntegerMax) |
| 70 | + { |
| 71 | + viewControllerBefore = self.viewControllers[index]; |
| 72 | + } |
| 73 | + } |
| 74 | + return viewControllerBefore; |
| 75 | +} |
| 76 | + |
| 77 | +- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController |
| 78 | +{ |
| 79 | + UIViewController *viewControllerAfter = nil; |
| 80 | + NSUInteger index = [self.viewControllers indexOfObject:viewController]; |
| 81 | + if (index != NSNotFound) |
| 82 | + { |
| 83 | + index++; |
| 84 | + if (index != [self.viewControllers count]) |
| 85 | + { |
| 86 | + viewControllerAfter = self.viewControllers[index]; |
| 87 | + } |
| 88 | + } |
| 89 | + return viewControllerAfter; |
| 90 | +} |
| 91 | + |
| 92 | +- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController |
| 93 | +{ |
| 94 | + return (NSInteger)[self.viewControllers count]; |
| 95 | +} |
| 96 | + |
| 97 | +- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController |
| 98 | +{ |
| 99 | + if (self.viewControllers && [self.pageViewController.viewControllers count]) |
| 100 | + { |
| 101 | + NSUInteger index = [self.viewControllers indexOfObject:self.pageViewController.viewControllers[0]]; |
| 102 | + if (index != NSNotFound) |
| 103 | + { |
| 104 | + return index; |
| 105 | + } |
| 106 | + } |
| 107 | + return 0; |
| 108 | +} |
| 109 | + |
| 110 | +- (BOOL)accessibilityScroll:(UIAccessibilityScrollDirection)direction |
| 111 | +{ |
| 112 | + BOOL handled = NO; |
| 113 | + if (self.viewControllers && [self.pageViewController.viewControllers count]) |
| 114 | + { |
| 115 | + NSUInteger index = [self.viewControllers indexOfObject:self.pageViewController.viewControllers[0]]; |
| 116 | + if (index != NSNotFound) |
| 117 | + { |
| 118 | + UIPageViewControllerNavigationDirection navDirection; |
| 119 | + switch (direction) { |
| 120 | + case UIAccessibilityScrollDirectionLeft: |
| 121 | + case UIAccessibilityScrollDirectionNext: |
| 122 | + index++; |
| 123 | + if (index != [self.viewControllers count]) |
| 124 | + { |
| 125 | + handled = YES; |
| 126 | + } |
| 127 | + navDirection = UIPageViewControllerNavigationDirectionForward; |
| 128 | + break; |
| 129 | + case UIAccessibilityScrollDirectionRight: |
| 130 | + case UIAccessibilityScrollDirectionPrevious: |
| 131 | + index--; |
| 132 | + if (index != NSUIntegerMax) |
| 133 | + { |
| 134 | + handled = YES; |
| 135 | + } |
| 136 | + navDirection = UIPageViewControllerNavigationDirectionReverse; |
| 137 | + break; |
| 138 | + default: |
| 139 | + break; |
| 140 | + } |
| 141 | + if (handled) |
| 142 | + { |
| 143 | + [self.pageViewController setViewControllers:@[self.viewControllers[index]] direction:navDirection animated:YES completion:^(BOOL finished) { |
| 144 | + UIAccessibilityPostNotification(UIAccessibilityPageScrolledNotification, nil); |
| 145 | + }]; |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + return handled; |
| 150 | +} |
| 151 | + |
| 152 | +@end |
0 commit comments