Skip to content

Commit 70b577d

Browse files
committed
[PageControl] Updates readme.
Summary: Merge branch 'master' into page-control-docs # Conflicts: # components/PageControl/README.md Reviewers: ajsecord, #material_components_ios_owners Reviewed By: ajsecord, #material_components_ios_owners Projects: #material_components_ios_owners Differential Revision: http://codereview.cc/D50
1 parent f0a1edb commit 70b577d

File tree

5 files changed

+64
-26
lines changed

5 files changed

+64
-26
lines changed

components/PageControl/README.md

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,83 @@
11
# PageControl
22

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.
54

65
## Requirements
76

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+
| ![screenshot-1](docs/MDCPageControl_screenshot-1.png =250x) | ![screenshot-2](docs/MDCPageControl_screenshot-2.png =250x)| ![screenshot-3](docs/MDCPageControl_screenshot-3.png =250x) |
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+
[![ScreenShot](docs/MDCPageControl_screenshot-1.png)](docs/MDCPageControl_video.mov)
1023

1124
## Usage
1225

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+
1332
```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
2535

26-
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
27-
[myPageControl scrollViewDidScroll:scrollView];
36+
@implementation ViewController {
37+
UIScrollView *_scrollView;
38+
MDCPageControl *_pageControl;
2839
}
2940

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];
3257
}
3358

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];
3664
}
65+
```
3766
38-
#pragma mark - User events
67+
##### Step 2: Forwarding the required scroll view delegate methods
3968
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];
4482
}
4583
```
Loading
Loading
Loading
Binary file not shown.

0 commit comments

Comments
 (0)