Permalink
Browse files

Add SYPageControl

  • Loading branch information...
soffes committed Mar 20, 2012
1 parent 044ba76 commit 88d29166e79b8bd17529645ea743fc7b805ad935
@@ -16,6 +16,8 @@
B2C4DFAF1509631500ABF21F /* SYPaginatorView.m in Sources */ = {isa = PBXBuildFile; fileRef = B2C4DFAA1509631500ABF21F /* SYPaginatorView.m */; };
B2C4DFB01509631500ABF21F /* SYPaginatorViewController.h in Headers */ = {isa = PBXBuildFile; fileRef = B2C4DFAB1509631500ABF21F /* SYPaginatorViewController.h */; };
B2C4DFB11509631500ABF21F /* SYPaginatorViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B2C4DFAC1509631500ABF21F /* SYPaginatorViewController.m */; };
+ B2D195FA1518FA41006BB5AD /* SYPageControl.h in Headers */ = {isa = PBXBuildFile; fileRef = B2D195F81518FA41006BB5AD /* SYPageControl.h */; };
+ B2D195FB1518FA41006BB5AD /* SYPageControl.m in Sources */ = {isa = PBXBuildFile; fileRef = B2D195F91518FA41006BB5AD /* SYPageControl.m */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
@@ -30,6 +32,8 @@
B2C4DFAA1509631500ABF21F /* SYPaginatorView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SYPaginatorView.m; sourceTree = "<group>"; };
B2C4DFAB1509631500ABF21F /* SYPaginatorViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SYPaginatorViewController.h; sourceTree = "<group>"; };
B2C4DFAC1509631500ABF21F /* SYPaginatorViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SYPaginatorViewController.m; sourceTree = "<group>"; };
+ B2D195F81518FA41006BB5AD /* SYPageControl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SYPageControl.h; sourceTree = "<group>"; };
+ B2D195F91518FA41006BB5AD /* SYPageControl.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SYPageControl.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -81,6 +85,8 @@
B275DFEE1509783500D6E7C1 /* SYPageView.m */,
B275DFF11509788C00D6E7C1 /* SYPaginatorScrollView.h */,
B275DFF21509788C00D6E7C1 /* SYPaginatorScrollView.m */,
+ B2D195F81518FA41006BB5AD /* SYPageControl.h */,
+ B2D195F91518FA41006BB5AD /* SYPageControl.m */,
);
path = SYPaginator;
sourceTree = "<group>";
@@ -96,6 +102,7 @@
B2C4DFB01509631500ABF21F /* SYPaginatorViewController.h in Headers */,
B275DFEF1509783500D6E7C1 /* SYPageView.h in Headers */,
B275DFF31509788C00D6E7C1 /* SYPaginatorScrollView.h in Headers */,
+ B2D195FA1518FA41006BB5AD /* SYPageControl.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -154,6 +161,7 @@
B2C4DFB11509631500ABF21F /* SYPaginatorViewController.m in Sources */,
B275DFF01509783500D6E7C1 /* SYPageView.m in Sources */,
B275DFF41509788C00D6E7C1 /* SYPaginatorScrollView.m in Sources */,
+ B2D195FB1518FA41006BB5AD /* SYPageControl.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -0,0 +1,20 @@
+//
+// SYPageControl.h
+// SYPaginator
+//
+// Created by Sam Soffes on 3/20/12.
+// Copyright (c) 2012 Synthetic. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@interface SYPageControl : UIControl
+
+@property(nonatomic) NSInteger numberOfPages;
+@property(nonatomic) NSInteger currentPage;
+@property(nonatomic) BOOL hidesForSinglePage;
+
+@property (nonatomic, strong, readonly) UIPageControl *pageControl;
+@property (nonatomic, strong, readonly) UILabel *textLabel;
+
+@end
View
@@ -0,0 +1,122 @@
+//
+// SYPageControl.m
+// SYPaginator
+//
+// Created by Sam Soffes on 3/20/12.
+// Copyright (c) 2012 Synthetic. All rights reserved.
+//
+
+#import "SYPageControl.h"
+
+static NSInteger const kSYPageControlMaxNumberOfDots = 12;
+
+@interface SYPageControl ()
+- (void)_updateTextLabel;
+- (void)_pageControlChanged:(id)sender;
+- (void)_labelTapped:(UITapGestureRecognizer *)gestureRecognizer;
+@end
+
+@implementation SYPageControl
+
+@synthesize numberOfPages = _numberOfPages;
+@synthesize currentPage = _currentPage;
+@synthesize hidesForSinglePage = _hidesForSinglePage;
+@synthesize pageControl = _pageControl;
+@synthesize textLabel = _textLabel;
+
+- (void)setNumberOfPages:(NSInteger)numberOfPages {
+ _numberOfPages = numberOfPages;
+ if (numberOfPages == 1 && _hidesForSinglePage) {
+ self.hidden = YES;
+ [_pageControl removeFromSuperview];
+ [_textLabel removeFromSuperview];
+ return;
+ }
+
+ self.hidden = NO;
+
+ if (numberOfPages <= kSYPageControlMaxNumberOfDots) {
+ _pageControl.numberOfPages = numberOfPages;
+ if (!_pageControl.superview) {
+ [self addSubview:_pageControl];
+ }
+ [_textLabel removeFromSuperview];
+ } else {
+ _pageControl.numberOfPages = 0;
+ [_pageControl removeFromSuperview];
+
+ [self _updateTextLabel];
+ if (!_textLabel.superview) {
+ [self addSubview:_textLabel];
+ }
+ }
+
+ [self setNeedsLayout];
+}
+
+
+- (void)setCurrentPage:(NSInteger)currentPage {
+ currentPage = (NSInteger)fminf(fmaxf(0.0f, (CGFloat)currentPage), (CGFloat)_numberOfPages - 1.0f);
+ if (currentPage == _currentPage) {
+ return;
+ }
+
+ _currentPage = currentPage;
+
+ if (_numberOfPages <= kSYPageControlMaxNumberOfDots) {
+ _pageControl.currentPage = currentPage;
+ } else {
+ [self _updateTextLabel];
+ }
+}
+
+
+#pragma mark - UIView
+
+- (id)initWithFrame:(CGRect)frame {
+ if ((self = [super initWithFrame:frame])) {
+ _pageControl = [[UIPageControl alloc] initWithFrame:CGRectZero];
+ [_pageControl addTarget:self action:@selector(_pageControlChanged:) forControlEvents:UIControlEventValueChanged];
+
+ _textLabel = [[UILabel alloc] initWithFrame:CGRectZero];
+ _textLabel.font = [UIFont boldSystemFontOfSize:12.0f];
+ _textLabel.textColor = [UIColor whiteColor];
+ _textLabel.backgroundColor = [UIColor clearColor];
+ _textLabel.textAlignment = UITextAlignmentCenter;
+ _textLabel.shadowColor = [UIColor colorWithWhite:0.0f alpha:0.5f];
+ _textLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
+ _textLabel.userInteractionEnabled = YES;
+
+ UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_labelTapped:)];
+ [_textLabel addGestureRecognizer:tap];
+ }
+ return self;
+}
+
+
+- (void)layoutSubviews {
+ _pageControl.frame = self.bounds;
+ _textLabel.frame = self.bounds;
+}
+
+
+#pragma mark - Private
+
+- (void)_updateTextLabel {
+ _textLabel.text = [NSString stringWithFormat:@"%i of %i", _currentPage + 1, _numberOfPages];
+}
+
+
+- (void)_pageControlChanged:(id)sender {
+ self.currentPage = _pageControl.currentPage;
+ [self sendActionsForControlEvents:UIControlEventValueChanged];
+}
+
+
+- (void)_labelTapped:(UITapGestureRecognizer *)gestureRecognizer {
+ BOOL increment = [gestureRecognizer locationInView:self].x >= self.bounds.size.width / 2.0f;
+ self.currentPage += increment ? 1 : -1;
+ [self sendActionsForControlEvents:UIControlEventValueChanged];
+}
+
+@end
@@ -9,3 +9,4 @@
#import <SYPaginator/SYPaginatorView.h>
#import <SYPaginator/SYPaginatorViewController.h>
#import <SYPaginator/SYPageView.h>
+#import <SYPaginator/SYPageControl.h>
@@ -18,6 +18,7 @@ typedef enum {
@protocol SYPaginatorViewDataSource;
@protocol SYPaginatorViewDelegate;
@class SYPageView;
+@class SYPageControl;
/**
This class manages a paging UIScrollView and a UIPageControl.
@@ -32,7 +33,7 @@ typedef enum {
// UI
@property (nonatomic, strong, readonly) UIScrollView *scrollView;
-@property (nonatomic, strong, readonly) UIPageControl *pageControl;
+@property (nonatomic, strong, readonly) SYPageControl *pageControl;
@property (nonatomic, assign) NSUInteger currentPageIndex;
@property (nonatomic, assign, readonly) NSUInteger numberOfPages;
@@ -8,6 +8,7 @@
#import "SYPaginatorView.h"
#import "SYPageView.h"
+#import "SYPageControl.h"
#import "SYPaginatorScrollView.h"
@interface SYPaginatorView () <UIScrollViewDelegate>
@@ -91,7 +92,7 @@ - (id)initWithFrame:(CGRect)frame {
// Page control
CGSize size = self.bounds.size;
- _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0.0f, size.height - 18.0f, size.width, 18.0f)];
+ _pageControl = [[SYPageControl alloc] initWithFrame:CGRectMake(0.0f, size.height - 18.0f, size.width, 18.0f)];
_pageControl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
_pageControl.currentPage = 0;
[_pageControl addTarget:self action:@selector(_pageControlChanged:) forControlEvents:UIControlEventValueChanged];
@@ -143,14 +144,7 @@ - (void)reloadDataRemovingCurrentPage:(BOOL)removeCurrentPage {
NSUInteger numberOfPages = [self numberOfPages];
CGSize size = _scrollView.bounds.size;
_scrollView.contentSize = CGSizeMake(size.width * numberOfPages, size.height);
-
- if (numberOfPages <= 10) {
- _pageControl.numberOfPages = (NSInteger)numberOfPages;
-// _pageControl.hidden = NO;
- } else {
- _pageControl.numberOfPages = 0;
-// _pageControl.hidden = YES;
- }
+ _pageControl.numberOfPages = (NSInteger)numberOfPages;
// Setup views
if (!_pages) {
@@ -353,10 +347,7 @@ - (void)_setCurrentPageIndex:(NSUInteger)targetPage animated:(BOOL)animated scro
if (_currentPageIndex != targetPage || [self pageForIndex:targetPage] == nil) {
_currentPageIndex = targetPage;
-
- if (!_pageControl.hidden) {
- _pageControl.currentPage = (NSInteger)targetPage;
- }
+ _pageControl.currentPage = (NSInteger)targetPage;
[self _loadPage:targetPage];
[self _loadPagesToPreloadAroundPageAtIndex:targetPage];

0 comments on commit 88d2916

Please sign in to comment.