├── logo.png
├── demo1.gif
├── demo2.gif
├── demo3.gif
├── demo4.gif
├── Example
├── LCAnimatedPageControlExample
│ ├── Images.xcassets
│ │ └── AppIcon.appiconset
│ │ │ ├── 40@2x.png
│ │ │ ├── 40@3x.png
│ │ │ └── Contents.json
│ ├── ViewController.h
│ ├── AppDelegate.h
│ ├── IndicatorView.h
│ ├── main.m
│ ├── LCAnimatedPageControl.h
│ ├── Info.plist
│ ├── IndicatorView.m
│ ├── AppDelegate.m
│ ├── Base.lproj
│ │ ├── LaunchScreen.xib
│ │ └── Main.storyboard
│ ├── ViewController.m
│ └── LCAnimatedPageControl.m
├── LCAnimatedPageControl.xcodeproj
│ ├── project.xcworkspace
│ │ └── contents.xcworkspacedata
│ ├── xcshareddata
│ │ └── xcschemes
│ │ │ └── LCAnimatedPageControl.xcscheme
│ └── project.pbxproj
└── LCAnimatedPageControl
│ └── Info.plist
├── IndicatorView.h
├── .gitignore
├── LCAnimatedPageControl.podspec
├── LCAnimatedPageControl.h
├── LICENSE
├── IndicatorView.m
├── README.md
└── LCAnimatedPageControl.m
/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bawn/LCAnimatedPageControl/HEAD/logo.png
--------------------------------------------------------------------------------
/demo1.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bawn/LCAnimatedPageControl/HEAD/demo1.gif
--------------------------------------------------------------------------------
/demo2.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bawn/LCAnimatedPageControl/HEAD/demo2.gif
--------------------------------------------------------------------------------
/demo3.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bawn/LCAnimatedPageControl/HEAD/demo3.gif
--------------------------------------------------------------------------------
/demo4.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bawn/LCAnimatedPageControl/HEAD/demo4.gif
--------------------------------------------------------------------------------
/Example/LCAnimatedPageControlExample/Images.xcassets/AppIcon.appiconset/40@2x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bawn/LCAnimatedPageControl/HEAD/Example/LCAnimatedPageControlExample/Images.xcassets/AppIcon.appiconset/40@2x.png
--------------------------------------------------------------------------------
/Example/LCAnimatedPageControlExample/Images.xcassets/AppIcon.appiconset/40@3x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bawn/LCAnimatedPageControl/HEAD/Example/LCAnimatedPageControlExample/Images.xcassets/AppIcon.appiconset/40@3x.png
--------------------------------------------------------------------------------
/Example/LCAnimatedPageControl.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Example/LCAnimatedPageControlExample/ViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.h
3 | // LCAnimatedPageControl
4 | //
5 | // Created by beike on 6/17/15.
6 | // Copyright (c) 2015 beike. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface ViewController : UIViewController
12 |
13 |
14 | @end
15 |
16 |
--------------------------------------------------------------------------------
/IndicatorView.h:
--------------------------------------------------------------------------------
1 | //
2 | // IndicatorView.h
3 | // LCAnimatedPageControl
4 | //
5 | // Created by beike on 8/14/15.
6 | // Copyright (c) 2015 beike. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface IndicatorView : UIView
12 |
13 | @property (nonatomic, strong) UIView *frontView;
14 | @property (nonatomic, strong) UIView *backView;
15 |
16 | @end
17 |
--------------------------------------------------------------------------------
/Example/LCAnimatedPageControlExample/AppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.h
3 | // LCAnimatedPageControl
4 | //
5 | // Created by beike on 6/17/15.
6 | // Copyright (c) 2015 beike. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface AppDelegate : UIResponder
12 |
13 | @property (strong, nonatomic) UIWindow *window;
14 |
15 |
16 | @end
17 |
18 |
--------------------------------------------------------------------------------
/Example/LCAnimatedPageControlExample/IndicatorView.h:
--------------------------------------------------------------------------------
1 | //
2 | // IndicatorView.h
3 | // LCAnimatedPageControl
4 | //
5 | // Created by beike on 8/14/15.
6 | // Copyright (c) 2015 beike. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | @interface IndicatorView : UIView
12 |
13 | @property (nonatomic, strong) UIView *frontView;
14 | @property (nonatomic, strong) UIView *backView;
15 |
16 | @end
17 |
--------------------------------------------------------------------------------
/Example/LCAnimatedPageControlExample/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // LCAnimatedPageControl
4 | //
5 | // Created by beike on 6/17/15.
6 | // Copyright (c) 2015 beike. All rights reserved.
7 | //
8 |
9 | #import
10 | #import "AppDelegate.h"
11 |
12 | int main(int argc, char * argv[]) {
13 | @autoreleasepool {
14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Xcode
2 | #
3 | build/
4 | *.pbxuser
5 | !default.pbxuser
6 | *.mode1v3
7 | !default.mode1v3
8 | *.mode2v3
9 | !default.mode2v3
10 | *.perspectivev3
11 | !default.perspectivev3
12 | xcuserdata
13 | *.xccheckout
14 | *.moved-aside
15 | DerivedData
16 | *.hmap
17 | *.ipa
18 | *.xcuserstate
19 |
20 | # CocoaPods
21 | #
22 | # We recommend against adding the Pods directory to your .gitignore. However
23 | # you should judge for yourself, the pros and cons are mentioned at:
24 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control
25 | #
26 | #Pods/
27 |
--------------------------------------------------------------------------------
/LCAnimatedPageControl.podspec:
--------------------------------------------------------------------------------
1 | Pod::Spec.new do |s|
2 | s.name = "LCAnimatedPageControl"
3 | s.version = "1.1.0"
4 | s.summary = "Custom UIPageControl"
5 | s.homepage = "https://github.com/bawn/LCAnimatedPageControl"
6 | s.license = 'MIT'
7 | s.author = { "bawn" => "lc5491137@gmail.com" }
8 | s.source = { :git => "https://github.com/bawn/LCAnimatedPageControl.git", :tag => s.version.to_s }
9 | s.platform = :ios, '6.0'
10 | s.requires_arc = true
11 | s.frameworks = 'Foundation', 'UIKit'
12 | s.source_files = 'LCAnimatedPageControl.{h,m}', 'IndicatorView.{h,m}'
13 |
14 | end
15 |
--------------------------------------------------------------------------------
/Example/LCAnimatedPageControl/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | FMWK
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | $(CURRENT_PROJECT_VERSION)
21 | NSPrincipalClass
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/LCAnimatedPageControl.h:
--------------------------------------------------------------------------------
1 | //
2 | // LCAnimatedPageControl.h
3 | // LCAnimatedPageControl
4 | //
5 | // Created by beike on 6/17/15.
6 | // Copyright (c) 2015 beike. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | typedef NS_ENUM(NSInteger, PageStyle){
12 | LCScaleColorPageStyle,
13 | LCSquirmPageStyle,
14 | LCDepthColorPageStyle,
15 | LCFillColorPageStyle,
16 | };
17 |
18 | @interface LCAnimatedPageControl : UIControl
19 |
20 | @property (nonatomic, strong) UIScrollView *sourceScrollView;
21 | @property (nonatomic, assign) NSInteger numberOfPages;
22 | @property (nonatomic, strong) UIColor *pageIndicatorColor;
23 | @property (nonatomic, strong) UIColor *currentPageIndicatorColor;
24 | @property (nonatomic, assign) CGFloat indicatorMultiple;
25 | @property (nonatomic, assign) CGFloat indicatorMargin;
26 | @property (nonatomic, assign) CGFloat indicatorDiameter;
27 | @property (nonatomic, assign) PageStyle pageStyle;
28 | @property (nonatomic, assign, readonly) NSInteger currentPage;
29 |
30 | - (void)prepareShow;
31 | - (void)clearIndicators;
32 |
33 | @end
34 |
--------------------------------------------------------------------------------
/Example/LCAnimatedPageControlExample/Images.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "20x20",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "20x20",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "29x29",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "29x29",
21 | "scale" : "3x"
22 | },
23 | {
24 | "size" : "40x40",
25 | "idiom" : "iphone",
26 | "filename" : "40@2x.png",
27 | "scale" : "2x"
28 | },
29 | {
30 | "size" : "40x40",
31 | "idiom" : "iphone",
32 | "filename" : "40@3x.png",
33 | "scale" : "3x"
34 | },
35 | {
36 | "idiom" : "iphone",
37 | "size" : "60x60",
38 | "scale" : "2x"
39 | },
40 | {
41 | "idiom" : "iphone",
42 | "size" : "60x60",
43 | "scale" : "3x"
44 | }
45 | ],
46 | "info" : {
47 | "version" : 1,
48 | "author" : "xcode"
49 | }
50 | }
--------------------------------------------------------------------------------
/Example/LCAnimatedPageControlExample/LCAnimatedPageControl.h:
--------------------------------------------------------------------------------
1 | //
2 | // LCAnimatedPageControl.h
3 | // LCAnimatedPageControl
4 | //
5 | // Created by beike on 6/17/15.
6 | // Copyright (c) 2015 beike. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | typedef NS_ENUM(NSInteger, PageStyle){
12 | LCScaleColorPageStyle,
13 | LCSquirmPageStyle,
14 | LCDepthColorPageStyle,
15 | LCFillColorPageStyle,
16 | };
17 |
18 | @interface LCAnimatedPageControl : UIControl
19 |
20 | @property (nonatomic, strong) UIScrollView *sourceScrollView;
21 | @property (nonatomic, assign) NSInteger numberOfPages;
22 | @property (nonatomic, strong) UIColor *pageIndicatorColor;
23 | @property (nonatomic, strong) UIColor *currentPageIndicatorColor;
24 | @property (nonatomic, assign) CGFloat indicatorMultiple;
25 | @property (nonatomic, assign) CGFloat indicatorMargin;
26 | @property (nonatomic, assign) CGFloat indicatorDiameter;
27 | @property (nonatomic, assign) PageStyle pageStyle;
28 | @property (nonatomic, assign, readonly) NSInteger currentPage;
29 |
30 | - (void)prepareShow;
31 | - (void)clearIndicators;
32 |
33 | @end
34 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 凌晨
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/Example/LCAnimatedPageControlExample/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | beike.$(PRODUCT_NAME:rfc1034identifier)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | APPL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleSignature
20 | ????
21 | CFBundleVersion
22 | 1
23 | LSRequiresIPhoneOS
24 |
25 | UILaunchStoryboardName
26 | LaunchScreen
27 | UIMainStoryboardFile
28 | Main
29 | UIRequiredDeviceCapabilities
30 |
31 | armv7
32 |
33 | UISupportedInterfaceOrientations
34 |
35 | UIInterfaceOrientationPortrait
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/IndicatorView.m:
--------------------------------------------------------------------------------
1 | //
2 | // IndicatorView.m
3 | // LCAnimatedPageControl
4 | //
5 | // Created by beike on 8/14/15.
6 | // Copyright (c) 2015 beike. All rights reserved.
7 | //
8 |
9 | #import "IndicatorView.h"
10 |
11 | @implementation IndicatorView
12 |
13 |
14 | - (instancetype)init{
15 | self = [super init];
16 | if (self) {
17 | _frontView = [[UIView alloc] init];
18 | _backView = [[UIView alloc] init];
19 | _frontView.translatesAutoresizingMaskIntoConstraints = NO;
20 | _backView.translatesAutoresizingMaskIntoConstraints = NO;
21 | [self addSubview:_backView];
22 | [self addSubview:_frontView];
23 |
24 | NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_frontView, _backView);
25 | [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_frontView]-0-|" options:0 metrics:nil views:viewsDictionary]];
26 | [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_frontView]-0-|" options:0 metrics:nil views:viewsDictionary]];
27 | [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_backView]-0-|" options:0 metrics:nil views:viewsDictionary]];
28 | [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_backView]-0-|" options:0 metrics:nil views:viewsDictionary]];
29 |
30 | }
31 | return self;
32 | }
33 |
34 | @end
35 |
--------------------------------------------------------------------------------
/Example/LCAnimatedPageControlExample/IndicatorView.m:
--------------------------------------------------------------------------------
1 | //
2 | // IndicatorView.m
3 | // LCAnimatedPageControl
4 | //
5 | // Created by beike on 8/14/15.
6 | // Copyright (c) 2015 beike. All rights reserved.
7 | //
8 |
9 | #import "IndicatorView.h"
10 |
11 | @implementation IndicatorView
12 |
13 |
14 | - (instancetype)init{
15 | self = [super init];
16 | if (self) {
17 | _frontView = [[UIView alloc] init];
18 | _backView = [[UIView alloc] init];
19 | _frontView.translatesAutoresizingMaskIntoConstraints = NO;
20 | _backView.translatesAutoresizingMaskIntoConstraints = NO;
21 | [self addSubview:_backView];
22 | [self addSubview:_frontView];
23 |
24 | NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_frontView, _backView);
25 | [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_frontView]-0-|" options:0 metrics:nil views:viewsDictionary]];
26 | [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_frontView]-0-|" options:0 metrics:nil views:viewsDictionary]];
27 | [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_backView]-0-|" options:0 metrics:nil views:viewsDictionary]];
28 | [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_backView]-0-|" options:0 metrics:nil views:viewsDictionary]];
29 |
30 | }
31 | return self;
32 | }
33 |
34 | @end
35 |
--------------------------------------------------------------------------------
/Example/LCAnimatedPageControlExample/AppDelegate.m:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.m
3 | // LCAnimatedPageControl
4 | //
5 | // Created by beike on 6/17/15.
6 | // Copyright (c) 2015 beike. All rights reserved.
7 | //
8 |
9 | #import "AppDelegate.h"
10 |
11 | @interface AppDelegate ()
12 |
13 | @end
14 |
15 | @implementation AppDelegate
16 |
17 |
18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
19 | // Override point for customization after application launch.
20 | return YES;
21 | }
22 |
23 | - (void)applicationWillResignActive:(UIApplication *)application {
24 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
25 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
26 | }
27 |
28 | - (void)applicationDidEnterBackground:(UIApplication *)application {
29 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
30 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
31 | }
32 |
33 | - (void)applicationWillEnterForeground:(UIApplication *)application {
34 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
35 | }
36 |
37 | - (void)applicationDidBecomeActive:(UIApplication *)application {
38 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
39 | }
40 |
41 | - (void)applicationWillTerminate:(UIApplication *)application {
42 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
43 | }
44 |
45 | @end
46 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # LCAnimatedPageControl
2 |
3 | 
4 | 
5 | 
6 | [](https://www.apple.com/nl/ios/)
7 |
8 | 
9 |
10 | Custom UIPageControl with a simple animation.
11 |
12 | ## Demo
13 |
14 | __ LCSquirmPageStyle LCScaleColorPageStyle__
15 |
16 |
17 |
18 |
19 |
20 | __ LCSquirmPageStyle LCFillColorPageStyle__
21 |
22 |
23 |
24 |
25 | ## Installation
26 |
27 | CocoaPods:
28 | ```
29 | pod 'LCAnimatedPageControl'
30 | ```
31 |
32 | ## Example Usage
33 | ```
34 | #import
35 | ```
36 | ```
37 | self.pageControl = [[LCAnimatedPageControl alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 40, 280, 20)];
38 | self.pageControl.center = CGPointMake(self.view.frame.size.width * 0.5f, _pageControl.center.y);
39 | self.pageControl.pageStyle = LCScalePageStyle;
40 | self.pageControl.numberOfPages = 5;
41 | self.pageControl.indicatorMargin = 5.0f;
42 | self.pageControl.indicatorMultiple = 1.6f;
43 | self.pageControl.pageIndicatorColor = [UIColor redColor];
44 | self.pageControl.currentPageIndicatorColor = [UIColor blackColor];
45 | self.pageControl.sourceScrollView = _collectionView;
46 | [self.pageControl prepareShow];
47 | [self.view addSubview:_pageControl];
48 | ```
49 |
50 | Use the ScalePageStyle, If you want to scrollView to scroll to the non-adjacent location, Please realize the following protocol methods
51 |
52 | ```
53 | - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView;{
54 | [self.pageControl clearIndicators];
55 | }
56 | ```
57 |
58 | ## Requirements
59 | * iOS 6 or higher
60 | * ARC
61 |
62 | ## More Info
63 | [Blog](http://bawn.github.io/ios/uipagecontrol/2015/06/16/LCAnimatedPageControl.html)
64 |
65 | ## License
66 |
67 | [MIT](http://mit-license.org/)
68 |
69 |
--------------------------------------------------------------------------------
/Example/LCAnimatedPageControl.xcodeproj/xcshareddata/xcschemes/LCAnimatedPageControl.xcscheme:
--------------------------------------------------------------------------------
1 |
2 |
5 |
8 |
9 |
15 |
21 |
22 |
23 |
24 |
25 |
30 |
31 |
32 |
33 |
34 |
35 |
45 |
46 |
52 |
53 |
54 |
55 |
56 |
57 |
63 |
64 |
70 |
71 |
72 |
73 |
75 |
76 |
79 |
80 |
81 |
--------------------------------------------------------------------------------
/Example/LCAnimatedPageControlExample/Base.lproj/LaunchScreen.xib:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
21 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/Example/LCAnimatedPageControlExample/ViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.m
3 | // LCAnimatedPageControl
4 | //
5 | // Created by beike on 6/17/15.
6 | // Copyright (c) 2015 beike. All rights reserved.
7 | //
8 |
9 | #import "ViewController.h"
10 | #import "LCAnimatedPageControl.h"
11 | #import "IndicatorView.h"
12 |
13 | @interface ViewController ()
14 |
15 | @property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
16 | @property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
17 | @property (strong, nonatomic) LCAnimatedPageControl *pageControl;
18 | @property (assign, nonatomic) NSUInteger number;
19 |
20 | @end
21 |
22 | @implementation ViewController
23 |
24 | - (void)viewDidLoad {
25 | [super viewDidLoad];
26 | [self.view layoutIfNeeded];
27 | [self.collectionView layoutIfNeeded];
28 | self.number = 5;
29 |
30 | self.pageControl = [[LCAnimatedPageControl alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 40, 280, 20)];
31 | self.pageControl.center = CGPointMake(self.view.frame.size.width * 0.5f, _pageControl.center.y);
32 | self.pageControl.numberOfPages = _number;
33 | self.pageControl.indicatorDiameter = 8.0f;
34 | self.pageControl.indicatorMargin = 10.0f;
35 | self.pageControl.indicatorMultiple = 1.4;
36 | self.pageControl.pageStyle = LCScaleColorPageStyle;
37 | self.pageControl.pageIndicatorColor = [UIColor colorWithRed:176.0f/255.0f green:176.0f/255.0f blue:176.0f/255.0f alpha:1.0f];
38 | self.pageControl.currentPageIndicatorColor = [UIColor colorWithRed:221.0f/255.0f green:34.0f/255.0f blue:56.0f/255.0f alpha:1.0f];
39 | self.pageControl.sourceScrollView = _collectionView;
40 | [self.pageControl prepareShow];
41 | [self.view addSubview:_pageControl];
42 |
43 |
44 | /* ------autolayout-------- */
45 |
46 | // self.pageControl = [[LCAnimatedPageControl alloc] init];
47 | // self.pageControl.translatesAutoresizingMaskIntoConstraints = NO;
48 | // self.pageControl.numberOfPages = _number;
49 | // self.pageControl.indicatorDiameter = 5.0f;// Required
50 | // self.pageControl.indicatorMargin = 18.0f;
51 | // self.pageControl.indicatorMultiple = 1.8f;
52 | // self.pageControl.pageStyle = ScaleColorPageStyle;
53 | // self.pageControl.pageIndicatorColor = [UIColor colorWithRed:176.0f/255.0f green:176.0f/255.0f blue:176.0f/255.0f alpha:1.0f];
54 | // self.pageControl.currentPageIndicatorColor = [UIColor colorWithRed:221.0f/255.0f green:34.0f/255.0f blue:56.0f/255.0f alpha:1.0f];
55 | // self.pageControl.sourceScrollView = _collectionView;
56 | // [self.pageControl prepareShow];
57 | // [self.view addSubview:_pageControl];
58 | //
59 | // [self.view addConstraints:@[
60 | // [NSLayoutConstraint constraintWithItem:self.pageControl attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.collectionView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f],
61 | // [NSLayoutConstraint constraintWithItem:self.pageControl attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f],
62 | // [NSLayoutConstraint constraintWithItem:self.pageControl attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f],
63 | // [NSLayoutConstraint constraintWithItem:self.pageControl attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f],
64 | // ]];
65 | //
66 | // [self.collectionView setContentOffset:CGPointMake(self.collectionView.bounds.size.width * 2, 0)];
67 |
68 | self.flowLayout.itemSize = CGSizeMake(self.collectionView.frame.size.width - 60, self.collectionView.frame.size.height - 100);
69 | [self.pageControl addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
70 | [self.collectionView reloadData];
71 |
72 |
73 | //
74 | // IndicatorView *view = [[IndicatorView alloc] init];
75 | // view.translatesAutoresizingMaskIntoConstraints = NO;
76 | // view.backgroundColor = [UIColor clearColor];
77 | // [self.view addSubview:view];
78 | //
79 | //
80 | // NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(view);
81 | // [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[view]-0-|" options:0 metrics:nil views:viewsDictionary]];
82 | // [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[view]-0-|" options:0 metrics:nil views:viewsDictionary]];
83 |
84 |
85 | }
86 |
87 |
88 | // If you want to scrollView to scroll to the non-adjacent location, Please realize the following protocol methods
89 |
90 | //- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView;{
91 | // [self.pageControl clearIndicators];
92 | //}
93 |
94 |
95 |
96 | - (void)didReceiveMemoryWarning {
97 | [super didReceiveMemoryWarning];
98 | // Dispose of any resources that can be recreated.
99 | }
100 |
101 | - (void)valueChanged:(LCAnimatedPageControl *)sender{
102 | // NSLog(@"%d", sender.currentPage);
103 | [self.collectionView setContentOffset:CGPointMake(self.collectionView.frame.size.width * (sender.currentPage + 0) , self.collectionView.contentOffset.y) animated:YES];
104 | }
105 |
106 | - (IBAction)buttonPress:(id)sender{
107 | if ([[(UIBarButtonItem *)sender title] isEqualToString:@"+"]) {
108 | self.number++;
109 | }
110 | else{
111 | if (self.number > 0) {
112 | self.number--;
113 | }
114 | }
115 | [self.collectionView reloadData];
116 | self.pageControl.numberOfPages = _number;
117 | }
118 |
119 | #pragma mark - UICollectionView DataSource
120 |
121 | - (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
122 | return 1;
123 | }
124 | - (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
125 | return _number;
126 | }
127 |
128 | - (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
129 | UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
130 | return cell;
131 | }
132 |
133 | - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
134 | return CGSizeMake(self.view.frame.size.width - 20, 400);
135 | }
136 |
137 |
138 | @end
139 |
--------------------------------------------------------------------------------
/Example/LCAnimatedPageControlExample/Base.lproj/Main.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
--------------------------------------------------------------------------------
/Example/LCAnimatedPageControl.xcodeproj/project.pbxproj:
--------------------------------------------------------------------------------
1 | // !$*UTF8*$!
2 | {
3 | archiveVersion = 1;
4 | classes = {
5 | };
6 | objectVersion = 46;
7 | objects = {
8 |
9 | /* Begin PBXBuildFile section */
10 | 7A68D8A41B450433004B77B9 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7A68D8A21B450433004B77B9 /* ViewController.m */; };
11 | 7A7420D91B3149EC00B30BEA /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 7A7420D81B3149EC00B30BEA /* main.m */; };
12 | 7A7420DC1B3149EC00B30BEA /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 7A7420DB1B3149EC00B30BEA /* AppDelegate.m */; };
13 | 7A7420E21B3149EC00B30BEA /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 7A7420E01B3149EC00B30BEA /* Main.storyboard */; };
14 | 7A7420E41B3149EC00B30BEA /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7A7420E31B3149EC00B30BEA /* Images.xcassets */; };
15 | 7A7420E71B3149EC00B30BEA /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 7A7420E51B3149EC00B30BEA /* LaunchScreen.xib */; };
16 | 9AE1926C1DF6910000A5A448 /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 9AE1926B1DF6910000A5A448 /* Info.plist */; };
17 | 9AE192791DF6912C00A5A448 /* LCAnimatedPageControl.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9AE192721DF6912B00A5A448 /* LCAnimatedPageControl.framework */; };
18 | 9AE1927A1DF6912C00A5A448 /* LCAnimatedPageControl.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 9AE192721DF6912B00A5A448 /* LCAnimatedPageControl.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
19 | 9AE192811DF6916D00A5A448 /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 9AE192801DF6916D00A5A448 /* Info.plist */; };
20 | 9AE192861DF6917700A5A448 /* IndicatorView.h in Headers */ = {isa = PBXBuildFile; fileRef = 9AE192821DF6917700A5A448 /* IndicatorView.h */; };
21 | 9AE192871DF6917700A5A448 /* IndicatorView.m in Sources */ = {isa = PBXBuildFile; fileRef = 9AE192831DF6917700A5A448 /* IndicatorView.m */; };
22 | 9AE192881DF6917700A5A448 /* LCAnimatedPageControl.h in Headers */ = {isa = PBXBuildFile; fileRef = 9AE192841DF6917700A5A448 /* LCAnimatedPageControl.h */; settings = {ATTRIBUTES = (Public, ); }; };
23 | 9AE192891DF6917700A5A448 /* LCAnimatedPageControl.m in Sources */ = {isa = PBXBuildFile; fileRef = 9AE192851DF6917700A5A448 /* LCAnimatedPageControl.m */; };
24 | /* End PBXBuildFile section */
25 |
26 | /* Begin PBXContainerItemProxy section */
27 | 9AE192771DF6912C00A5A448 /* PBXContainerItemProxy */ = {
28 | isa = PBXContainerItemProxy;
29 | containerPortal = 7A7420CB1B3149EC00B30BEA /* Project object */;
30 | proxyType = 1;
31 | remoteGlobalIDString = 9AE192711DF6912B00A5A448;
32 | remoteInfo = LCAnimatedPageControl;
33 | };
34 | /* End PBXContainerItemProxy section */
35 |
36 | /* Begin PBXCopyFilesBuildPhase section */
37 | 9AE1927E1DF6912C00A5A448 /* Embed Frameworks */ = {
38 | isa = PBXCopyFilesBuildPhase;
39 | buildActionMask = 2147483647;
40 | dstPath = "";
41 | dstSubfolderSpec = 10;
42 | files = (
43 | 9AE1927A1DF6912C00A5A448 /* LCAnimatedPageControl.framework in Embed Frameworks */,
44 | );
45 | name = "Embed Frameworks";
46 | runOnlyForDeploymentPostprocessing = 0;
47 | };
48 | /* End PBXCopyFilesBuildPhase section */
49 |
50 | /* Begin PBXFileReference section */
51 | 7A68D8A11B450433004B77B9 /* ViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; };
52 | 7A68D8A21B450433004B77B9 /* ViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; };
53 | 7A7420D31B3149EC00B30BEA /* LCAnimatedPageControlExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = LCAnimatedPageControlExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
54 | 7A7420D81B3149EC00B30BEA /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; };
55 | 7A7420DA1B3149EC00B30BEA /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; };
56 | 7A7420DB1B3149EC00B30BEA /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; };
57 | 7A7420E11B3149EC00B30BEA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; };
58 | 7A7420E31B3149EC00B30BEA /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; };
59 | 7A7420E61B3149EC00B30BEA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; };
60 | 9AE1926B1DF6910000A5A448 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; };
61 | 9AE192721DF6912B00A5A448 /* LCAnimatedPageControl.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = LCAnimatedPageControl.framework; sourceTree = BUILT_PRODUCTS_DIR; };
62 | 9AE192801DF6916D00A5A448 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; };
63 | 9AE192821DF6917700A5A448 /* IndicatorView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = IndicatorView.h; path = ../../IndicatorView.h; sourceTree = ""; };
64 | 9AE192831DF6917700A5A448 /* IndicatorView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = IndicatorView.m; path = ../../IndicatorView.m; sourceTree = ""; };
65 | 9AE192841DF6917700A5A448 /* LCAnimatedPageControl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = LCAnimatedPageControl.h; path = ../../LCAnimatedPageControl.h; sourceTree = ""; };
66 | 9AE192851DF6917700A5A448 /* LCAnimatedPageControl.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = LCAnimatedPageControl.m; path = ../../LCAnimatedPageControl.m; sourceTree = ""; };
67 | /* End PBXFileReference section */
68 |
69 | /* Begin PBXFrameworksBuildPhase section */
70 | 7A7420D01B3149EC00B30BEA /* Frameworks */ = {
71 | isa = PBXFrameworksBuildPhase;
72 | buildActionMask = 2147483647;
73 | files = (
74 | 9AE192791DF6912C00A5A448 /* LCAnimatedPageControl.framework in Frameworks */,
75 | );
76 | runOnlyForDeploymentPostprocessing = 0;
77 | };
78 | 9AE1926E1DF6912B00A5A448 /* Frameworks */ = {
79 | isa = PBXFrameworksBuildPhase;
80 | buildActionMask = 2147483647;
81 | files = (
82 | );
83 | runOnlyForDeploymentPostprocessing = 0;
84 | };
85 | /* End PBXFrameworksBuildPhase section */
86 |
87 | /* Begin PBXGroup section */
88 | 7A7420CA1B3149EC00B30BEA = {
89 | isa = PBXGroup;
90 | children = (
91 | 7A7420D51B3149EC00B30BEA /* LCAnimatedPageControlExample */,
92 | 9AE1927F1DF6916D00A5A448 /* LCAnimatedPageControl */,
93 | 7A7420D41B3149EC00B30BEA /* Products */,
94 | );
95 | sourceTree = "";
96 | };
97 | 7A7420D41B3149EC00B30BEA /* Products */ = {
98 | isa = PBXGroup;
99 | children = (
100 | 7A7420D31B3149EC00B30BEA /* LCAnimatedPageControlExample.app */,
101 | 9AE192721DF6912B00A5A448 /* LCAnimatedPageControl.framework */,
102 | );
103 | name = Products;
104 | sourceTree = "";
105 | };
106 | 7A7420D51B3149EC00B30BEA /* LCAnimatedPageControlExample */ = {
107 | isa = PBXGroup;
108 | children = (
109 | 7A68D8A11B450433004B77B9 /* ViewController.h */,
110 | 7A68D8A21B450433004B77B9 /* ViewController.m */,
111 | 7A7420DA1B3149EC00B30BEA /* AppDelegate.h */,
112 | 7A7420DB1B3149EC00B30BEA /* AppDelegate.m */,
113 | 7A7420E01B3149EC00B30BEA /* Main.storyboard */,
114 | 7A7420E31B3149EC00B30BEA /* Images.xcassets */,
115 | 7A7420E51B3149EC00B30BEA /* LaunchScreen.xib */,
116 | 7A7420D61B3149EC00B30BEA /* Supporting Files */,
117 | );
118 | path = LCAnimatedPageControlExample;
119 | sourceTree = "";
120 | };
121 | 7A7420D61B3149EC00B30BEA /* Supporting Files */ = {
122 | isa = PBXGroup;
123 | children = (
124 | 9AE1926B1DF6910000A5A448 /* Info.plist */,
125 | 7A7420D81B3149EC00B30BEA /* main.m */,
126 | );
127 | name = "Supporting Files";
128 | sourceTree = "";
129 | };
130 | 9AE1927F1DF6916D00A5A448 /* LCAnimatedPageControl */ = {
131 | isa = PBXGroup;
132 | children = (
133 | 9AE192821DF6917700A5A448 /* IndicatorView.h */,
134 | 9AE192831DF6917700A5A448 /* IndicatorView.m */,
135 | 9AE192841DF6917700A5A448 /* LCAnimatedPageControl.h */,
136 | 9AE192851DF6917700A5A448 /* LCAnimatedPageControl.m */,
137 | 9AE192801DF6916D00A5A448 /* Info.plist */,
138 | );
139 | path = LCAnimatedPageControl;
140 | sourceTree = "";
141 | };
142 | /* End PBXGroup section */
143 |
144 | /* Begin PBXHeadersBuildPhase section */
145 | 9AE1926F1DF6912B00A5A448 /* Headers */ = {
146 | isa = PBXHeadersBuildPhase;
147 | buildActionMask = 2147483647;
148 | files = (
149 | 9AE192881DF6917700A5A448 /* LCAnimatedPageControl.h in Headers */,
150 | 9AE192861DF6917700A5A448 /* IndicatorView.h in Headers */,
151 | );
152 | runOnlyForDeploymentPostprocessing = 0;
153 | };
154 | /* End PBXHeadersBuildPhase section */
155 |
156 | /* Begin PBXNativeTarget section */
157 | 7A7420D21B3149EC00B30BEA /* LCAnimatedPageControlExample */ = {
158 | isa = PBXNativeTarget;
159 | buildConfigurationList = 7A7420F61B3149EC00B30BEA /* Build configuration list for PBXNativeTarget "LCAnimatedPageControlExample" */;
160 | buildPhases = (
161 | 7A7420CF1B3149EC00B30BEA /* Sources */,
162 | 7A7420D01B3149EC00B30BEA /* Frameworks */,
163 | 7A7420D11B3149EC00B30BEA /* Resources */,
164 | 9AE1927E1DF6912C00A5A448 /* Embed Frameworks */,
165 | );
166 | buildRules = (
167 | );
168 | dependencies = (
169 | 9AE192781DF6912C00A5A448 /* PBXTargetDependency */,
170 | );
171 | name = LCAnimatedPageControlExample;
172 | productName = LCAnimatedPageControl;
173 | productReference = 7A7420D31B3149EC00B30BEA /* LCAnimatedPageControlExample.app */;
174 | productType = "com.apple.product-type.application";
175 | };
176 | 9AE192711DF6912B00A5A448 /* LCAnimatedPageControl */ = {
177 | isa = PBXNativeTarget;
178 | buildConfigurationList = 9AE1927B1DF6912C00A5A448 /* Build configuration list for PBXNativeTarget "LCAnimatedPageControl" */;
179 | buildPhases = (
180 | 9AE1926D1DF6912B00A5A448 /* Sources */,
181 | 9AE1926E1DF6912B00A5A448 /* Frameworks */,
182 | 9AE1926F1DF6912B00A5A448 /* Headers */,
183 | 9AE192701DF6912B00A5A448 /* Resources */,
184 | );
185 | buildRules = (
186 | );
187 | dependencies = (
188 | );
189 | name = LCAnimatedPageControl;
190 | productName = LCAnimatedPageControl;
191 | productReference = 9AE192721DF6912B00A5A448 /* LCAnimatedPageControl.framework */;
192 | productType = "com.apple.product-type.framework";
193 | };
194 | /* End PBXNativeTarget section */
195 |
196 | /* Begin PBXProject section */
197 | 7A7420CB1B3149EC00B30BEA /* Project object */ = {
198 | isa = PBXProject;
199 | attributes = {
200 | LastUpgradeCheck = 0630;
201 | ORGANIZATIONNAME = beike;
202 | TargetAttributes = {
203 | 7A7420D21B3149EC00B30BEA = {
204 | CreatedOnToolsVersion = 6.3.2;
205 | };
206 | 9AE192711DF6912B00A5A448 = {
207 | CreatedOnToolsVersion = 8.1;
208 | ProvisioningStyle = Automatic;
209 | };
210 | };
211 | };
212 | buildConfigurationList = 7A7420CE1B3149EC00B30BEA /* Build configuration list for PBXProject "LCAnimatedPageControl" */;
213 | compatibilityVersion = "Xcode 3.2";
214 | developmentRegion = English;
215 | hasScannedForEncodings = 0;
216 | knownRegions = (
217 | en,
218 | Base,
219 | );
220 | mainGroup = 7A7420CA1B3149EC00B30BEA;
221 | productRefGroup = 7A7420D41B3149EC00B30BEA /* Products */;
222 | projectDirPath = "";
223 | projectRoot = "";
224 | targets = (
225 | 7A7420D21B3149EC00B30BEA /* LCAnimatedPageControlExample */,
226 | 9AE192711DF6912B00A5A448 /* LCAnimatedPageControl */,
227 | );
228 | };
229 | /* End PBXProject section */
230 |
231 | /* Begin PBXResourcesBuildPhase section */
232 | 7A7420D11B3149EC00B30BEA /* Resources */ = {
233 | isa = PBXResourcesBuildPhase;
234 | buildActionMask = 2147483647;
235 | files = (
236 | 9AE1926C1DF6910000A5A448 /* Info.plist in Resources */,
237 | 7A7420E21B3149EC00B30BEA /* Main.storyboard in Resources */,
238 | 7A7420E71B3149EC00B30BEA /* LaunchScreen.xib in Resources */,
239 | 7A7420E41B3149EC00B30BEA /* Images.xcassets in Resources */,
240 | );
241 | runOnlyForDeploymentPostprocessing = 0;
242 | };
243 | 9AE192701DF6912B00A5A448 /* Resources */ = {
244 | isa = PBXResourcesBuildPhase;
245 | buildActionMask = 2147483647;
246 | files = (
247 | 9AE192811DF6916D00A5A448 /* Info.plist in Resources */,
248 | );
249 | runOnlyForDeploymentPostprocessing = 0;
250 | };
251 | /* End PBXResourcesBuildPhase section */
252 |
253 | /* Begin PBXSourcesBuildPhase section */
254 | 7A7420CF1B3149EC00B30BEA /* Sources */ = {
255 | isa = PBXSourcesBuildPhase;
256 | buildActionMask = 2147483647;
257 | files = (
258 | 7A7420DC1B3149EC00B30BEA /* AppDelegate.m in Sources */,
259 | 7A68D8A41B450433004B77B9 /* ViewController.m in Sources */,
260 | 7A7420D91B3149EC00B30BEA /* main.m in Sources */,
261 | );
262 | runOnlyForDeploymentPostprocessing = 0;
263 | };
264 | 9AE1926D1DF6912B00A5A448 /* Sources */ = {
265 | isa = PBXSourcesBuildPhase;
266 | buildActionMask = 2147483647;
267 | files = (
268 | 9AE192891DF6917700A5A448 /* LCAnimatedPageControl.m in Sources */,
269 | 9AE192871DF6917700A5A448 /* IndicatorView.m in Sources */,
270 | );
271 | runOnlyForDeploymentPostprocessing = 0;
272 | };
273 | /* End PBXSourcesBuildPhase section */
274 |
275 | /* Begin PBXTargetDependency section */
276 | 9AE192781DF6912C00A5A448 /* PBXTargetDependency */ = {
277 | isa = PBXTargetDependency;
278 | target = 9AE192711DF6912B00A5A448 /* LCAnimatedPageControl */;
279 | targetProxy = 9AE192771DF6912C00A5A448 /* PBXContainerItemProxy */;
280 | };
281 | /* End PBXTargetDependency section */
282 |
283 | /* Begin PBXVariantGroup section */
284 | 7A7420E01B3149EC00B30BEA /* Main.storyboard */ = {
285 | isa = PBXVariantGroup;
286 | children = (
287 | 7A7420E11B3149EC00B30BEA /* Base */,
288 | );
289 | name = Main.storyboard;
290 | sourceTree = "";
291 | };
292 | 7A7420E51B3149EC00B30BEA /* LaunchScreen.xib */ = {
293 | isa = PBXVariantGroup;
294 | children = (
295 | 7A7420E61B3149EC00B30BEA /* Base */,
296 | );
297 | name = LaunchScreen.xib;
298 | sourceTree = "";
299 | };
300 | /* End PBXVariantGroup section */
301 |
302 | /* Begin XCBuildConfiguration section */
303 | 7A7420F41B3149EC00B30BEA /* Debug */ = {
304 | isa = XCBuildConfiguration;
305 | buildSettings = {
306 | ALWAYS_SEARCH_USER_PATHS = NO;
307 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
308 | CLANG_CXX_LIBRARY = "libc++";
309 | CLANG_ENABLE_MODULES = YES;
310 | CLANG_ENABLE_OBJC_ARC = YES;
311 | CLANG_WARN_BOOL_CONVERSION = YES;
312 | CLANG_WARN_CONSTANT_CONVERSION = YES;
313 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
314 | CLANG_WARN_EMPTY_BODY = YES;
315 | CLANG_WARN_ENUM_CONVERSION = YES;
316 | CLANG_WARN_INT_CONVERSION = YES;
317 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
318 | CLANG_WARN_UNREACHABLE_CODE = YES;
319 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
320 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
321 | COPY_PHASE_STRIP = NO;
322 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
323 | ENABLE_STRICT_OBJC_MSGSEND = YES;
324 | GCC_C_LANGUAGE_STANDARD = gnu99;
325 | GCC_DYNAMIC_NO_PIC = NO;
326 | GCC_NO_COMMON_BLOCKS = YES;
327 | GCC_OPTIMIZATION_LEVEL = 0;
328 | GCC_PREPROCESSOR_DEFINITIONS = (
329 | "DEBUG=1",
330 | "$(inherited)",
331 | );
332 | GCC_SYMBOLS_PRIVATE_EXTERN = NO;
333 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
334 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
335 | GCC_WARN_UNDECLARED_SELECTOR = YES;
336 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
337 | GCC_WARN_UNUSED_FUNCTION = YES;
338 | GCC_WARN_UNUSED_VARIABLE = YES;
339 | IPHONEOS_DEPLOYMENT_TARGET = 8.0;
340 | MTL_ENABLE_DEBUG_INFO = YES;
341 | ONLY_ACTIVE_ARCH = YES;
342 | SDKROOT = iphoneos;
343 | };
344 | name = Debug;
345 | };
346 | 7A7420F51B3149EC00B30BEA /* Release */ = {
347 | isa = XCBuildConfiguration;
348 | buildSettings = {
349 | ALWAYS_SEARCH_USER_PATHS = NO;
350 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
351 | CLANG_CXX_LIBRARY = "libc++";
352 | CLANG_ENABLE_MODULES = YES;
353 | CLANG_ENABLE_OBJC_ARC = YES;
354 | CLANG_WARN_BOOL_CONVERSION = YES;
355 | CLANG_WARN_CONSTANT_CONVERSION = YES;
356 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
357 | CLANG_WARN_EMPTY_BODY = YES;
358 | CLANG_WARN_ENUM_CONVERSION = YES;
359 | CLANG_WARN_INT_CONVERSION = YES;
360 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
361 | CLANG_WARN_UNREACHABLE_CODE = YES;
362 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
363 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
364 | COPY_PHASE_STRIP = NO;
365 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
366 | ENABLE_NS_ASSERTIONS = NO;
367 | ENABLE_STRICT_OBJC_MSGSEND = YES;
368 | GCC_C_LANGUAGE_STANDARD = gnu99;
369 | GCC_NO_COMMON_BLOCKS = YES;
370 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
371 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
372 | GCC_WARN_UNDECLARED_SELECTOR = YES;
373 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
374 | GCC_WARN_UNUSED_FUNCTION = YES;
375 | GCC_WARN_UNUSED_VARIABLE = YES;
376 | IPHONEOS_DEPLOYMENT_TARGET = 8.0;
377 | MTL_ENABLE_DEBUG_INFO = NO;
378 | SDKROOT = iphoneos;
379 | VALIDATE_PRODUCT = YES;
380 | };
381 | name = Release;
382 | };
383 | 7A7420F71B3149EC00B30BEA /* Debug */ = {
384 | isa = XCBuildConfiguration;
385 | buildSettings = {
386 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
387 | INFOPLIST_FILE = "$(SRCROOT)/LCAnimatedPageControlExample/Info.plist";
388 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
389 | PRODUCT_NAME = "$(TARGET_NAME)";
390 | };
391 | name = Debug;
392 | };
393 | 7A7420F81B3149EC00B30BEA /* Release */ = {
394 | isa = XCBuildConfiguration;
395 | buildSettings = {
396 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
397 | INFOPLIST_FILE = "$(SRCROOT)/LCAnimatedPageControlExample/Info.plist";
398 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
399 | PRODUCT_NAME = "$(TARGET_NAME)";
400 | };
401 | name = Release;
402 | };
403 | 9AE1927C1DF6912C00A5A448 /* Debug */ = {
404 | isa = XCBuildConfiguration;
405 | buildSettings = {
406 | CLANG_ANALYZER_NONNULL = YES;
407 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
408 | CLANG_WARN_INFINITE_RECURSION = YES;
409 | CLANG_WARN_SUSPICIOUS_MOVES = YES;
410 | CODE_SIGN_IDENTITY = "";
411 | CURRENT_PROJECT_VERSION = 1;
412 | DEBUG_INFORMATION_FORMAT = dwarf;
413 | DEFINES_MODULE = YES;
414 | DYLIB_COMPATIBILITY_VERSION = 1;
415 | DYLIB_CURRENT_VERSION = 1;
416 | DYLIB_INSTALL_NAME_BASE = "@rpath";
417 | ENABLE_TESTABILITY = YES;
418 | INFOPLIST_FILE = LCAnimatedPageControl/Info.plist;
419 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
420 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
421 | PRODUCT_BUNDLE_IDENTIFIER = beike.LCAnimatedPageControl;
422 | PRODUCT_NAME = "$(TARGET_NAME)";
423 | SKIP_INSTALL = YES;
424 | TARGETED_DEVICE_FAMILY = "1,2";
425 | VERSIONING_SYSTEM = "apple-generic";
426 | VERSION_INFO_PREFIX = "";
427 | };
428 | name = Debug;
429 | };
430 | 9AE1927D1DF6912C00A5A448 /* Release */ = {
431 | isa = XCBuildConfiguration;
432 | buildSettings = {
433 | CLANG_ANALYZER_NONNULL = YES;
434 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
435 | CLANG_WARN_INFINITE_RECURSION = YES;
436 | CLANG_WARN_SUSPICIOUS_MOVES = YES;
437 | CODE_SIGN_IDENTITY = "";
438 | CURRENT_PROJECT_VERSION = 1;
439 | DEFINES_MODULE = YES;
440 | DYLIB_COMPATIBILITY_VERSION = 1;
441 | DYLIB_CURRENT_VERSION = 1;
442 | DYLIB_INSTALL_NAME_BASE = "@rpath";
443 | INFOPLIST_FILE = LCAnimatedPageControl/Info.plist;
444 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
445 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
446 | PRODUCT_BUNDLE_IDENTIFIER = beike.LCAnimatedPageControl;
447 | PRODUCT_NAME = "$(TARGET_NAME)";
448 | SKIP_INSTALL = YES;
449 | TARGETED_DEVICE_FAMILY = "1,2";
450 | VERSIONING_SYSTEM = "apple-generic";
451 | VERSION_INFO_PREFIX = "";
452 | };
453 | name = Release;
454 | };
455 | /* End XCBuildConfiguration section */
456 |
457 | /* Begin XCConfigurationList section */
458 | 7A7420CE1B3149EC00B30BEA /* Build configuration list for PBXProject "LCAnimatedPageControl" */ = {
459 | isa = XCConfigurationList;
460 | buildConfigurations = (
461 | 7A7420F41B3149EC00B30BEA /* Debug */,
462 | 7A7420F51B3149EC00B30BEA /* Release */,
463 | );
464 | defaultConfigurationIsVisible = 0;
465 | defaultConfigurationName = Release;
466 | };
467 | 7A7420F61B3149EC00B30BEA /* Build configuration list for PBXNativeTarget "LCAnimatedPageControlExample" */ = {
468 | isa = XCConfigurationList;
469 | buildConfigurations = (
470 | 7A7420F71B3149EC00B30BEA /* Debug */,
471 | 7A7420F81B3149EC00B30BEA /* Release */,
472 | );
473 | defaultConfigurationIsVisible = 0;
474 | defaultConfigurationName = Release;
475 | };
476 | 9AE1927B1DF6912C00A5A448 /* Build configuration list for PBXNativeTarget "LCAnimatedPageControl" */ = {
477 | isa = XCConfigurationList;
478 | buildConfigurations = (
479 | 9AE1927C1DF6912C00A5A448 /* Debug */,
480 | 9AE1927D1DF6912C00A5A448 /* Release */,
481 | );
482 | defaultConfigurationIsVisible = 0;
483 | };
484 | /* End XCConfigurationList section */
485 | };
486 | rootObject = 7A7420CB1B3149EC00B30BEA /* Project object */;
487 | }
488 |
--------------------------------------------------------------------------------
/LCAnimatedPageControl.m:
--------------------------------------------------------------------------------
1 | //
2 | // LCAnimatedPageControl.m
3 | // LCAnimatedPageControl
4 | //
5 | // Created by beike on 6/17/15.
6 | // Copyright (c) 2015 beike. All rights reserved.
7 | //
8 |
9 | #import "LCAnimatedPageControl.h"
10 | #import "IndicatorView.h"
11 |
12 | static CGFloat kLCDoubleNumber = 2.0f;
13 | static CGFloat kLCHalfNumber = 0.5f;
14 | static CGFloat kLCMultiple = 1.4f;
15 |
16 | @interface LCAnimatedPageControl ()
17 |
18 | @property (nonatomic, strong) NSMutableArray *indicatorViews;
19 | @property (nonatomic, strong) NSMutableArray *indicatorCons;
20 | @property (nonatomic, assign) NSInteger currentPage;
21 | @property (nonatomic, strong) UIView *contentView;
22 | @property (nonatomic, assign) BOOL isDefaultSet;
23 | @property (nonatomic, assign) CGFloat radius;
24 | @property (nonatomic, strong) NSLayoutConstraint *contentWidthCon;
25 | @property (nonatomic, strong) NSLayoutConstraint *squirmCenterCon;
26 | @property (nonatomic, strong) NSLayoutConstraint *squirmWidthCon;
27 | @property (nonatomic, strong) UIView *squirmView;
28 |
29 | @end
30 |
31 | @implementation LCAnimatedPageControl
32 |
33 | - (instancetype)init{
34 | self = [super init];
35 | if (self) {
36 | [self initialize];
37 | }
38 | return self;
39 | }
40 |
41 | - (instancetype)initWithFrame:(CGRect)frame{
42 | self = [super initWithFrame:frame];
43 | if (self) {
44 | [self initialize];
45 | }
46 | return self;
47 | }
48 |
49 | - (void)initialize{
50 |
51 | _contentView = [[UIView alloc] init];
52 | self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
53 | [self addSubview:_contentView];
54 | [self addConstraints:@[
55 | [NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f],
56 | [NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f]
57 | ]];
58 | _indicatorViews = [NSMutableArray array];
59 | _indicatorCons = [NSMutableArray array];
60 | _numberOfPages = 0;
61 | _currentPage = 0;
62 | _pageIndicatorColor = [UIColor orangeColor];
63 | _currentPageIndicatorColor = [UIColor blackColor];
64 | _indicatorMultiple = kLCMultiple;
65 | _indicatorDiameter = self.frame.size.height / _indicatorMultiple;
66 | _indicatorMargin = 0.0f;
67 | _radius = _indicatorDiameter * kLCHalfNumber;
68 |
69 | }
70 |
71 | - (void)prepareShow{
72 |
73 | [self addIndicatorsWithIndex:0];
74 | [self.sourceScrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
75 | CGFloat viewWidth = (_indicatorDiameter * _indicatorMultiple) * _numberOfPages + (_numberOfPages - 1) * _indicatorMargin;
76 | self.contentWidthCon = [NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0f constant:viewWidth];
77 | [self.contentView addConstraint:_contentWidthCon];
78 | [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0f constant:_indicatorDiameter * _indicatorMultiple]];
79 |
80 | if (_pageStyle == LCScaleColorPageStyle) {
81 | [self configDefaultIndicator];
82 | }
83 | else if (_pageStyle == LCDepthColorPageStyle){
84 | [self configDefaultIndicator];
85 | [self.indicatorViews.firstObject setBackgroundColor:_currentPageIndicatorColor];
86 | [self.contentView bringSubviewToFront:self.indicatorViews.firstObject];
87 | }
88 | else if(_pageStyle == LCSquirmPageStyle){
89 | self.squirmView = [[UIView alloc] init];
90 | self.squirmView.translatesAutoresizingMaskIntoConstraints = NO;
91 | self.squirmView .clipsToBounds = YES;
92 | self.squirmView .layer.cornerRadius = _radius;
93 | self.squirmView .backgroundColor = _currentPageIndicatorColor;
94 | [self.contentView addSubview:_squirmView];
95 |
96 | [self layoutSquirmView];
97 | }
98 | else if (_pageStyle == LCFillColorPageStyle){
99 | [self configDefaultIndicator];
100 | }
101 | }
102 |
103 | - (void)setPageStyle:(PageStyle)pageStyle{
104 | _pageStyle = pageStyle;
105 | if (_pageStyle == LCSquirmPageStyle) {
106 | self.indicatorMultiple = 1.0f;
107 | }
108 | }
109 |
110 |
111 | - (void)clearIndicators{
112 | for (UIView *view in _indicatorViews) {
113 | if (view.layer.timeOffset < 1.0f) {
114 | view.layer.timeOffset = 0.0f;
115 | }
116 | }
117 | }
118 |
119 | - (void)setIndicatorDiameter:(CGFloat)indicatorDiameter{
120 | _indicatorDiameter = indicatorDiameter;
121 | self.radius = _indicatorDiameter * kLCHalfNumber;
122 | }
123 |
124 | - (void)addIndicatorsWithIndex:(NSInteger)index{
125 | for (NSInteger number = index; number < _numberOfPages; number ++ ) {
126 | UIView *indicator = nil;
127 | if (_pageStyle == LCFillColorPageStyle) {
128 | indicator = [[IndicatorView alloc] init];
129 | [(IndicatorView *)indicator backView].backgroundColor = _currentPageIndicatorColor;
130 | [(IndicatorView *)indicator frontView].backgroundColor = _pageIndicatorColor;
131 | [(IndicatorView *)indicator backView].layer.cornerRadius = _radius;
132 | [(IndicatorView *)indicator frontView].layer.cornerRadius = _radius;
133 | [self configZeroScaleAnimation:[(IndicatorView *)indicator backView]];
134 | [self configZeroScaleAnimation:[(IndicatorView *)indicator frontView]];
135 | }
136 | else{
137 | indicator = [[UIView alloc] init];
138 | indicator.backgroundColor = _pageIndicatorColor;
139 | }
140 | indicator.clipsToBounds = YES;
141 | indicator.layer.cornerRadius = _radius;
142 |
143 | [self.contentView addSubview:indicator];
144 | [self.indicatorViews addObject:indicator];
145 | if (_pageStyle == LCScaleColorPageStyle) {
146 | [self configCSAnimation:indicator];
147 | }
148 | else if(_pageStyle == LCDepthColorPageStyle){
149 | if (number == 0) {
150 | [self configScaleAnimation:indicator];
151 | }
152 | else{
153 | [self configSmallScaleAnimation:indicator];
154 | }
155 | }
156 | }
157 | [self layoutContentView];
158 | }
159 |
160 |
161 | - (void)removeIndicatorsWithNumber:(NSInteger)number{
162 | NSArray *array = [self.indicatorViews subarrayWithRange:NSMakeRange(0, ABS(number))];
163 | [array makeObjectsPerformSelector:@selector(removeFromSuperview)];
164 | [self.indicatorViews removeObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, ABS(number))]];
165 | [self layoutContentView];
166 | }
167 |
168 |
169 | - (void)setCurrentPage:(NSInteger)currentPage{
170 | [self setCurrentPage:currentPage sendEvent:NO];
171 | }
172 |
173 | - (void)setCurrentPage:(NSInteger)currentPage sendEvent:(BOOL)sendEvent{
174 | _currentPage = MIN(currentPage, _numberOfPages - 1);
175 | if (sendEvent) {
176 | [self sendActionsForControlEvents:UIControlEventValueChanged];
177 | }
178 | }
179 |
180 | - (void)setNumberOfPages:(NSInteger)numberOfPages{
181 | numberOfPages = MAX(0, numberOfPages);
182 | NSInteger difference = numberOfPages - _numberOfPages;
183 | NSInteger lastNumberPages = _numberOfPages;
184 | _numberOfPages = numberOfPages;
185 | if (difference && self.superview) {
186 | [self.contentView removeConstraints:_contentView.constraints];
187 | // remove
188 | if (difference < 0) {
189 | if (_currentPage != lastNumberPages - 1) {
190 | [self configCurrentIndicator];
191 | }
192 | if (_currentPage > _numberOfPages - 1) {
193 | _currentPage = _numberOfPages - 1;
194 | }
195 | [self removeIndicatorsWithNumber:difference];
196 | }
197 | // add
198 | else{
199 | [self addIndicatorsWithIndex:lastNumberPages];
200 | }
201 | [self resetContentLayout];
202 | if (_pageStyle == LCDepthColorPageStyle) {
203 | [self configDepthView];
204 | }
205 | else if (_pageStyle == LCSquirmPageStyle) {
206 | [self layoutSquirmView];
207 | }
208 | }
209 | }
210 |
211 | - (void)configCurrentIndicator{
212 | UIView *view = self.indicatorViews[_currentPage];
213 | if ([view isKindOfClass:[IndicatorView class]]) {
214 | [(IndicatorView *)view frontView].layer.timeOffset = 1.0f;
215 | }
216 | else{
217 | view.layer.timeOffset = 0.0f;
218 | }
219 | }
220 |
221 |
222 | - (void)resetContentLayout{
223 | if (_numberOfPages) {
224 | CGFloat viewWidth = (_indicatorDiameter * _indicatorMultiple) * _numberOfPages + (_numberOfPages - 1) * _indicatorMargin;
225 | self.contentWidthCon = [NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0f constant:viewWidth];
226 | [self.contentView addConstraint:_contentWidthCon];
227 | [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0f constant:_indicatorDiameter * _indicatorMultiple]];
228 | }
229 | }
230 |
231 |
232 | - (void)configDepthView{
233 | UIView *depthView = self.indicatorViews.firstObject;
234 | [depthView.layer removeAllAnimations];
235 | depthView.backgroundColor = _currentPageIndicatorColor;
236 | [self.contentView bringSubviewToFront:depthView];
237 | [self configScaleAnimation:depthView];
238 |
239 | for (NSInteger index = 1; index <= _currentPage; index ++) {
240 | NSLayoutConstraint *con = self.indicatorCons[index];
241 | con.constant = (_indicatorDiameter * _indicatorMultiple * kLCHalfNumber) + (index - 1) * (_indicatorDiameter * _indicatorMultiple + _indicatorMargin);
242 | }
243 | }
244 |
245 |
246 | - (void)configDefaultIndicator{
247 | self.isDefaultSet = YES;
248 | if (self.indicatorViews.count) {
249 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
250 |
251 | CGPoint offset = self.sourceScrollView.contentOffset;
252 | CGFloat rate = offset.x / self.sourceScrollView.bounds.size.width;
253 | NSInteger currentIndex = (NSInteger)ceilf(rate);
254 | if (currentIndex > self.numberOfPages - 1) {
255 | currentIndex --;
256 | }
257 | UIView *currentIndicator = self.indicatorViews[currentIndex];
258 | self.currentPage = currentIndex;
259 | if (_pageStyle == LCScaleColorPageStyle) {
260 | currentIndicator.layer.timeOffset = 1.0f;
261 | }
262 | else if (_pageStyle == LCDepthColorPageStyle){
263 | currentIndicator.layer.timeOffset = 0.0f;
264 | if (currentIndex) {
265 | NSLayoutConstraint *currentCon = self.indicatorCons[currentIndex];
266 | NSLayoutConstraint *lastCon = self.indicatorCons.firstObject;
267 | currentCon.constant = (_indicatorDiameter * _indicatorMultiple * kLCHalfNumber);
268 | lastCon.constant = (_indicatorDiameter * _indicatorMultiple * kLCHalfNumber) + currentIndex * (_indicatorDiameter * _indicatorMultiple + _indicatorMargin);
269 | }
270 | }
271 | else if (_pageStyle == LCFillColorPageStyle){
272 | [(IndicatorView *)currentIndicator frontView].layer.timeOffset = 1.0f;
273 | }
274 | self.isDefaultSet = NO;
275 | });
276 | }
277 | }
278 |
279 |
280 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
281 |
282 | NSValue *oldOffsetValue = change[NSKeyValueChangeOldKey];
283 | CGPoint oldOffset = [oldOffsetValue CGPointValue];
284 |
285 | NSValue *newOffsetValue = change[NSKeyValueChangeNewKey];
286 | CGPoint newOffset = [newOffsetValue CGPointValue];
287 |
288 | CGFloat scrollViewWidth = [(UIScrollView *)object bounds].size.width;
289 |
290 | CGFloat rate = newOffset.x / scrollViewWidth;
291 | if (rate >= 0.0f && rate <= _numberOfPages - 1) {
292 |
293 | NSInteger currentIndex = (NSInteger)ceilf(rate);
294 | NSInteger lastIndex = (NSInteger)floorf(rate);
295 | if (currentIndex == lastIndex && currentIndex >= 1) {
296 | lastIndex -= 1;
297 | }
298 |
299 | UIView *currentPointView = self.indicatorViews[currentIndex];
300 | UIView *lastPointView = self.indicatorViews[lastIndex];
301 | CGFloat timeOffset = rate - floorf(rate);
302 | if (timeOffset == 0.0f && currentIndex) {
303 | timeOffset = 1.0f;
304 | }
305 |
306 | BOOL isNoAnimationScroll = NO;
307 | if ((NSInteger)newOffset.x % (NSInteger)scrollViewWidth == 0 &&
308 | (NSInteger)oldOffset.x % (NSInteger)scrollViewWidth == 0 &&
309 | newOffset.x != oldOffset.x &&
310 | (NSInteger)ABS(newOffset.x - oldOffset.x)) {
311 | CGFloat oldRate = oldOffset.x / scrollViewWidth;
312 | lastIndex = (NSInteger)ceilf(oldRate);
313 | if (lastIndex <= _numberOfPages - 1) {
314 | lastPointView = self.indicatorViews[lastIndex];
315 | isNoAnimationScroll = YES;
316 | }
317 | }
318 |
319 | if (!_sourceScrollView.decelerating && _isDefaultSet) {
320 | return;
321 | }
322 | if (_pageStyle == LCScaleColorPageStyle) {
323 | if (isNoAnimationScroll) {
324 | timeOffset = 1.0f;
325 | }
326 | currentPointView.layer.timeOffset = timeOffset;
327 | lastPointView.layer.timeOffset = 1.0f - timeOffset;
328 | }
329 | else if (_pageStyle == LCDepthColorPageStyle){
330 |
331 | UIView *lastPointView = self.indicatorViews.firstObject;
332 | lastIndex = 0;
333 | CGFloat halfTimeOffset = 0.0f;
334 | if (timeOffset - kLCHalfNumber <= 0.0f) {
335 | halfTimeOffset = timeOffset * kLCDoubleNumber;
336 | }
337 | else{
338 | halfTimeOffset = (CGFloat)ABS(timeOffset - 1.0f) * kLCDoubleNumber;
339 | }
340 |
341 | currentPointView.layer.timeOffset = halfTimeOffset;
342 | lastPointView.layer.timeOffset = halfTimeOffset;
343 | NSLayoutConstraint *currentCon = self.indicatorCons[currentIndex];
344 | NSLayoutConstraint *lastCon = self.indicatorCons[lastIndex];
345 | if (isNoAnimationScroll) {
346 | CGFloat oldRate = oldOffset.x / scrollViewWidth;
347 | lastIndex = (NSInteger)ceilf(oldRate);
348 | if (lastIndex > currentIndex) {
349 | currentCon = self.indicatorCons[currentIndex + 1];
350 | }
351 | currentCon.constant = (_indicatorDiameter * _indicatorMultiple * kLCHalfNumber) + (currentIndex - (lastIndex > currentIndex ? -1 : 1)) * (_indicatorDiameter * _indicatorMultiple + _indicatorMargin);
352 | }
353 | else{
354 | currentCon.constant = (_indicatorDiameter * _indicatorMultiple * kLCHalfNumber) + (currentIndex - timeOffset) * (_indicatorDiameter * _indicatorMultiple + _indicatorMargin);
355 | }
356 | lastCon.constant = (_indicatorDiameter * _indicatorMultiple * kLCHalfNumber) + (timeOffset + (currentIndex ? : 1 ) - 1) * (_indicatorDiameter * _indicatorMultiple + _indicatorMargin);
357 | }
358 | else if (_pageStyle == LCSquirmPageStyle){
359 |
360 | if (timeOffset - kLCHalfNumber <= 0.0f) {
361 | timeOffset = timeOffset * kLCDoubleNumber;
362 | }
363 | else{
364 | timeOffset = (CGFloat)ABS(timeOffset - 1.0f) * kLCDoubleNumber;
365 | }
366 | CGFloat number = (_indicatorMargin - _indicatorDiameter) * kLCHalfNumber;
367 | self.squirmCenterCon.constant = rate * kLCDoubleNumber * (_indicatorMargin - number) + _indicatorDiameter * kLCHalfNumber;
368 | self.squirmWidthCon.constant = timeOffset * (_indicatorDiameter + _indicatorMargin);
369 |
370 | }
371 | else if (_pageStyle == LCFillColorPageStyle){
372 | if (isNoAnimationScroll) {
373 | timeOffset = 1.0f;
374 | }
375 | [(IndicatorView *)currentPointView frontView].layer.timeOffset = timeOffset;
376 | [(IndicatorView *)lastPointView frontView].layer.timeOffset = 1.0f - timeOffset;
377 | }
378 | self.currentPage = currentIndex;
379 | }
380 | }
381 |
382 |
383 | - (void)layoutSquirmView{
384 | [self.contentView bringSubviewToFront:_squirmView];
385 | self.squirmView.hidden = !_numberOfPages;
386 | self.squirmCenterCon = [NSLayoutConstraint constraintWithItem:self.squirmView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:-_indicatorDiameter * kLCHalfNumber];
387 | [self.contentView addConstraints:@[
388 | self.squirmCenterCon,
389 | [NSLayoutConstraint constraintWithItem:self.squirmView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f]
390 | ]];
391 | if (!_squirmWidthCon) {
392 | [self.squirmView addConstraint:[NSLayoutConstraint constraintWithItem:self.squirmView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:self.indicatorDiameter]];
393 |
394 | self.squirmWidthCon = [NSLayoutConstraint constraintWithItem:self.squirmView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.squirmView attribute:NSLayoutAttributeHeight multiplier:1.0f constant:0.0f];
395 | [self.squirmView addConstraint:self.squirmWidthCon];
396 | }
397 | }
398 |
399 |
400 | - (void)layoutContentView{
401 | [self.indicatorCons removeAllObjects];
402 | [self.indicatorViews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
403 | view.translatesAutoresizingMaskIntoConstraints = NO;
404 | // size
405 | [view addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:_indicatorDiameter]];
406 | [view addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeHeight multiplier:1.0f constant:0.0f]];
407 | // position
408 | [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f]];
409 | NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:(_indicatorDiameter * _indicatorMultiple * kLCHalfNumber) + idx * (_indicatorDiameter * _indicatorMultiple + _indicatorMargin)];
410 | [self.indicatorCons addObject:con];
411 | [self.contentView addConstraint:con];
412 | }];
413 | }
414 |
415 |
416 | - (void)configCSAnimation:(UIView *)view{
417 | [self configColorAnimation:view];
418 | [self configScaleAnimation:view];
419 | }
420 |
421 |
422 | - (void)configColorAnimation:(UIView *)view{
423 | CABasicAnimation *changeColor = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
424 | changeColor.fromValue = (id)[self.pageIndicatorColor CGColor];
425 | changeColor.toValue = (id)[self.currentPageIndicatorColor CGColor];
426 | changeColor.duration = 1.0;
427 | changeColor.removedOnCompletion = NO;
428 | [view.layer addAnimation:changeColor forKey:@"Change color"];
429 | view.layer.speed = 0.0;
430 | }
431 |
432 | - (void)configScaleAnimation:(UIView *)view{
433 | CABasicAnimation *changeScale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
434 | changeScale.fromValue = @1.0f;
435 | changeScale.toValue = @(_indicatorMultiple);
436 | changeScale.duration = 1.0;
437 | changeScale.removedOnCompletion = NO;
438 | [view.layer addAnimation:changeScale forKey:@"Change scale"];
439 | view.layer.speed = 0.0;
440 | }
441 |
442 | - (void)configSmallScaleAnimation:(UIView *)view{
443 | CABasicAnimation *changeScale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
444 | changeScale.fromValue = @1.0f;
445 | changeScale.toValue = @(1/_indicatorMultiple);
446 | changeScale.duration = 1.0;
447 | changeScale.removedOnCompletion = NO;
448 | [view.layer addAnimation:changeScale forKey:@"Change scale small"];
449 | view.layer.speed = 0.0;
450 | }
451 |
452 | - (void)configZeroScaleAnimation:(UIView *)view{
453 | CABasicAnimation *changeScale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
454 | changeScale.fromValue = @1.0f;
455 | changeScale.toValue = @0.0f;
456 | changeScale.duration = 1.0;
457 | changeScale.removedOnCompletion = NO;
458 | [view.layer addAnimation:changeScale forKey:@"Change scale zero"];
459 | view.layer.speed = 0.0;
460 | }
461 |
462 |
463 | - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
464 | [super touchesEnded:touches withEvent:event];
465 | UITouch *touch = [touches anyObject];
466 | CGPoint point = [touch locationInView:self.contentView];
467 | CGFloat currentX = (_indicatorMultiple * _indicatorDiameter + _indicatorMargin) * _currentPage + _indicatorMultiple * _indicatorDiameter * kLCHalfNumber;
468 | if (point.x > currentX) {
469 | self.currentPage++;
470 | }
471 | else{
472 | if (_currentPage) {
473 | self.currentPage--;
474 | }
475 | }
476 | [self setCurrentPage:_currentPage sendEvent:YES];
477 | }
478 |
479 |
480 | - (void)dealloc{
481 | [self.sourceScrollView removeObserver:self forKeyPath:@"contentOffset"];
482 | }
483 |
484 | @end
485 |
--------------------------------------------------------------------------------
/Example/LCAnimatedPageControlExample/LCAnimatedPageControl.m:
--------------------------------------------------------------------------------
1 | //
2 | // LCAnimatedPageControl.m
3 | // LCAnimatedPageControl
4 | //
5 | // Created by beike on 6/17/15.
6 | // Copyright (c) 2015 beike. All rights reserved.
7 | //
8 |
9 | #import "LCAnimatedPageControl.h"
10 | #import "IndicatorView.h"
11 |
12 | static CGFloat kLCDoubleNumber = 2.0f;
13 | static CGFloat kLCHalfNumber = 0.5f;
14 | static CGFloat kLCMultiple = 1.4f;
15 |
16 | @interface LCAnimatedPageControl ()
17 |
18 | @property (nonatomic, strong) NSMutableArray *indicatorViews;
19 | @property (nonatomic, strong) NSMutableArray *indicatorCons;
20 | @property (nonatomic, assign) NSInteger currentPage;
21 | @property (nonatomic, strong) UIView *contentView;
22 | @property (nonatomic, assign) BOOL isDefaultSet;
23 | @property (nonatomic, assign) CGFloat radius;
24 | @property (nonatomic, strong) NSLayoutConstraint *contentWidthCon;
25 | @property (nonatomic, strong) NSLayoutConstraint *squirmCenterCon;
26 | @property (nonatomic, strong) NSLayoutConstraint *squirmWidthCon;
27 | @property (nonatomic, strong) UIView *squirmView;
28 |
29 | @end
30 |
31 | @implementation LCAnimatedPageControl
32 |
33 | - (instancetype)init{
34 | self = [super init];
35 | if (self) {
36 | [self initialize];
37 | }
38 | return self;
39 | }
40 |
41 | - (instancetype)initWithFrame:(CGRect)frame{
42 | self = [super initWithFrame:frame];
43 | if (self) {
44 | [self initialize];
45 | }
46 | return self;
47 | }
48 |
49 | - (void)initialize{
50 |
51 | _contentView = [[UIView alloc] init];
52 | self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
53 | [self addSubview:_contentView];
54 | [self addConstraints:@[
55 | [NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f],
56 | [NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f]
57 | ]];
58 | _indicatorViews = [NSMutableArray array];
59 | _indicatorCons = [NSMutableArray array];
60 | _numberOfPages = 0;
61 | _currentPage = 0;
62 | _pageIndicatorColor = [UIColor orangeColor];
63 | _currentPageIndicatorColor = [UIColor blackColor];
64 | _indicatorMultiple = kLCMultiple;
65 | _indicatorDiameter = self.frame.size.height / _indicatorMultiple;
66 | _indicatorMargin = 0.0f;
67 | _radius = _indicatorDiameter * kLCHalfNumber;
68 |
69 | }
70 |
71 | - (void)prepareShow{
72 |
73 | [self addIndicatorsWithIndex:0];
74 | [self.sourceScrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
75 | CGFloat viewWidth = (_indicatorDiameter * _indicatorMultiple) * _numberOfPages + (_numberOfPages - 1) * _indicatorMargin;
76 | self.contentWidthCon = [NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0f constant:viewWidth];
77 | [self.contentView addConstraint:_contentWidthCon];
78 | [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0f constant:_indicatorDiameter * _indicatorMultiple]];
79 |
80 | if (_pageStyle == LCScaleColorPageStyle) {
81 | [self configDefaultIndicator];
82 | }
83 | else if (_pageStyle == LCDepthColorPageStyle){
84 | [self configDefaultIndicator];
85 | [self.indicatorViews.firstObject setBackgroundColor:_currentPageIndicatorColor];
86 | [self.contentView bringSubviewToFront:self.indicatorViews.firstObject];
87 | }
88 | else if(_pageStyle == LCSquirmPageStyle){
89 | self.squirmView = [[UIView alloc] init];
90 | self.squirmView.translatesAutoresizingMaskIntoConstraints = NO;
91 | self.squirmView .clipsToBounds = YES;
92 | self.squirmView .layer.cornerRadius = _radius;
93 | self.squirmView .backgroundColor = _currentPageIndicatorColor;
94 | [self.contentView addSubview:_squirmView];
95 |
96 | [self layoutSquirmView];
97 | }
98 | else if (_pageStyle == LCFillColorPageStyle){
99 | [self configDefaultIndicator];
100 | }
101 | }
102 |
103 | - (void)setPageStyle:(PageStyle)pageStyle{
104 | _pageStyle = pageStyle;
105 | if (_pageStyle == LCSquirmPageStyle) {
106 | self.indicatorMultiple = 1.0f;
107 | }
108 | }
109 |
110 |
111 | - (void)clearIndicators{
112 | for (UIView *view in _indicatorViews) {
113 | if (view.layer.timeOffset < 1.0f) {
114 | view.layer.timeOffset = 0.0f;
115 | }
116 | }
117 | }
118 |
119 | - (void)setIndicatorDiameter:(CGFloat)indicatorDiameter{
120 | _indicatorDiameter = indicatorDiameter;
121 | self.radius = _indicatorDiameter * kLCHalfNumber;
122 | }
123 |
124 | - (void)addIndicatorsWithIndex:(NSInteger)index{
125 | for (NSInteger number = index; number < _numberOfPages; number ++ ) {
126 | UIView *indicator = nil;
127 | if (_pageStyle == LCFillColorPageStyle) {
128 | indicator = [[IndicatorView alloc] init];
129 | [(IndicatorView *)indicator backView].backgroundColor = _currentPageIndicatorColor;
130 | [(IndicatorView *)indicator frontView].backgroundColor = _pageIndicatorColor;
131 | [(IndicatorView *)indicator backView].layer.cornerRadius = _radius;
132 | [(IndicatorView *)indicator frontView].layer.cornerRadius = _radius;
133 | [self configZeroScaleAnimation:[(IndicatorView *)indicator backView]];
134 | [self configZeroScaleAnimation:[(IndicatorView *)indicator frontView]];
135 | }
136 | else{
137 | indicator = [[UIView alloc] init];
138 | indicator.backgroundColor = _pageIndicatorColor;
139 | }
140 | indicator.clipsToBounds = YES;
141 | indicator.layer.cornerRadius = _radius;
142 |
143 | [self.contentView addSubview:indicator];
144 | [self.indicatorViews addObject:indicator];
145 | if (_pageStyle == LCScaleColorPageStyle) {
146 | [self configCSAnimation:indicator];
147 | }
148 | else if(_pageStyle == LCDepthColorPageStyle){
149 | if (number == 0) {
150 | [self configScaleAnimation:indicator];
151 | }
152 | else{
153 | [self configSmallScaleAnimation:indicator];
154 | }
155 | }
156 | }
157 | [self layoutContentView];
158 | }
159 |
160 |
161 | - (void)removeIndicatorsWithNumber:(NSInteger)number{
162 | NSArray *array = [self.indicatorViews subarrayWithRange:NSMakeRange(0, ABS(number))];
163 | [array makeObjectsPerformSelector:@selector(removeFromSuperview)];
164 | [self.indicatorViews removeObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, ABS(number))]];
165 | [self layoutContentView];
166 | }
167 |
168 |
169 | - (void)setCurrentPage:(NSInteger)currentPage{
170 | [self setCurrentPage:currentPage sendEvent:NO];
171 | }
172 |
173 | - (void)setCurrentPage:(NSInteger)currentPage sendEvent:(BOOL)sendEvent{
174 | _currentPage = MIN(currentPage, _numberOfPages - 1);
175 | if (sendEvent) {
176 | [self sendActionsForControlEvents:UIControlEventValueChanged];
177 | }
178 | }
179 |
180 | - (void)setNumberOfPages:(NSInteger)numberOfPages{
181 | numberOfPages = MAX(0, numberOfPages);
182 | NSInteger difference = numberOfPages - _numberOfPages;
183 | NSInteger lastNumberPages = _numberOfPages;
184 | _numberOfPages = numberOfPages;
185 | if (difference && self.superview) {
186 | [self.contentView removeConstraints:_contentView.constraints];
187 | // remove
188 | if (difference < 0) {
189 | if (_currentPage != lastNumberPages - 1) {
190 | [self configCurrentIndicator];
191 | }
192 | if (_currentPage > _numberOfPages - 1) {
193 | _currentPage = _numberOfPages - 1;
194 | }
195 | [self removeIndicatorsWithNumber:difference];
196 | }
197 | // add
198 | else{
199 | [self addIndicatorsWithIndex:lastNumberPages];
200 | }
201 | [self resetContentLayout];
202 | if (_pageStyle == LCDepthColorPageStyle) {
203 | [self configDepthView];
204 | }
205 | else if (_pageStyle == LCSquirmPageStyle) {
206 | [self layoutSquirmView];
207 | }
208 | }
209 | }
210 |
211 | - (void)configCurrentIndicator{
212 | UIView *view = self.indicatorViews[_currentPage];
213 | if ([view isKindOfClass:[IndicatorView class]]) {
214 | [(IndicatorView *)view frontView].layer.timeOffset = 1.0f;
215 | }
216 | else{
217 | view.layer.timeOffset = 0.0f;
218 | }
219 | }
220 |
221 |
222 | - (void)resetContentLayout{
223 | if (_numberOfPages) {
224 | CGFloat viewWidth = (_indicatorDiameter * _indicatorMultiple) * _numberOfPages + (_numberOfPages - 1) * _indicatorMargin;
225 | self.contentWidthCon = [NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0f constant:viewWidth];
226 | [self.contentView addConstraint:_contentWidthCon];
227 | [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0f constant:_indicatorDiameter * _indicatorMultiple]];
228 | }
229 | }
230 |
231 |
232 | - (void)configDepthView{
233 | UIView *depthView = self.indicatorViews.firstObject;
234 | [depthView.layer removeAllAnimations];
235 | depthView.backgroundColor = _currentPageIndicatorColor;
236 | [self.contentView bringSubviewToFront:depthView];
237 | [self configScaleAnimation:depthView];
238 |
239 | for (NSInteger index = 1; index <= _currentPage; index ++) {
240 | NSLayoutConstraint *con = self.indicatorCons[index];
241 | con.constant = (_indicatorDiameter * _indicatorMultiple * kLCHalfNumber) + (index - 1) * (_indicatorDiameter * _indicatorMultiple + _indicatorMargin);
242 | }
243 | }
244 |
245 |
246 | - (void)configDefaultIndicator{
247 | self.isDefaultSet = YES;
248 | if (self.indicatorViews.count) {
249 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
250 |
251 | CGPoint offset = self.sourceScrollView.contentOffset;
252 | CGFloat rate = offset.x / self.sourceScrollView.bounds.size.width;
253 | NSInteger currentIndex = (NSInteger)ceilf(rate);
254 | if (currentIndex > self.numberOfPages - 1) {
255 | currentIndex --;
256 | }
257 | UIView *currentIndicator = self.indicatorViews[currentIndex];
258 | self.currentPage = currentIndex;
259 | if (_pageStyle == LCScaleColorPageStyle) {
260 | currentIndicator.layer.timeOffset = 1.0f;
261 | }
262 | else if (_pageStyle == LCDepthColorPageStyle){
263 | currentIndicator.layer.timeOffset = 0.0f;
264 | if (currentIndex) {
265 | NSLayoutConstraint *currentCon = self.indicatorCons[currentIndex];
266 | NSLayoutConstraint *lastCon = self.indicatorCons.firstObject;
267 | currentCon.constant = (_indicatorDiameter * _indicatorMultiple * kLCHalfNumber);
268 | lastCon.constant = (_indicatorDiameter * _indicatorMultiple * kLCHalfNumber) + currentIndex * (_indicatorDiameter * _indicatorMultiple + _indicatorMargin);
269 | }
270 | }
271 | else if (_pageStyle == LCFillColorPageStyle){
272 | [(IndicatorView *)currentIndicator frontView].layer.timeOffset = 1.0f;
273 | }
274 | self.isDefaultSet = NO;
275 | });
276 | }
277 | }
278 |
279 |
280 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
281 |
282 | NSValue *oldOffsetValue = change[NSKeyValueChangeOldKey];
283 | CGPoint oldOffset = [oldOffsetValue CGPointValue];
284 |
285 | NSValue *newOffsetValue = change[NSKeyValueChangeNewKey];
286 | CGPoint newOffset = [newOffsetValue CGPointValue];
287 |
288 | CGFloat scrollViewWidth = [(UIScrollView *)object bounds].size.width;
289 |
290 | CGFloat rate = newOffset.x / scrollViewWidth;
291 | if (rate >= 0.0f && rate <= _numberOfPages - 1) {
292 |
293 | NSInteger currentIndex = (NSInteger)ceilf(rate);
294 | NSInteger lastIndex = (NSInteger)floorf(rate);
295 | if (currentIndex == lastIndex && currentIndex >= 1) {
296 | lastIndex -= 1;
297 | }
298 |
299 | UIView *currentPointView = self.indicatorViews[currentIndex];
300 | UIView *lastPointView = self.indicatorViews[lastIndex];
301 | CGFloat timeOffset = rate - floorf(rate);
302 | if (timeOffset == 0.0f && currentIndex) {
303 | timeOffset = 1.0f;
304 | }
305 |
306 | BOOL isNoAnimationScroll = NO;
307 | if ((NSInteger)newOffset.x % (NSInteger)scrollViewWidth == 0 &&
308 | (NSInteger)oldOffset.x % (NSInteger)scrollViewWidth == 0 &&
309 | newOffset.x != oldOffset.x &&
310 | (NSInteger)ABS(newOffset.x - oldOffset.x)) {
311 | CGFloat oldRate = oldOffset.x / scrollViewWidth;
312 | lastIndex = (NSInteger)ceilf(oldRate);
313 | if (lastIndex <= _numberOfPages - 1) {
314 | lastPointView = self.indicatorViews[lastIndex];
315 | isNoAnimationScroll = YES;
316 | }
317 | }
318 |
319 | if (!_sourceScrollView.decelerating && _isDefaultSet) {
320 | return;
321 | }
322 | if (_pageStyle == LCScaleColorPageStyle) {
323 | if (isNoAnimationScroll) {
324 | timeOffset = 1.0f;
325 | }
326 | currentPointView.layer.timeOffset = timeOffset;
327 | lastPointView.layer.timeOffset = 1.0f - timeOffset;
328 | }
329 | else if (_pageStyle == LCDepthColorPageStyle){
330 |
331 | UIView *lastPointView = self.indicatorViews.firstObject;
332 | lastIndex = 0;
333 | CGFloat halfTimeOffset = 0.0f;
334 | if (timeOffset - kLCHalfNumber <= 0.0f) {
335 | halfTimeOffset = timeOffset * kLCDoubleNumber;
336 | }
337 | else{
338 | halfTimeOffset = (CGFloat)ABS(timeOffset - 1.0f) * kLCDoubleNumber;
339 | }
340 |
341 | currentPointView.layer.timeOffset = halfTimeOffset;
342 | lastPointView.layer.timeOffset = halfTimeOffset;
343 | NSLayoutConstraint *currentCon = self.indicatorCons[currentIndex];
344 | NSLayoutConstraint *lastCon = self.indicatorCons[lastIndex];
345 | if (isNoAnimationScroll) {
346 | CGFloat oldRate = oldOffset.x / scrollViewWidth;
347 | lastIndex = (NSInteger)ceilf(oldRate);
348 | if (lastIndex > currentIndex) {
349 | currentCon = self.indicatorCons[currentIndex + 1];
350 | }
351 | currentCon.constant = (_indicatorDiameter * _indicatorMultiple * kLCHalfNumber) + (currentIndex - (lastIndex > currentIndex ? -1 : 1)) * (_indicatorDiameter * _indicatorMultiple + _indicatorMargin);
352 | }
353 | else{
354 | currentCon.constant = (_indicatorDiameter * _indicatorMultiple * kLCHalfNumber) + (currentIndex - timeOffset) * (_indicatorDiameter * _indicatorMultiple + _indicatorMargin);
355 | }
356 | lastCon.constant = (_indicatorDiameter * _indicatorMultiple * kLCHalfNumber) + (timeOffset + (currentIndex ? : 1 ) - 1) * (_indicatorDiameter * _indicatorMultiple + _indicatorMargin);
357 | }
358 | else if (_pageStyle == LCSquirmPageStyle){
359 |
360 | if (timeOffset - kLCHalfNumber <= 0.0f) {
361 | timeOffset = timeOffset * kLCDoubleNumber;
362 | }
363 | else{
364 | timeOffset = (CGFloat)ABS(timeOffset - 1.0f) * kLCDoubleNumber;
365 | }
366 | CGFloat number = (_indicatorMargin - _indicatorDiameter) * kLCHalfNumber;
367 | self.squirmCenterCon.constant = rate * kLCDoubleNumber * (_indicatorMargin - number) + _indicatorDiameter * kLCHalfNumber;
368 | self.squirmWidthCon.constant = timeOffset * (_indicatorDiameter + _indicatorMargin);
369 |
370 | }
371 | else if (_pageStyle == LCFillColorPageStyle){
372 | if (isNoAnimationScroll) {
373 | timeOffset = 1.0f;
374 | }
375 | [(IndicatorView *)currentPointView frontView].layer.timeOffset = timeOffset;
376 | [(IndicatorView *)lastPointView frontView].layer.timeOffset = 1.0f - timeOffset;
377 | }
378 | self.currentPage = currentIndex;
379 | }
380 | }
381 |
382 |
383 | - (void)layoutSquirmView{
384 | [self.contentView bringSubviewToFront:_squirmView];
385 | self.squirmView.hidden = !_numberOfPages;
386 | self.squirmCenterCon = [NSLayoutConstraint constraintWithItem:self.squirmView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:-_indicatorDiameter * kLCHalfNumber];
387 | [self.contentView addConstraints:@[
388 | self.squirmCenterCon,
389 | [NSLayoutConstraint constraintWithItem:self.squirmView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f]
390 | ]];
391 | if (!_squirmWidthCon) {
392 | [self.squirmView addConstraint:[NSLayoutConstraint constraintWithItem:self.squirmView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:self.indicatorDiameter]];
393 |
394 | self.squirmWidthCon = [NSLayoutConstraint constraintWithItem:self.squirmView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.squirmView attribute:NSLayoutAttributeHeight multiplier:1.0f constant:0.0f];
395 | [self.squirmView addConstraint:self.squirmWidthCon];
396 | }
397 | }
398 |
399 |
400 | - (void)layoutContentView{
401 | [self.indicatorCons removeAllObjects];
402 | [self.indicatorViews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
403 | view.translatesAutoresizingMaskIntoConstraints = NO;
404 | // size
405 | [view addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:_indicatorDiameter]];
406 | [view addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeHeight multiplier:1.0f constant:0.0f]];
407 | // position
408 | [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f]];
409 | NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:(_indicatorDiameter * _indicatorMultiple * kLCHalfNumber) + idx * (_indicatorDiameter * _indicatorMultiple + _indicatorMargin)];
410 | [self.indicatorCons addObject:con];
411 | [self.contentView addConstraint:con];
412 | }];
413 | }
414 |
415 |
416 | - (void)configCSAnimation:(UIView *)view{
417 | [self configColorAnimation:view];
418 | [self configScaleAnimation:view];
419 | }
420 |
421 |
422 | - (void)configColorAnimation:(UIView *)view{
423 | CABasicAnimation *changeColor = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
424 | changeColor.fromValue = (id)[self.pageIndicatorColor CGColor];
425 | changeColor.toValue = (id)[self.currentPageIndicatorColor CGColor];
426 | changeColor.duration = 1.0;
427 | changeColor.removedOnCompletion = NO;
428 | [view.layer addAnimation:changeColor forKey:@"Change color"];
429 | view.layer.speed = 0.0;
430 | }
431 |
432 | - (void)configScaleAnimation:(UIView *)view{
433 | CABasicAnimation *changeScale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
434 | changeScale.fromValue = @1.0f;
435 | changeScale.toValue = @(_indicatorMultiple);
436 | changeScale.duration = 1.0;
437 | changeScale.removedOnCompletion = NO;
438 | [view.layer addAnimation:changeScale forKey:@"Change scale"];
439 | view.layer.speed = 0.0;
440 | }
441 |
442 | - (void)configSmallScaleAnimation:(UIView *)view{
443 | CABasicAnimation *changeScale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
444 | changeScale.fromValue = @1.0f;
445 | changeScale.toValue = @(1/_indicatorMultiple);
446 | changeScale.duration = 1.0;
447 | changeScale.removedOnCompletion = NO;
448 | [view.layer addAnimation:changeScale forKey:@"Change scale small"];
449 | view.layer.speed = 0.0;
450 | }
451 |
452 | - (void)configZeroScaleAnimation:(UIView *)view{
453 | CABasicAnimation *changeScale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
454 | changeScale.fromValue = @1.0f;
455 | changeScale.toValue = @0.0f;
456 | changeScale.duration = 1.0;
457 | changeScale.removedOnCompletion = NO;
458 | [view.layer addAnimation:changeScale forKey:@"Change scale zero"];
459 | view.layer.speed = 0.0;
460 | }
461 |
462 |
463 | - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
464 | [super touchesEnded:touches withEvent:event];
465 | UITouch *touch = [touches anyObject];
466 | CGPoint point = [touch locationInView:self.contentView];
467 | CGFloat currentX = (_indicatorMultiple * _indicatorDiameter + _indicatorMargin) * _currentPage + _indicatorMultiple * _indicatorDiameter * kLCHalfNumber;
468 | if (point.x > currentX) {
469 | self.currentPage++;
470 | }
471 | else{
472 | if (_currentPage) {
473 | self.currentPage--;
474 | }
475 | }
476 | [self setCurrentPage:_currentPage sendEvent:YES];
477 | }
478 |
479 |
480 | - (void)dealloc{
481 | [self.sourceScrollView removeObserver:self forKeyPath:@"contentOffset"];
482 | }
483 |
484 | @end
485 |
--------------------------------------------------------------------------------