Merge pull request #1 from iznv/remove_text_from_dot

Remove text from dot
This commit is contained in:
Ivan Zinovyev 2018-11-27 12:28:57 +03:00 committed by GitHub
commit 15bcbce38b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 42 additions and 91 deletions

View File

@ -8,19 +8,19 @@
/** /**
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2017 Looping Copyright (c) 2017 Looping
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@ -57,7 +57,6 @@
@property (nonatomic) NSInteger currentDisplayedPage; @property (nonatomic) NSInteger currentDisplayedPage;
@property (nonatomic) NSInteger previousDisplayPage; @property (nonatomic) NSInteger previousDisplayPage;
@property (nonatomic) UILabel *indicatorIndexLabel;
@end @end
@ -73,99 +72,70 @@
if (self = [super initWithFrame:frame]) { if (self = [super initWithFrame:frame]) {
[self commConfig]; [self commConfig];
} }
return self; return self;
} }
- (instancetype)initWithNumberOfPages:(NSInteger)pages { - (instancetype)initWithNumberOfPages:(NSInteger)pages {
RCPageControl *pageControl = [self init]; RCPageControl *pageControl = [self init];
if (pageControl) { if (pageControl) {
[pageControl setNumberOfPages:pages]; [pageControl setNumberOfPages:pages];
} }
return pageControl; return pageControl;
} }
- (void)awakeFromNib { - (void)awakeFromNib {
[super awakeFromNib]; [super awakeFromNib];
[self commConfig]; [self commConfig];
} }
- (void)commConfig { - (void)commConfig {
_currentDisplayedPage = 0; _currentDisplayedPage = 0;
_previousDisplayPage = 0; _previousDisplayPage = 0;
_numberOfPages = 0; _numberOfPages = 0;
_currentPage = 0; _currentPage = 0;
_indicatorDotGap = 10.f; _indicatorDotGap = 10.f;
_indicatorDotWidth = 4.f; _indicatorDotWidth = 4.f;
_animationSpeed = 8.f; _animationSpeed = 8.f;
_animationBounciness = 12.f; _animationBounciness = 12.f;
_animationDuration = .6f; _animationDuration = .6f;
_animationScaleFactor = 2; _animationScaleFactor = 2;
_hidesForSinglePage = NO; _hidesForSinglePage = NO;
_defersCurrentPageDisplay = NO; _defersCurrentPageDisplay = NO;
_hideCurrentPageIndex = NO; _hideCurrentPageIndex = NO;
_disableAnimation = NO; _disableAnimation = NO;
_pageIndicatorTintColor = [UIColor lightTextColor]; _pageIndicatorTintColor = [UIColor lightTextColor];
_currentPageIndicatorTintColor = [UIColor whiteColor]; _currentPageIndicatorTintColor = [UIColor whiteColor];
_currentPageIndexTextTintColor = [UIColor darkTextColor]; _currentPageIndexTextTintColor = [UIColor darkTextColor];
_currentPageIndexTextFont = [UIFont systemFontOfSize:0]; _currentPageIndexTextFont = [UIFont systemFontOfSize:0];
[self loadIndicatorIndexLabel];
[self setBackgroundColor:[UIColor clearColor]]; [self setBackgroundColor:[UIColor clearColor]];
} }
- (void)loadIndicatorIndexLabel {
CGFloat width = MAX(RCDefaultIndicatorDotIndexDisplayMinWidth, [self _scaledDotMaxWidth]);
if (_indicatorIndexLabel) {
[_indicatorIndexLabel setFrame:CGRectMake(0, 0, width, width)];
} else {
_indicatorIndexLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, width)];
[_indicatorIndexLabel setTextAlignment:NSTextAlignmentCenter];
[_indicatorIndexLabel setBackgroundColor:[UIColor clearColor]];
}
[_indicatorIndexLabel setTextColor:_currentPageIndexTextTintColor];
[_indicatorIndexLabel setFont:[_currentPageIndexTextFont fontWithSize:[self _scaledDotMaxWidth] * 2 / 3]];
[_indicatorIndexLabel setHidden:_hideCurrentPageIndex];
UIView *dot = [self _currentDisplayedDot];
if (dot) {
[_indicatorIndexLabel setCenter:dot.center];
} else {
[_indicatorIndexLabel setHidden:YES];
}
}
#pragma mark - Properties #pragma mark - Properties
- (void)setNumberOfPages:(NSInteger)numberOfPages { - (void)setNumberOfPages:(NSInteger)numberOfPages {
if (numberOfPages >= 0 && numberOfPages != _numberOfPages) { if (numberOfPages >= 0 && numberOfPages != _numberOfPages) {
_numberOfPages = numberOfPages; _numberOfPages = numberOfPages;
[self _refreshIndicator:YES]; [self _refreshIndicator:YES];
} }
} }
- (void)setCurrentPage:(NSInteger)currentPage forceRefresh:(BOOL)forceRefresh { - (void)setCurrentPage:(NSInteger)currentPage forceRefresh:(BOOL)forceRefresh {
_previousDisplayPage = _currentPage; _previousDisplayPage = _currentPage;
_currentPage = MIN(MAX(0, currentPage), _numberOfPages - 1); _currentPage = MIN(MAX(0, currentPage), _numberOfPages - 1);
if ( !self.defersCurrentPageDisplay || forceRefresh) { if ( !self.defersCurrentPageDisplay || forceRefresh) {
_currentDisplayedPage = _currentPage; _currentDisplayedPage = _currentPage;
[self _animateIndicator:forceRefresh]; [self _animateIndicator:forceRefresh];
@ -178,21 +148,20 @@
- (void)setIndicatorDotGap:(CGFloat)indicatorDotGap { - (void)setIndicatorDotGap:(CGFloat)indicatorDotGap {
CGFloat gap = MAX(RCDefaultIndicatorDotGapMinValue, indicatorDotGap); CGFloat gap = MAX(RCDefaultIndicatorDotGapMinValue, indicatorDotGap);
if ( !IsFloatEqualToFloat(_indicatorDotGap, gap)) { if ( !IsFloatEqualToFloat(_indicatorDotGap, gap)) {
_indicatorDotGap = gap; _indicatorDotGap = gap;
[self _refreshIndicator:YES]; [self _refreshIndicator:YES];
} }
} }
- (void)setIndicatorDotWidth:(CGFloat)indicatorDotWidth { - (void)setIndicatorDotWidth:(CGFloat)indicatorDotWidth {
CGFloat width = MAX(RCDefaultIndicatorDotWidthMinValue, indicatorDotWidth); CGFloat width = MAX(RCDefaultIndicatorDotWidthMinValue, indicatorDotWidth);
if ( !IsFloatEqualToFloat(_indicatorDotWidth, width)) { if ( !IsFloatEqualToFloat(_indicatorDotWidth, width)) {
_indicatorDotWidth = width; _indicatorDotWidth = width;
[self loadIndicatorIndexLabel];
[self _refreshIndicator:YES]; [self _refreshIndicator:YES];
} }
} }
@ -200,8 +169,7 @@
- (void)setAnimationScaleFactor:(NSInteger)animationScaleFactor { - (void)setAnimationScaleFactor:(NSInteger)animationScaleFactor {
if ( _animationScaleFactor != animationScaleFactor) { if ( _animationScaleFactor != animationScaleFactor) {
_animationScaleFactor = MAX(RCDefaultIndicatorDotScaleFactorMinValue, animationScaleFactor); _animationScaleFactor = MAX(RCDefaultIndicatorDotScaleFactorMinValue, animationScaleFactor);
[self loadIndicatorIndexLabel];
[self _dotScaleAnimationAtIndex:_currentDisplayedPage withProgress:RCDefaultIndicatorDotChangeProgressMaxValue]; [self _dotScaleAnimationAtIndex:_currentDisplayedPage withProgress:RCDefaultIndicatorDotChangeProgressMaxValue];
} }
} }
@ -223,7 +191,6 @@
- (void)setHideCurrentPageIndex:(BOOL)hideCurrentPageIndex { - (void)setHideCurrentPageIndex:(BOOL)hideCurrentPageIndex {
if (_hideCurrentPageIndex != hideCurrentPageIndex) { if (_hideCurrentPageIndex != hideCurrentPageIndex) {
_hideCurrentPageIndex = hideCurrentPageIndex; _hideCurrentPageIndex = hideCurrentPageIndex;
[self loadIndicatorIndexLabel];
} }
} }
@ -244,14 +211,12 @@
- (void)setCurrentPageIndexTintColor:(UIColor *)currentPageIndexTintColor { - (void)setCurrentPageIndexTintColor:(UIColor *)currentPageIndexTintColor {
if ( ![_currentPageIndexTextTintColor isEqual:currentPageIndexTintColor]) { if ( ![_currentPageIndexTextTintColor isEqual:currentPageIndexTintColor]) {
_currentPageIndexTextTintColor = currentPageIndexTintColor; _currentPageIndexTextTintColor = currentPageIndexTintColor;
[self loadIndicatorIndexLabel];
} }
} }
- (void)setCurrentPageIndexTextFont:(UIFont *)currentPageIndexTextFont { - (void)setCurrentPageIndexTextFont:(UIFont *)currentPageIndexTextFont {
if ( ![_currentPageIndexTextFont isEqual:currentPageIndexTextFont]) { if ( ![_currentPageIndexTextFont isEqual:currentPageIndexTextFont]) {
_currentPageIndexTextFont = currentPageIndexTextFont; _currentPageIndexTextFont = currentPageIndexTextFont;
[self loadIndicatorIndexLabel];
} }
} }
@ -263,7 +228,7 @@
} else { } else {
[self setCurrentPage:self.currentPage + 1 forceRefresh:NO]; [self setCurrentPage:self.currentPage + 1 forceRefresh:NO];
} }
if (_currentPageChangedBlock) { if (_currentPageChangedBlock) {
_currentPageChangedBlock(self); _currentPageChangedBlock(self);
} else { } else {
@ -323,9 +288,9 @@
UIView *dot = [self _dotAtIndex:index]; UIView *dot = [self _dotAtIndex:index];
[dot pop_removeAnimationForKey:RCDefaultIndicatorScaleAnimationKey]; [dot pop_removeAnimationForKey:RCDefaultIndicatorScaleAnimationKey];
POPPropertyAnimation *animation; POPPropertyAnimation *animation;
if (_disableAnimation) { if (_disableAnimation) {
animation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewScaleXY]; animation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewScaleXY];
((POPBasicAnimation *)animation).duration = RCDefaultIndicatorDotAnimationDurationMinValue; ((POPBasicAnimation *)animation).duration = RCDefaultIndicatorDotAnimationDurationMinValue;
@ -334,10 +299,10 @@
((POPSpringAnimation *)animation).springSpeed = _animationSpeed; ((POPSpringAnimation *)animation).springSpeed = _animationSpeed;
((POPSpringAnimation *)animation).springBounciness = _animationBounciness; ((POPSpringAnimation *)animation).springBounciness = _animationBounciness;
} }
[animation setRemovedOnCompletion:YES]; [animation setRemovedOnCompletion:YES];
animation.toValue = toValue; animation.toValue = toValue;
[dot pop_addAnimation:animation forKey:RCDefaultIndicatorScaleAnimationKey]; [dot pop_addAnimation:animation forKey:RCDefaultIndicatorScaleAnimationKey];
} }
@ -345,12 +310,12 @@
UIView *dot = [self _dotAtIndex:index]; UIView *dot = [self _dotAtIndex:index];
[dot pop_removeAnimationForKey:RCDefaultIndicatorColorAnimationKey]; [dot pop_removeAnimationForKey:RCDefaultIndicatorColorAnimationKey];
POPBasicAnimation *animation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewBackgroundColor]; POPBasicAnimation *animation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewBackgroundColor];
[animation setRemovedOnCompletion:YES]; [animation setRemovedOnCompletion:YES];
animation.toValue = toValue; animation.toValue = toValue;
animation.duration = _disableAnimation ? RCDefaultIndicatorDotAnimationDurationMinValue : _animationDuration; animation.duration = _disableAnimation ? RCDefaultIndicatorDotAnimationDurationMinValue : _animationDuration;
[dot pop_addAnimation:animation forKey:RCDefaultIndicatorColorAnimationKey]; [dot pop_addAnimation:animation forKey:RCDefaultIndicatorColorAnimationKey];
} }
@ -366,25 +331,17 @@
if (toPage >= 0 && fromPage >= 0) { if (toPage >= 0 && fromPage >= 0) {
[self _dotScaleAnimationAtIndex:fromPage withProgress:1 - progress]; [self _dotScaleAnimationAtIndex:fromPage withProgress:1 - progress];
[self _dotScaleAnimationAtIndex:toPage withProgress:progress]; [self _dotScaleAnimationAtIndex:toPage withProgress:progress];
[self _dotColorAnimationAtIndex:fromPage withProgress:1 - progress]; [self _dotColorAnimationAtIndex:fromPage withProgress:1 - progress];
[self _dotColorAnimationAtIndex:toPage withProgress:progress]; [self _dotColorAnimationAtIndex:toPage withProgress:progress];
BOOL hidden = ![self _dotAtIndex:toPage] || ([self _scaledDotMaxWidth] < RCDefaultIndicatorDotIndexDisplayMinWidth) || (progress < 1 - RCDefaultIndicatorDotChangeProgressMinValue) || _hideCurrentPageIndex;
[_indicatorIndexLabel setHidden:hidden];
if ( !hidden) {
[self bringSubviewToFront:_indicatorIndexLabel];
[_indicatorIndexLabel setCenter:[self _dotAtIndex:toPage].center]; BOOL hidden = ![self _dotAtIndex:toPage] || ([self _scaledDotMaxWidth] < RCDefaultIndicatorDotIndexDisplayMinWidth) || (progress < 1 - RCDefaultIndicatorDotChangeProgressMinValue) || _hideCurrentPageIndex;
[_indicatorIndexLabel setText:[NSString stringWithFormat:@"%@", @(toPage + 1)]];
if ( !hidden) {
POPBasicAnimation *alphaAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha]; POPBasicAnimation *alphaAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
alphaAnimation.fromValue = @(0.f); alphaAnimation.fromValue = @(0.f);
alphaAnimation.toValue = @(1.f); alphaAnimation.toValue = @(1.f);
alphaAnimation.duration = _disableAnimation ? 0 : _animationDuration; alphaAnimation.duration = _disableAnimation ? 0 : _animationDuration;
[_indicatorIndexLabel pop_addAnimation:alphaAnimation forKey:RCDefaultIndicatorIndexLabelAlphaAnimationKey];
} }
} }
} }
@ -400,16 +357,16 @@
- (void)_refreshIndicator:(BOOL)forceRefresh { - (void)_refreshIndicator:(BOOL)forceRefresh {
if ( !(_hidesForSinglePage && _numberOfPages <= 1)) { if ( !(_hidesForSinglePage && _numberOfPages <= 1)) {
[self setHidden:NO]; [self setHidden:NO];
if (forceRefresh || self.subviews.count != _numberOfPages) { if (forceRefresh || self.subviews.count != _numberOfPages) {
CGPoint position = [self positionForNumberOfPages:self.numberOfPages]; CGPoint position = [self positionForNumberOfPages:self.numberOfPages];
NSInteger index = 0; NSInteger index = 0;
for (; index < _numberOfPages; index ++) { for (; index < _numberOfPages; index ++) {
CGRect frame = CGRectMake(position.x + index * (_indicatorDotGap + _indicatorDotWidth), position.y, _indicatorDotWidth, _indicatorDotWidth); CGRect frame = CGRectMake(position.x + index * (_indicatorDotGap + _indicatorDotWidth), position.y, _indicatorDotWidth, _indicatorDotWidth);
UIView *dot = [self _dotAtIndex:index] ?: [[UIView alloc] initWithFrame:frame]; UIView *dot = [self _dotAtIndex:index] ?: [[UIView alloc] initWithFrame:frame];
if ([UIView respondsToSelector:@selector(performWithoutAnimation:)]) { if ([UIView respondsToSelector:@selector(performWithoutAnimation:)]) {
[UIView performWithoutAnimation:^{ [UIView performWithoutAnimation:^{
[dot setTransform:CGAffineTransformIdentity]; [dot setTransform:CGAffineTransformIdentity];
@ -420,32 +377,26 @@
[dot setTransform:CGAffineTransformIdentity]; [dot setTransform:CGAffineTransformIdentity];
[UIView setAnimationsEnabled:reenableAnimations]; [UIView setAnimationsEnabled:reenableAnimations];
} }
[dot setTag:[self _dotTagAtIndex:index]]; [dot setTag:[self _dotTagAtIndex:index]];
[dot setBackgroundColor:_pageIndicatorTintColor]; [dot setBackgroundColor:_pageIndicatorTintColor];
[dot.layer setMasksToBounds:YES]; [dot.layer setMasksToBounds:YES];
[dot.layer setCornerRadius:dot.frame.size.height / 2]; [dot.layer setCornerRadius:dot.frame.size.height / 2];
if ( !dot.superview) { if ( !dot.superview) {
[self addSubview:dot]; [self addSubview:dot];
} else { } else {
[dot setFrame:frame]; [dot setFrame:frame];
} }
} }
for (; self.subviews.count && index < self.subviews.count - 1; index ++) { for (; self.subviews.count && index < self.subviews.count - 1; index ++) {
[[self _dotAtIndex:index] removeFromSuperview]; [[self _dotAtIndex:index] removeFromSuperview];
} }
[self _animateIndicator:forceRefresh]; [self _animateIndicator:forceRefresh];
} }
if ( !_indicatorIndexLabel.superview) {
[self addSubview:_indicatorIndexLabel];
}
[self bringSubviewToFront:_indicatorIndexLabel];
} else { } else {
[self setHidden:YES]; [self setHidden:YES];
} }