|
1 | 1 | # PageControl
|
2 | 2 |
|
3 |
| -This control is designed to be a drop-in replacement for UIPageControl, but adhering to the |
4 |
| -material design specifications for animation and layout. |
| 3 | +This control is designed to be a drop-in replacement for `UIPageControl`, but adhering to the material design specifications for animation and layout. The API methods are the same as a `UIPageControl`, however with the addition of a few key methods required to achieve the desired animation of the control. |
5 | 4 |
|
6 | 5 | ## Requirements
|
7 | 6 |
|
8 |
| -- Xcode 7.0 or higher. |
9 |
| -- iOS SDK version 7.0 or higher. |
| 7 | +- Xcode 7.0 or higher |
| 8 | +- iOS SDK version 7.0 or higher |
| 9 | + |
| 10 | +## Page Control Animation |
| 11 | + |
| 12 | +This page control provides an animation effect that keeps a page indicator in sync with the scrolling of a designated scroll view. This is in contrast to a native `UIPageControl` that simply shows the current page indicator without any animated transitions between changes. As the user scrolls, a track will be drawn with animation from the current indicator position towards the next indicator position that is being scrolled towards. The current indicator will float along this track and position itself based on the percent scrolled between the pages. When the scroll view finishes scrolling, the track will disappear with animation towards the final position of the new page. |
| 13 | + |
| 14 | +#### Example Screenshots |
| 15 | + |
| 16 | +|  | |  | |
| 17 | +| ------------ | ------------- | ------------ | |
| 18 | +| Page control showing current page in resting state. | Page control showing animated track with current page indicator positioned | along the track. | Page control showing new current page. | |
| 19 | + |
| 20 | +#### Demo Video |
| 21 | + |
| 22 | +[](docs/MDCPageControl_video.mov) |
10 | 23 |
|
11 | 24 | ## Usage
|
12 | 25 |
|
| 26 | +Integrating the page control requires two steps. First, add a page control with companion scroll view, and second, forward the scroll view delegate methods to the page control. |
| 27 | + |
| 28 | +##### Step 1: Add the page control to a view |
| 29 | + |
| 30 | +Add the page control to a view and set the desired page control properties. This step is done similarly to a native `UIPageControl`. In addition, provide a tap gesture handler for the control to to fire off the `UIControlEventValueChanged` events in which the scroll view would typically be notified of page changes. |
| 31 | + |
13 | 32 | ```objectivec
|
14 |
| -// Page control configuration. |
15 |
| -CGRect frame = |
16 |
| - CGRectMake(0, CGRectGetHeight(self.view.bounds) - 40, CGRectGetWidth(self.view.bounds), 40); |
17 |
| -myPageControl = [[MDCPageControl alloc] initWithFrame:frame]; |
18 |
| -myPageControl.numberOfPages = _myPages.count; |
19 |
| -[myPageControl addTarget:self |
20 |
| - action:@selector(didChangePage:) |
21 |
| - forControlEvents:UIControlEventValueChanged]; |
22 |
| -[self.view addSubview:myPageControl]; |
23 |
| - |
24 |
| -#pragma mark - UIScrollViewDelegate |
| 33 | +@interface ViewController () <UIScrollViewDelegate> |
| 34 | +@end |
25 | 35 |
|
26 |
| -- (void)scrollViewDidScroll:(UIScrollView *)scrollView { |
27 |
| - [myPageControl scrollViewDidScroll:scrollView]; |
| 36 | +@implementation ViewController { |
| 37 | + UIScrollView *_scrollView; |
| 38 | + MDCPageControl *_pageControl; |
28 | 39 | }
|
29 | 40 |
|
30 |
| -- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { |
31 |
| - [myPageControl scrollViewDidEndDecelerating:scrollView]; |
| 41 | +- (void)viewDidLoad { |
| 42 | + [super viewDidLoad]; |
| 43 | + |
| 44 | + // Scroll view configuration |
| 45 | + _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; |
| 46 | + _scrollView.delegate = self; |
| 47 | + ... |
| 48 | + [self.view addSubview:_scrollView]; |
| 49 | + |
| 50 | + // Page control configuration. |
| 51 | + _pageControl = [[MDCPageControl alloc] initWithFrame:myFrame]; |
| 52 | + _pageControl.numberOfPages = 3; // Should match page count of scrollView. |
| 53 | + [_pageControl addTarget:self |
| 54 | + action:@selector(didChangePage:) |
| 55 | + forControlEvents:UIControlEventValueChanged]; |
| 56 | + [self.view addSubview:_pageControl]; |
32 | 57 | }
|
33 | 58 |
|
34 |
| -- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { |
35 |
| - [myPageControl scrollViewDidEndScrollingAnimation:scrollView]; |
| 59 | +- (void)didChangePage:(MDCPageControl *)sender { |
| 60 | + // Transition scroll view to new page. |
| 61 | + CGPoint offset = _scrollView.contentOffset; |
| 62 | + offset.x = sender.currentPage * _scrollView.bounds.size.width; |
| 63 | + [_scrollView setContentOffset:offset animated:YES]; |
36 | 64 | }
|
| 65 | +``` |
37 | 66 |
|
38 |
| -#pragma mark - User events |
| 67 | +##### Step 2: Forwarding the required scroll view delegate methods |
39 | 68 |
|
40 |
| -- (void)didChangePage:(MDCPageControl *)sender { |
41 |
| - CGPoint offset = myScrollView.contentOffset; |
42 |
| - offset.x = sender.currentPage * myScrollView.bounds.size.width; |
43 |
| - [myScrollView setContentOffset:offset animated:YES]; |
| 69 | +This page control is designed to be used in conjunction with a scroll view. To achieve the desired page control animation effects, there are three scroll view delegate methods that must be forwarded to the page control (`-scrollViewDidScroll`, `-scrollViewDidEndDecelerating`, and `-scrollViewDidEndScrollingAnimation`). This allows the page control to keep in sync with the scrolling movement of the designated scroll view. |
| 70 | +
|
| 71 | +```objectivec |
| 72 | +- (void)scrollViewDidScroll:(UIScrollView *)scrollView { |
| 73 | + [_pageControl scrollViewDidScroll:scrollView]; |
| 74 | +} |
| 75 | +
|
| 76 | +- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { |
| 77 | + [_pageControl scrollViewDidEndDecelerating:scrollView]; |
| 78 | +} |
| 79 | +
|
| 80 | +- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { |
| 81 | + [_pageControl scrollViewDidEndScrollingAnimation:scrollView]; |
44 | 82 | }
|
45 | 83 | ```
|
0 commit comments