Initial commit

This commit is contained in:
ifish
2017-08-15 16:59:01 +08:00
commit bdc83e07ef
5766 changed files with 423223 additions and 0 deletions
@@ -0,0 +1,46 @@
//
// WMMenuItem.h
// WMPageController
//
// Created by Mark on 15/4/26.
// Copyright (c) 2015年 yq. All rights reserved.
//
#import <UIKit/UIKit.h>
@class WMMenuItem;
typedef NS_ENUM(NSUInteger, WMMenuItemState) {
WMMenuItemStateSelected,
WMMenuItemStateNormal,
};
@protocol WMMenuItemDelegate <NSObject>
@optional
- (void)didPressedMenuItem:(WMMenuItem *)menuItem;
@end
@interface WMMenuItem : UILabel
/** 设置rate,并刷新标题状态 */
@property (nonatomic, assign) CGFloat rate;
/** normal状态的字体大小,默认大小为15 */
@property (nonatomic, assign) CGFloat normalSize;
/** selected状态的字体大小,默认大小为18 */
@property (nonatomic, assign) CGFloat selectedSize;
/** normal状态的字体颜色,默认为黑色 (可动画) */
@property (nonatomic, strong) UIColor *normalColor;
/** selected状态的字体颜色,默认为红色 (可动画) */
@property (nonatomic, strong) UIColor *selectedColor;
@property (nonatomic, assign, getter=isSelected) BOOL selected;
@property (nonatomic, weak) id<WMMenuItemDelegate> delegate;
/** 进度条的速度因数,默认为 15,越小越快, 大于 0 */
@property (nonatomic, assign) CGFloat speedFactor;
- (void)selectedWithoutAnimation;
- (void)deselectedWithoutAnimation;
@end
@@ -0,0 +1,120 @@
//
// WMMenuItem.m
// WMPageController
//
// Created by Mark on 15/4/26.
// Copyright (c) 2015年 yq. All rights reserved.
//
#import "WMMenuItem.h"
@interface WMMenuItem () {
CGFloat _selectedRed, _selectedGreen, _selectedBlue, _selectedAlpha;
CGFloat _normalRed, _normalGreen, _normalBlue, _normalAlpha;
int _sign;
CGFloat _gap;
CGFloat _step;
__weak CADisplayLink *_link;
}
@end
@implementation WMMenuItem
#pragma mark - Public Methods
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.normalColor = [UIColor blackColor];
self.selectedColor = [UIColor blackColor];
self.normalSize = 15;
self.selectedSize = 18;
self.numberOfLines = 0;
}
return self;
}
- (CGFloat)speedFactor {
if (_speedFactor <= 0) {
_speedFactor = 15.0;
}
return _speedFactor;
}
// 设置选中,隐式动画所在
- (void)setSelected:(BOOL)selected {
if (self.selected == selected) { return; }
_selected = selected;
_sign = (selected == YES) ? 1 : -1;
_gap = (selected == YES) ? (1.0 - self.rate) : (self.rate - 0.0);
_step = _gap / self.speedFactor;
if (_link) {
[_link invalidate];
// [_link removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(rateChange)];
[link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
_link = link;
}
- (void)rateChange {
if (_gap > 0.000001) {
_gap -= _step;
if (_gap < 0.0) {
self.rate = (int)(self.rate + _sign * _step + 0.5);
return;
}
self.rate += _sign * _step;
} else {
self.rate = (int)(self.rate + 0.5);
[_link invalidate];
_link = nil;
}
}
// 设置rate,并刷新标题状态
- (void)setRate:(CGFloat)rate {
_rate = rate;
CGFloat r = _normalRed + (_selectedRed - _normalRed) * rate;
CGFloat g = _normalGreen + (_selectedGreen - _normalGreen) * rate;
CGFloat b = _normalBlue + (_selectedBlue - _normalBlue) * rate;
CGFloat a = _normalAlpha + (_selectedAlpha - _normalAlpha) * rate;
self.textColor = [UIColor colorWithRed:r green:g blue:b alpha:a];
CGFloat minScale = self.normalSize / self.selectedSize;
CGFloat trueScale = minScale + (1 - minScale)*rate;
self.transform = CGAffineTransformMakeScale(trueScale, trueScale);
}
- (void)selectedWithoutAnimation {
if (self.rate == 1.0 && _selected == YES) {
return;
}
self.rate = 1.0;
_selected = YES;
}
- (void)deselectedWithoutAnimation {
if (self.rate == 0.0 && _selected == NO) {
return;
}
self.rate = 0;
_selected = NO;
}
- (void)setSelectedColor:(UIColor *)selectedColor {
_selectedColor = selectedColor;
[selectedColor getRed:&_selectedRed green:&_selectedGreen blue:&_selectedBlue alpha:&_selectedAlpha];
}
- (void)setNormalColor:(UIColor *)normalColor {
_normalColor = normalColor;
[normalColor getRed:&_normalRed green:&_normalGreen blue:&_normalBlue alpha:&_normalAlpha];
}
#pragma mark - Private Methods
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if ([self.delegate respondsToSelector:@selector(didPressedMenuItem:)]) {
[self.delegate didPressedMenuItem:self];
}
}
@end
@@ -0,0 +1,113 @@
//
// WMMenuView.h
// WMPageController
//
// Created by Mark on 15/4/26.
// Copyright (c) 2015年 yq. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "WMMenuItem.h"
#import "WMProgressView.h"
@class WMMenuView;
typedef NS_ENUM(NSUInteger, WMMenuViewStyle) {
WMMenuViewStyleDefault, // 默认
WMMenuViewStyleLine, // 带下划线 (若要选中字体大小不变,设置选中和非选中大小一样即可)
WMMenuViewStyleTriangle, // 三角形 (progressHeight 为三角形的高, progressWidths 为底边长)
WMMenuViewStyleFlood, // 涌入效果 (填充)
WMMenuViewStyleFloodHollow, // 涌入效果 (空心的)
WMMenuViewStyleSegmented, // 涌入带边框,即网易新闻选项卡
};
// 原先基础上添加了几个方便布局的枚举,更多布局格式可以通过设置 `itemsMargins` 属性来自定义
// 以下布局均只在 item 个数较少的情况下生效,即无法滚动 MenuView 时.
typedef NS_ENUM(NSUInteger, WMMenuViewLayoutMode) {
WMMenuViewLayoutModeScatter, // 默认的布局模式, item 会均匀分布在屏幕上,呈分散状
WMMenuViewLayoutModeLeft, // Item 紧靠屏幕左侧
WMMenuViewLayoutModeRight, // Item 紧靠屏幕右侧
WMMenuViewLayoutModeCenter, // Item 紧挨且居中分布
};
@protocol WMMenuViewDelegate <NSObject>
@optional
- (void)menuView:(WMMenuView *)menu didSelesctedIndex:(NSInteger)index currentIndex:(NSInteger)currentIndex;
- (CGFloat)menuView:(WMMenuView *)menu widthForItemAtIndex:(NSInteger)index;
- (CGFloat)menuView:(WMMenuView *)menu itemMarginAtIndex:(NSInteger)index;
- (CGFloat)menuView:(WMMenuView *)menu titleSizeForState:(WMMenuItemState)state;
- (UIColor *)menuView:(WMMenuView *)menu titleColorForState:(WMMenuItemState)state;
- (void)menuView:(WMMenuView *)menu didLayoutItemFrame:(WMMenuItem *)menuItem atIndex:(NSInteger)index;
@end
@protocol WMMenuViewDataSource <NSObject>
@required
- (NSInteger)numbersOfTitlesInMenuView:(WMMenuView *)menu;
- (NSString *)menuView:(WMMenuView *)menu titleAtIndex:(NSInteger)index;
@optional
/**
* 角标 (例如消息提醒的小红点) 的数据源方法,在 WMPageController 中实现这个方法来为 menuView 提供一个 badgeView
需要在返回的时候同时设置角标的 frame 属性,该 frame 为相对于 menuItem 的位置
*
* @param index 角标的序号
*
* @return 返回一个设置好 frame 的角标视图
*/
- (UIView *)menuView:(WMMenuView *)menu badgeViewAtIndex:(NSInteger)index;
/**
* 用于定制 WMMenuItem,可以对传出的 initialMenuItem 进行修改定制,也可以返回自己创建的子类,需要注意的是,此时的 item 的 frame 是不确定的,所以请勿根据此时的 frame 做计算!
如需根据 frame 修改,请使用代理
*
* @param menu 当前的 menuViewframe 也是不确定的
* @param initialMenuItem 初始化完成的 menuItem
* @param index Item 所属的位置;
*
* @return 定制完成的 MenuItem
*/
- (WMMenuItem *)menuView:(WMMenuView *)menu initialMenuItem:(WMMenuItem *)initialMenuItem atIndex:(NSInteger)index;
@end
@interface WMMenuView : UIView <WMMenuItemDelegate>
@property (nonatomic, strong) NSArray *progressWidths;
@property (nonatomic, weak) WMProgressView *progressView;
@property (nonatomic, assign) CGFloat progressHeight;
@property (nonatomic, assign) WMMenuViewStyle style;
@property (nonatomic, assign) WMMenuViewLayoutMode layoutMode;
@property (nonatomic, assign) CGFloat contentMargin;
@property (nonatomic, strong) UIColor *lineColor;
@property (nonatomic, assign) CGFloat progressViewBottomSpace;
@property (nonatomic, weak) id<WMMenuViewDelegate> delegate;
@property (nonatomic, weak) id<WMMenuViewDataSource> dataSource;
@property (nonatomic, weak) UIView *leftView;
@property (nonatomic, weak) UIView *rightView;
@property (nonatomic, copy) NSString *fontName;
@property (nonatomic, readonly) CGFloat selectedSize;
@property (nonatomic, readonly) CGFloat normalSize;
@property (nonatomic, readonly) UIColor *selectedColor;
@property (nonatomic, readonly) UIColor *normalColor;
@property (nonatomic, weak) UIScrollView *scrollView;
/** 进度条的速度因数,默认为 15,越小越快, 大于 0 */
@property (nonatomic, assign) CGFloat speedFactor;
@property (nonatomic, assign) CGFloat progressViewCornerRadius;
@property (nonatomic, assign) BOOL progressViewIsNaughty;
- (void)slideMenuAtProgress:(CGFloat)progress;
- (void)selectItemAtIndex:(NSInteger)index;
- (void)resetFrames;
- (void)reload;
- (void)updateTitle:(NSString *)title atIndex:(NSInteger)index andWidth:(BOOL)update;
- (WMMenuItem *)itemAtIndex:(NSInteger)index;
/// 立即刷新 menuView 的 contentOffset,使 title 居中
- (void)refreshContenOffset;
- (void)deselectedItemsIfNeeded;
/**
* 更新角标视图,如要移除,在 -menuView:badgeViewAtIndex: 中返回 nil 即可
*/
- (void)updateBadgeViewAtIndex:(NSInteger)index;
@end
@@ -0,0 +1,573 @@
//
// WMMenuView.m
// WMPageController
//
// Created by Mark on 15/4/26.
// Copyright (c) 2015年 yq. All rights reserved.
//
#import "WMMenuView.h"
@interface WMMenuView ()
@property (nonatomic, weak) WMMenuItem *selItem;
@property (nonatomic, strong) NSMutableArray *frames;
@property (nonatomic, readonly) NSInteger titlesCount;
@property (nonatomic, assign) NSInteger selectIndex;
@end
// 下划线的高度
static CGFloat const WMProgressHeight = 2.0;
static CGFloat const WMMenuItemWidth = 60.0;
static NSInteger const WMMenuItemTagOffset = 6250;
static NSInteger const WMBadgeViewTagOffset = 1212;
@implementation WMMenuView
#pragma mark - Setter
- (void)setLayoutMode:(WMMenuViewLayoutMode)layoutMode {
_layoutMode = layoutMode;
if (!self.superview) { return; }
[self reload];
}
- (void)setFrame:(CGRect)frame {
[super setFrame:frame];
if (!self.scrollView) { return; }
CGFloat leftMargin = self.contentMargin + self.leftView.frame.size.width;
CGFloat rightMargin = self.contentMargin + self.rightView.frame.size.width;
CGFloat contentWidth = self.scrollView.frame.size.width + leftMargin + rightMargin;
CGFloat startX = self.leftView ? self.leftView.frame.origin.x : self.scrollView.frame.origin.x - self.contentMargin;
// Make the contentView center, because system will change menuView's frame if it's a titleView.
if (startX + contentWidth / 2 != self.bounds.size.width / 2) {
CGFloat xOffset = (self.bounds.size.width - contentWidth) / 2;
self.leftView.frame = ({
CGRect frame = self.leftView.frame;
frame.origin.x = xOffset;
frame;
});
self.scrollView.frame = ({
CGRect frame = self.scrollView.frame;
frame.origin.x = self.leftView ? CGRectGetMaxX(self.leftView.frame) + self.contentMargin : xOffset;
frame;
});
self.rightView.frame = ({
CGRect frame = self.rightView.frame;
frame.origin.x = CGRectGetMaxX(self.scrollView.frame) + self.contentMargin;
frame;
});
}
}
- (void)setProgressViewCornerRadius:(CGFloat)progressViewCornerRadius {
_progressViewCornerRadius = progressViewCornerRadius;
if (self.progressView) {
self.progressView.cornerRadius = _progressViewCornerRadius;
}
}
- (void)setSpeedFactor:(CGFloat)speedFactor {
_speedFactor = speedFactor;
if (self.progressView) {
self.progressView.speedFactor = _speedFactor;
}
[self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:[WMMenuItem class]]) {
((WMMenuItem *)obj).speedFactor = _speedFactor;
}
}];
}
- (void)setProgressWidths:(NSArray *)progressWidths {
_progressWidths = progressWidths;
if (!self.progressView.superview) { return; }
[self resetFramesFromIndex:0];
}
- (void)setLeftView:(UIView *)leftView {
if (self.leftView) {
[self.leftView removeFromSuperview];
_leftView = nil;
}
if (leftView) {
[self addSubview:leftView];
_leftView = leftView;
}
[self resetFrames];
}
- (void)setRightView:(UIView *)rightView {
if (self.rightView) {
[self.rightView removeFromSuperview];
_rightView = nil;
}
if (rightView) {
[self addSubview:rightView];
_rightView = rightView;
}
[self resetFrames];
}
- (void)setContentMargin:(CGFloat)contentMargin {
_contentMargin = contentMargin;
if (self.scrollView) {
[self resetFrames];
}
}
#pragma mark - Getter
- (UIColor *)lineColor {
if (!_lineColor) {
_lineColor = self.selectedColor;
}
return _lineColor;
}
- (NSMutableArray *)frames {
if (_frames == nil) {
_frames = [NSMutableArray array];
}
return _frames;
}
- (UIColor *)selectedColor {
if ([self.delegate respondsToSelector:@selector(menuView:titleColorForState:)]) {
return [self.delegate menuView:self titleColorForState:WMMenuItemStateSelected];
}
return [UIColor blackColor];
}
- (UIColor *)normalColor {
if ([self.delegate respondsToSelector:@selector(menuView:titleColorForState:)]) {
return [self.delegate menuView:self titleColorForState:WMMenuItemStateNormal];
}
return [UIColor blackColor];
}
- (CGFloat)selectedSize {
if ([self.delegate respondsToSelector:@selector(menuView:titleSizeForState:)]) {
return [self.delegate menuView:self titleSizeForState:WMMenuItemStateSelected];
}
return 18.0;
}
- (CGFloat)normalSize {
if ([self.delegate respondsToSelector:@selector(menuView:titleSizeForState:)]) {
return [self.delegate menuView:self titleSizeForState:WMMenuItemStateNormal];
}
return 15.0;
}
- (UIView *)badgeViewAtIndex:(NSInteger)index {
if (![self.dataSource respondsToSelector:@selector(menuView:badgeViewAtIndex:)]) {
return nil;
}
UIView *badgeView = [self.dataSource menuView:self badgeViewAtIndex:index];
if (!badgeView) {
return nil;
}
badgeView.tag = index + WMBadgeViewTagOffset;
return badgeView;
}
#pragma mark - Public Methods
- (WMMenuItem *)itemAtIndex:(NSInteger)index {
return (WMMenuItem *)[self viewWithTag:(index + WMMenuItemTagOffset)];
}
- (void)setProgressViewIsNaughty:(BOOL)progressViewIsNaughty {
_progressViewIsNaughty = progressViewIsNaughty;
if (self.progressView) {
self.progressView.naughty = progressViewIsNaughty;
}
}
- (void)reload {
[self.frames removeAllObjects];
[self.progressView removeFromSuperview];
[self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj removeFromSuperview];
}];
[self addItems];
[self makeStyle];
[self addBadgeViews];
}
- (void)slideMenuAtProgress:(CGFloat)progress {
if (self.progressView) {
self.progressView.progress = progress;
}
NSInteger tag = (NSInteger)progress + WMMenuItemTagOffset;
CGFloat rate = progress - tag + WMMenuItemTagOffset;
WMMenuItem *currentItem = (WMMenuItem *)[self viewWithTag:tag];
WMMenuItem *nextItem = (WMMenuItem *)[self viewWithTag:tag+1];
if (rate == 0.0) {
[self.selItem deselectedWithoutAnimation];
self.selItem = currentItem;
[self.selItem selectedWithoutAnimation];
[self refreshContenOffset];
return;
}
currentItem.rate = 1-rate;
nextItem.rate = rate;
}
- (void)selectItemAtIndex:(NSInteger)index {
NSInteger tag = index + WMMenuItemTagOffset;
NSInteger currentIndex = self.selItem.tag - WMMenuItemTagOffset;
self.selectIndex = index;
if (index == currentIndex || !self.selItem) { return; }
WMMenuItem *item = (WMMenuItem *)[self viewWithTag:tag];
[self.selItem deselectedWithoutAnimation];
self.selItem = item;
[self.selItem selectedWithoutAnimation];
[self.progressView setProgressWithOutAnimate:index];
if ([self.delegate respondsToSelector:@selector(menuView:didSelesctedIndex:currentIndex:)]) {
[self.delegate menuView:self didSelesctedIndex:index currentIndex:currentIndex];
}
[self refreshContenOffset];
}
- (void)updateTitle:(NSString *)title atIndex:(NSInteger)index andWidth:(BOOL)update {
if (index >= self.titlesCount || index < 0) { return; }
WMMenuItem *item = (WMMenuItem *)[self viewWithTag:(WMMenuItemTagOffset + index)];
item.text = title;
if (!update) { return; }
[self resetFrames];
}
- (void)updateBadgeViewAtIndex:(NSInteger)index {
UIView *oldBadgeView = [self.scrollView viewWithTag:WMBadgeViewTagOffset + index];
if (oldBadgeView) {
[oldBadgeView removeFromSuperview];
}
[self addBadgeViewAtIndex:index];
[self resetBadgeFrame:index];
}
// 让选中的item位于中间
- (void)refreshContenOffset {
CGRect frame = self.selItem.frame;
CGFloat itemX = frame.origin.x;
CGFloat width = self.scrollView.frame.size.width;
CGSize contentSize = self.scrollView.contentSize;
if (itemX > width/2) {
CGFloat targetX;
if ((contentSize.width-itemX) <= width/2) {
targetX = contentSize.width - width;
} else {
targetX = frame.origin.x - width/2 + frame.size.width/2;
}
// 应该有更好的解决方法
if (targetX + width > contentSize.width) {
targetX = contentSize.width - width;
}
[self.scrollView setContentOffset:CGPointMake(targetX, 0) animated:YES];
} else {
[self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
}
#pragma mark - Data source
- (NSInteger)titlesCount {
return [self.dataSource numbersOfTitlesInMenuView:self];
}
#pragma mark - Private Methods
- (void)willMoveToSuperview:(UIView *)newSuperview {
if (self.scrollView) { return; }
[self addScrollView];
[self addItems];
[self makeStyle];
[self addBadgeViews];
if (self.selectIndex == 0) { return; }
[self selectItemAtIndex:self.selectIndex];
}
- (void)resetFrames {
CGRect frame = self.bounds;
if (self.rightView) {
CGRect rightFrame = self.rightView.frame;
rightFrame.origin.x = frame.size.width - rightFrame.size.width;
self.rightView.frame = rightFrame;
frame.size.width -= rightFrame.size.width;
}
if (self.leftView) {
CGRect leftFrame = self.leftView.frame;
leftFrame.origin.x = 0;
self.leftView.frame = leftFrame;
frame.origin.x += leftFrame.size.width;
frame.size.width -= leftFrame.size.width;
}
frame.origin.x += self.contentMargin;
frame.size.width -= self.contentMargin * 2;
self.scrollView.frame = frame;
[self resetFramesFromIndex:0];
// [self refreshContenOffset];
}
- (void)resetFramesFromIndex:(NSInteger)index {
[self.frames removeAllObjects];
[self calculateItemFrames];
for (NSInteger i = index; i < self.titlesCount; i++) {
[self resetItemFrame:i];
[self resetBadgeFrame:i];
}
if (!self.progressView.superview) { return; }
CGRect frame = self.progressView.frame;
frame.size.width = self.scrollView.contentSize.width;
if (self.style == WMMenuViewStyleLine || self.style == WMMenuViewStyleTriangle) {
frame.origin.y = self.frame.size.height - self.progressHeight - self.progressViewBottomSpace;
} else {
frame.origin.y = (self.scrollView.frame.size.height - frame.size.height) / 2.0;
}
self.progressView.frame = frame;
self.progressView.itemFrames = [self convertProgressWidthsToFrames];
[self.progressView setNeedsDisplay];
}
- (void)resetItemFrame:(NSInteger)index {
WMMenuItem *item = (WMMenuItem *)[self viewWithTag:(WMMenuItemTagOffset + index)];
CGRect frame = [self.frames[index] CGRectValue];
item.frame = frame;
if ([self.delegate respondsToSelector:@selector(menuView:didLayoutItemFrame:atIndex:)]) {
[self.delegate menuView:self didLayoutItemFrame:item atIndex:index];
}
}
- (void)resetBadgeFrame:(NSInteger)index {
CGRect frame = [self.frames[index] CGRectValue];
UIView *badgeView = [self.scrollView viewWithTag:(WMBadgeViewTagOffset + index)];
if (badgeView) {
CGRect badgeFrame = [self badgeViewAtIndex:index].frame;
badgeFrame.origin.x += frame.origin.x;
badgeView.frame = badgeFrame;
}
}
- (NSArray *)convertProgressWidthsToFrames {
if (!self.frames.count) { NSAssert(NO, @"BUUUUUUUG...SHOULDN'T COME HERE!!"); }
if (self.progressWidths.count < self.titlesCount) return self.frames;
NSMutableArray *progressFrames = [NSMutableArray array];
for (int i = 0; i < self.frames.count; i++) {
CGRect itemFrame = [self.frames[i] CGRectValue];
CGFloat progressWidth = [self.progressWidths[i] floatValue];
CGFloat x = itemFrame.origin.x + (itemFrame.size.width - progressWidth) / 2;
CGRect progressFrame = CGRectMake(x, itemFrame.origin.y, progressWidth, 0);
[progressFrames addObject:[NSValue valueWithCGRect:progressFrame]];
}
return progressFrames.copy;
}
- (void)addBadgeViews {
for (int i = 0; i < self.titlesCount; i++) {
[self addBadgeViewAtIndex:i];
}
}
- (void)addBadgeViewAtIndex:(NSInteger)index {
UIView *badgeView = [self badgeViewAtIndex:index];
if (badgeView) {
[self.scrollView addSubview:badgeView];
}
}
- (void)makeStyle {
CGRect frame = CGRectZero;
if (self.style == WMMenuViewStyleDefault) { return; }
if (self.style == WMMenuViewStyleLine) {
self.progressHeight = self.progressHeight > 0 ? self.progressHeight : WMProgressHeight;
frame = CGRectMake(0, self.frame.size.height - self.progressHeight - self.progressViewBottomSpace, self.scrollView.contentSize.width, self.progressHeight);
} else {
self.progressHeight = self.progressHeight > 0 ? self.progressHeight : self.frame.size.height * 0.8;
frame = CGRectMake(0, (self.frame.size.height - self.progressHeight) / 2, self.scrollView.contentSize.width, self.progressHeight);
self.progressViewCornerRadius = self.progressViewCornerRadius > 0 ? self.progressViewCornerRadius : self.progressHeight / 2.0;
}
[self wm_addProgressViewWithFrame:frame
isTriangle:(self.style == WMMenuViewStyleTriangle)
hasBorder:(self.style == WMMenuViewStyleSegmented)
hollow:(self.style == WMMenuViewStyleFloodHollow)
cornerRadius:self.progressViewCornerRadius];
}
- (void)deselectedItemsIfNeeded {
[self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (![obj isKindOfClass:[WMMenuItem class]] || obj == self.selItem) {
return;
}
[(WMMenuItem *)obj deselectedWithoutAnimation];
}];
}
- (void)addScrollView {
CGFloat width = self.frame.size.width - self.contentMargin * 2;
CGFloat height = self.frame.size.height;
CGRect frame = CGRectMake(self.contentMargin, 0, width, height);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:frame];
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.backgroundColor = [UIColor clearColor];
scrollView.scrollsToTop = NO;
[self addSubview:scrollView];
self.scrollView = scrollView;
}
- (void)addItems {
[self calculateItemFrames];
for (int i = 0; i < self.titlesCount; i++) {
CGRect frame = [self.frames[i] CGRectValue];
WMMenuItem *item = [[WMMenuItem alloc] initWithFrame:frame];
if (self.fontName) {
item.font = [UIFont fontWithName:self.fontName size:self.selectedSize];
} else {
item.font = [UIFont systemFontOfSize:self.selectedSize];
}
item.tag = (i+WMMenuItemTagOffset);
item.delegate = self;
item.text = [self.dataSource menuView:self titleAtIndex:i];
item.textAlignment = NSTextAlignmentCenter;
if ([self.dataSource respondsToSelector:@selector(menuView:initialMenuItem:atIndex:)]) {
item = [self.dataSource menuView:self initialMenuItem:item atIndex:i];
}
item.userInteractionEnabled = YES;
item.backgroundColor = [UIColor clearColor];
item.normalSize = self.normalSize;
item.selectedSize = self.selectedSize;
item.normalColor = self.normalColor;
item.selectedColor = self.selectedColor;
item.speedFactor = self.speedFactor;
if (i == 0) {
[item selectedWithoutAnimation];
self.selItem = item;
} else {
[item deselectedWithoutAnimation];
}
[self.scrollView addSubview:item];
}
}
// 计算所有item的frame值,主要是为了适配所有item的宽度之和小于屏幕宽的情况
// 这里与后面的 `-addItems` 做了重复的操作,并不是很合理
- (void)calculateItemFrames {
CGFloat contentWidth = [self itemMarginAtIndex:0];
for (int i = 0; i < self.titlesCount; i++) {
CGFloat itemW = WMMenuItemWidth;
if ([self.delegate respondsToSelector:@selector(menuView:widthForItemAtIndex:)]) {
itemW = [self.delegate menuView:self widthForItemAtIndex:i];
}
CGRect frame = CGRectMake(contentWidth, 0, itemW, self.frame.size.height);
// 记录frame
[self.frames addObject:[NSValue valueWithCGRect:frame]];
contentWidth += itemW + [self itemMarginAtIndex:i+1];
}
// 如果总宽度小于屏幕宽,重新计算frame,为item间添加间距
if (contentWidth < self.scrollView.frame.size.width) {
CGFloat distance = self.scrollView.frame.size.width - contentWidth;
CGFloat (^shiftDis)(int);
switch (self.layoutMode) {
case WMMenuViewLayoutModeScatter: {
CGFloat gap = distance / (self.titlesCount + 1);
shiftDis = ^CGFloat(int index) { return gap * (index + 1); };
break;
}
case WMMenuViewLayoutModeLeft: {
shiftDis = ^CGFloat(int index) { return 0.0; };
break;
}
case WMMenuViewLayoutModeRight: {
shiftDis = ^CGFloat(int index) { return distance; };
break;
}
case WMMenuViewLayoutModeCenter: {
shiftDis = ^CGFloat(int index) { return distance / 2; };
break;
}
}
for (int i = 0; i < self.frames.count; i++) {
CGRect frame = [self.frames[i] CGRectValue];
frame.origin.x += shiftDis(i);
self.frames[i] = [NSValue valueWithCGRect:frame];
}
contentWidth = self.scrollView.frame.size.width;
}
self.scrollView.contentSize = CGSizeMake(contentWidth, self.frame.size.height);
}
- (CGFloat)itemMarginAtIndex:(NSInteger)index {
if ([self.delegate respondsToSelector:@selector(menuView:itemMarginAtIndex:)]) {
return [self.delegate menuView:self itemMarginAtIndex:index];
}
return 0.0;
}
// MARK:Progress View
- (void)wm_addProgressViewWithFrame:(CGRect)frame isTriangle:(BOOL)isTriangle hasBorder:(BOOL)hasBorder hollow:(BOOL)isHollow cornerRadius:(CGFloat)cornerRadius {
WMProgressView *pView = [[WMProgressView alloc] initWithFrame:frame];
pView.itemFrames = [self convertProgressWidthsToFrames];
pView.color = self.lineColor.CGColor;
pView.isTriangle = isTriangle;
pView.hasBorder = hasBorder;
pView.hollow = isHollow;
pView.cornerRadius = cornerRadius;
pView.naughty = self.progressViewIsNaughty;
pView.speedFactor = self.speedFactor;
pView.backgroundColor = [UIColor clearColor];
self.progressView = pView;
[self.scrollView insertSubview:self.progressView atIndex:0];
}
#pragma mark - Menu item delegate
- (void)didPressedMenuItem:(WMMenuItem *)menuItem {
if (self.selItem == menuItem) return;
CGFloat progress = menuItem.tag - WMMenuItemTagOffset;
[self.progressView moveToPostion:progress];
NSInteger currentIndex = self.selItem.tag - WMMenuItemTagOffset;
if ([self.delegate respondsToSelector:@selector(menuView:didSelesctedIndex:currentIndex:)]) {
[self.delegate menuView:self didSelesctedIndex:menuItem.tag-WMMenuItemTagOffset currentIndex:currentIndex];
}
menuItem.selected = YES;
self.selItem.selected = NO;
self.selItem = menuItem;
NSTimeInterval delay = self.style == WMMenuViewStyleDefault ? 0 : 0.3f;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 让选中的item位于中间
[self refreshContenOffset];
});
}
@end
@@ -0,0 +1,28 @@
//
// WMProgressView.h
// WMPageController
//
// Created by Mark on 15/6/20.
// Copyright (c) 2015年 yq. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface WMProgressView : UIView
@property (nonatomic, strong) NSArray *itemFrames;
@property (nonatomic, assign) CGColorRef color;
@property (nonatomic, assign) CGFloat progress;
/** 进度条的速度因数,默认为 15,越小越快, 大于 0 */
@property (nonatomic, assign) CGFloat speedFactor;
@property (nonatomic, assign) CGFloat cornerRadius;
// 调皮属性,用于实现新腾讯视频效果
@property (nonatomic, assign) BOOL naughty;
@property (nonatomic, assign) BOOL isTriangle;
@property (nonatomic, assign) BOOL hollow;
@property (nonatomic, assign) BOOL hasBorder;
- (void)setProgressWithOutAnimate:(CGFloat)progress;
- (void)moveToPostion:(NSInteger)pos;
@end
@@ -0,0 +1,147 @@
//
// WMProgressView.m
// WMPageController
//
// Created by Mark on 15/6/20.
// Copyright (c) 2015年 yq. All rights reserved.
//
#import "WMProgressView.h"
@implementation WMProgressView {
int _sign;
CGFloat _gap;
CGFloat _step;
__weak CADisplayLink *_link;
}
- (CGFloat)speedFactor {
if (_speedFactor <= 0) {
_speedFactor = 15.0;
}
return _speedFactor;
}
- (void)setProgressWithOutAnimate:(CGFloat)progress {
if (self.progress == progress) return;
_progress = progress;
[self setNeedsDisplay];
}
- (void)setNaughty:(BOOL)naughty {
_naughty = naughty;
[self setNeedsDisplay];
}
- (void)setCornerRadius:(CGFloat)cornerRadius {
_cornerRadius = cornerRadius;
[self setNeedsDisplay];
}
- (void)moveToPostion:(NSInteger)pos {
_gap = fabs(self.progress - pos);
_sign = self.progress > pos ? -1 : 1;
_step = _gap / self.speedFactor;
if (_link) {
[_link invalidate];
// [_link removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(progressChanged)];
[link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
_link = link;
}
- (void)setProgress:(CGFloat)progress {
if (self.progress == progress) return;
_progress = progress;
[self setNeedsDisplay];
}
- (void)progressChanged {
if (_gap > 0.000001) {
_gap -= _step;
if (_gap < 0.0) {
self.progress = (int)(self.progress + _sign * _step + 0.5);
return;
}
self.progress += _sign * _step;
} else {
self.progress = (int)(self.progress + 0.5);
[_link invalidate];
_link = nil;
}
}
- (void)drawRect:(CGRect)rect {
// Drawing code
[super drawRect:rect];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGFloat height = self.frame.size.height;
int index = (int)self.progress;
index = (index <= self.itemFrames.count - 1) ? index : (int)self.itemFrames.count - 1;
CGFloat rate = self.progress - index;
CGRect currentFrame = [self.itemFrames[index] CGRectValue];
CGFloat currentWidth = currentFrame.size.width;
int nextIndex = index + 1 < self.itemFrames.count ? index + 1 : index;
CGFloat nextWidth = [self.itemFrames[nextIndex] CGRectValue].size.width;
CGFloat currentX = currentFrame.origin.x;
CGFloat nextX = [self.itemFrames[nextIndex] CGRectValue].origin.x;
CGFloat startX = currentX + (nextX - currentX) * rate;
CGFloat width = currentWidth + (nextWidth - currentWidth)*rate;
CGFloat endX = startX + width;
if (self.naughty) {
CGFloat currentMidX = currentX + currentWidth / 2.0;
CGFloat nextMidX = nextX + nextWidth / 2.0;
if (rate <= 0.5) {
startX = currentX + (currentMidX - currentX) * rate * 2.0;
CGFloat currentMaxX = currentX + currentWidth;
endX = currentMaxX + (nextMidX - currentMaxX) * rate * 2.0;
} else {
startX = currentMidX + (nextX - currentMidX) * (rate - 0.5) * 2.0;
CGFloat nextMaxX = nextX + nextWidth;
endX = nextMidX + (nextMaxX - nextMidX) * (rate - 0.5) * 2.0;
}
width = endX - startX;
}
CGFloat lineWidth = (self.hollow || self.hasBorder) ? 1.0 : 0.0;
if (self.isTriangle) {
CGContextMoveToPoint(ctx, startX, height);
CGContextAddLineToPoint(ctx, endX, height);
CGContextAddLineToPoint(ctx, startX + width / 2.0, 0);
CGContextClosePath(ctx);
CGContextSetFillColorWithColor(ctx, self.color);
CGContextFillPath(ctx);
return;
}
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(startX, lineWidth / 2.0, width, height - lineWidth) cornerRadius:self.cornerRadius];
CGContextAddPath(ctx, path.CGPath);
if (self.hollow) {
CGContextSetStrokeColorWithColor(ctx, self.color);
CGContextStrokePath(ctx);
return;
}
CGContextSetFillColorWithColor(ctx, self.color);
CGContextFillPath(ctx);
if (self.hasBorder) {
// 计算点
CGFloat startX = CGRectGetMinX([self.itemFrames.firstObject CGRectValue]);
CGFloat endX = CGRectGetMaxX([self.itemFrames.lastObject CGRectValue]);
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(startX, lineWidth / 2.0, (endX - startX), height - lineWidth) cornerRadius:self.cornerRadius];
CGContextSetLineWidth(ctx, lineWidth);
CGContextAddPath(ctx, path.CGPath);
// 绘制
CGContextSetStrokeColorWithColor(ctx, self.color);
CGContextStrokePath(ctx);
}
}
@end
@@ -0,0 +1,16 @@
//
// WMScrollView.h
// WMPageController
//
// Created by lh on 15/11/21.
// Copyright (c) 2015年 yq. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface WMScrollView : UIScrollView <UIGestureRecognizerDelegate>
/// 左滑时同时启用其他手势,比如系统左滑、sidemenu左滑。默认 NO
@property (assign, nonatomic) BOOL otherGestureRecognizerSimultaneously;
@end
@@ -0,0 +1,45 @@
//
// WMScrollView.m
// WMPageController
//
// Created by lh on 15/11/21.
// Copyright (c) 2015年 yq. All rights reserved.
//
#import "WMScrollView.h"
@implementation WMScrollView
#pragma mark - <UIGestureRecognizerDelegate>
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
// iOS横向滚动的scrollView和系统pop手势返回冲突的解决办法: http://blog.csdn.net/hjaycee/article/details/49279951
// 兼容系统pop手势 / FDFullscreenPopGesture / 如有自定义手势,需自行在此处判断
if ([otherGestureRecognizer.view isKindOfClass:NSClassFromString(@"UILayoutContainerView")]) {
if (otherGestureRecognizer.state == UIGestureRecognizerStateBegan && self.contentOffset.x == 0) {
return YES;
}
}
// ReSideMenu 及其他一些手势的开启,需要在这自行此有些。目前还没完全兼容好,会引起一个小问题
if (self.otherGestureRecognizerSimultaneously) {
// 再判断系统手势的state是began还是fail,同时判断scrollView的位置是不是正好在最左边
if (otherGestureRecognizer.state == UIGestureRecognizerStateBegan && self.contentOffset.x == 0) {
return YES;
}
}
return NO;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
//MARK: UITableViewCell 自定义手势可能要在此处自行定义
if ([otherGestureRecognizer.view isKindOfClass:NSClassFromString(@"UITableViewWrapperView")] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
return YES;
}
return NO;
}
@end
@@ -0,0 +1,61 @@
//
// WMPageConst.h
// WMPageController
//
// Created by Mark on 15/6/13.
// Copyright (c) 2015年 yq. All rights reserved.
//
// WMPageController的导航栏的一些默认属性
// 如懒得设置PageController的属性,可在此自行修改
// 标题的颜色(选中/非选中) (P.S.标题颜色是可动画的)
#define WMTitleColorSelected [UIColor colorWithRed:0.0/255.0 green:185.0/255.0 blue:239/255.0 alpha:1]
#define WMTitleColorNormal [UIColor colorWithRed:0 green:0 blue:0 alpha:1]
// 导航菜单栏的背景颜色
#define WMMenuBGColor [UIColor whiteColor]
// 标题的尺寸(选中/非选中)
static CGFloat const WMTitleSizeSelected = 18.0f;
static CGFloat const WMTitleSizeNormal = 15.0f;
// 导航菜单栏的高度
static CGFloat const WMMenuHeight = 44.0f;
// 导航菜单栏每个item的宽度
static CGFloat const WMMenuItemWidth = 65.0f;
// 如需要利用观察者来订阅通知,请将 postNotification 设置为 YES
//
// 当一个控制器的视图 被添加到父控制器的时候会发布一个通知
// 可用于判断当前控制器的序号,主要用于当同一个控制器管理时,通过判断序号来加载相应数据
// 传递的数据包含两个信息,当前序号 (index) 以及标题 (title)
//
// NOTE:由于缓存机制,不会在 controller 移出屏幕时销毁控制器,若想要控制器视图移除屏幕后不再收到通知,可在 -viewWillDisappear: 或 -viewDidDisappear: 中 removeObserver.
// 视图复用版本详见:https://github.com/wangmchn/YKPageView
static NSString *const WMControllerDidAddToSuperViewNotification = @"WMControllerDidAddToSuperViewNotification";
// 当一个控制器完全被展示在用户面前时发送的通知
// 可用于判断当前控制器的序号,加载或者刷新当前数据.
// 传递的数据包含两个信息,当前序号 (index) 以及标题 title
//
// NOTE:由于缓存机制,不会在 controller 移出屏幕时销毁控制器,若想要控制器视图移除屏幕后不再收到通知,可在 -viewWillDisappear: 或 -viewDidDisappear: 中 removeObserver.并且相应的,在 -viewWillAppear: 中添加 observer
static NSString *const WMControllerDidFullyDisplayedNotification = @"WMControllerDidFullyDisplayedNotification";
// 如果你遇到任何问题,或者有什么更好地建议,都可以联系我.
// 有什么能改进的都可以PR哦~ github有你更美好!~
// 好用的话还希望能够小小star一下~ :)
// Github: https://github.com/wangmchn
// Email: mailto:wangmchn@163.com
@@ -0,0 +1,352 @@
//
// WMPageController.h
// WMPageController
//
// Created by Mark on 15/6/11.
// Copyright (c) 2015年 yq. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "WMMenuView.h"
#import "WMScrollView.h"
@class WMPageController;
/*
* WMPageController 的缓存设置,默认缓存为无限制,当收到 memoryWarning 时,会自动切换到低缓存模式 (WMPageControllerCachePolicyLowMemory),并在一段时间后切换到 High .
收到多次警告后,会停留在到 WMPageControllerCachePolicyLowMemory 不再增长
*
* The Default cache policy is No Limit, when recieved memory warning, page controller will switch mode to 'LowMemory'
and continue to grow back after a while.
If recieved too much times, the cache policy will stay at 'LowMemory' and don't grow back any more.
*/
typedef NS_ENUM(NSUInteger, WMPageControllerCachePolicy) {
WMPageControllerCachePolicyNoLimit = 0, // No limit
WMPageControllerCachePolicyLowMemory = 1, // Low Memory but may block when scroll
WMPageControllerCachePolicyBalanced = 3, // Balanced ↑ and ↓
WMPageControllerCachePolicyHigh = 5 // High
};
typedef NS_ENUM(NSUInteger, WMPageControllerPreloadPolicy) {
WMPageControllerPreloadPolicyNever = 0, // Never pre-load controller.
WMPageControllerPreloadPolicyNeighbour = 1, // Pre-load the controller next to the current.
WMPageControllerPreloadPolicyNear = 2 // Pre-load 2 controllers near the current.
};
@protocol WMPageControllerDataSource <NSObject>
@optional
/**
* To inform how many child controllers will in `WMPageController`.
*
* @param pageController The parent controller.
*
* @return The value of child controllers's count.
*/
- (NSInteger)numbersOfChildControllersInPageController:(WMPageController * _Nonnull)pageController;
/**
* Return a controller that you wanna to display at index. You can set properties easily if you implement this methods.
*
* @param pageController The parent controller.
* @param index The index of child controller.
*
* @return The instance of a `UIViewController`.
*/
- (__kindof UIViewController * _Nonnull)pageController:(WMPageController * _Nonnull)pageController viewControllerAtIndex:(NSInteger)index;
/**
* Each title you wanna show in the `WMMenuView`
*
* @param pageController The parent controller.
* @param index The index of title.
*
* @return A `NSString` value to show at the top of `WMPageController`.
*/
- (NSString * _Nonnull)pageController:(WMPageController * _Nonnull)pageController titleAtIndex:(NSInteger)index;
@end
@protocol WMPageControllerDelegate <NSObject>
@optional
/**
* If the child controller is heavy, put some work in this method. This method will only be called when the controller is initialized and stop scrolling. (That means if the controller is cached and hasn't released will never call this method.)
*
* @param pageController The parent controller (WMPageController)
* @param viewController The viewController first show up when scroll stop.
* @param info A dictionary that includes some infos, such as: `index` / `title`
*/
- (void)pageController:(WMPageController * _Nonnull)pageController lazyLoadViewController:(__kindof UIViewController * _Nonnull)viewController withInfo:(NSDictionary * _Nonnull)info;
/**
* Called when a viewController will be cached. You can clear some data if it's not reusable.
*
* @param pageController The parent controller (WMPageController)
* @param viewController The viewController will be cached.
* @param info A dictionary that includes some infos, such as: `index` / `title`
*/
- (void)pageController:(WMPageController * _Nonnull)pageController willCachedViewController:(__kindof UIViewController * _Nonnull)viewController withInfo:(NSDictionary * _Nonnull)info;
/**
* Called when a viewController will be appear to user's sight. Do some preparatory methods if needed.
*
* @param pageController The parent controller (WMPageController)
* @param viewController The viewController will appear.
* @param info A dictionary that includes some infos, such as: `index` / `title`
*/
- (void)pageController:(WMPageController * _Nonnull)pageController willEnterViewController:(__kindof UIViewController * _Nonnull)viewController withInfo:(NSDictionary * _Nonnull)info;
/**
* Called when a viewController will fully displayed, that means, scrollView have stopped scrolling and the controller's view have entirely displayed.
*
* @param pageController The parent controller (WMPageController)
* @param viewController The viewController entirely displayed.
* @param info A dictionary that includes some infos, such as: `index` / `title`
*/
- (void)pageController:(WMPageController * _Nonnull)pageController didEnterViewController:(__kindof UIViewController * _Nonnull)viewController withInfo:(NSDictionary * _Nonnull)info;
@end
@interface WMPageController : UIViewController <WMMenuViewDelegate, WMMenuViewDataSource, UIScrollViewDelegate, WMPageControllerDataSource, WMPageControllerDelegate>
@property (nonatomic, weak) id<WMPageControllerDelegate> _Nullable delegate;
@property (nonatomic, weak) id<WMPageControllerDataSource> _Nullable dataSource;
/**
* Values and keys can set properties when initialize child controlelr (it's KVC)
* values keys 属性可以用于初始化控制器的时候为控制器传值(利用 KVC 来设置)
使用时请确保 key 与控制器的属性名字一致!!(例如:控制器有需要设置的属性 type,那么 keys 所放的就是字符串 @"type")
*/
@property (nonatomic, strong) NSMutableArray<id> * _Nullable values;
@property (nonatomic, strong) NSMutableArray<NSString *> * _Nullable keys;
/**
* 各个控制器的 class, 例如:[UITableViewController class]
* Each controller's class, example:[UITableViewController class]
*/
@property (nonatomic, copy) NSArray<Class> * _Nullable viewControllerClasses;
/**
* 各个控制器标题
* Titles of view controllers in page controller.
*/
@property (nonatomic, copy) NSArray<NSString *> * _Nullable titles;
@property (nonatomic, strong, readonly) UIViewController * _Nonnull currentViewController;
/**
* 设置选中几号 item
* To select item at index
*/
@property (nonatomic, assign) int selectIndex;
/**
* 点击的 MenuItem 是否触发滚动动画
* Whether to animate when press the MenuItem
*/
@property (nonatomic, assign) BOOL pageAnimatable;
/** 是否自动通过字符串计算 MenuItem 的宽度,默认为 NO. */
@property (nonatomic, assign) BOOL automaticallyCalculatesItemWidths;
/** Whether the controller can scroll. Default is YES. */
@property (nonatomic, assign) BOOL scrollEnable;
/**
* 选中时的标题尺寸
* The title size when selected (animatable)
*/
@property (nonatomic, assign) CGFloat titleSizeSelected;
/**
* 非选中时的标题尺寸
* The normal title size (animatable)
*/
@property (nonatomic, assign) CGFloat titleSizeNormal;
/**
* 标题选中时的颜色, 颜色是可动画的.
* The title color when selected, the color is animatable.
*/
@property (nonatomic, strong) UIColor * _Nullable titleColorSelected;
/**
* 标题非选择时的颜色, 颜色是可动画的.
* The title's normal color, the color is animatable.
*/
@property (nonatomic, strong) UIColor * _Nullable titleColorNormal;
/**
* 标题的字体名字
* The name of title's font
*/
@property (nonatomic, copy) NSString * _Nullable titleFontName;
/**
* 导航栏高度
* The menu view's height
*/
@property (nonatomic, assign) CGFloat menuHeight;
/**
* 每个 MenuItem 的宽度
* The item width,when all are same,use this property
*/
@property (nonatomic, assign) CGFloat menuItemWidth;
/**
* 各个 MenuItem 的宽度,可不等,数组内为 NSNumber.
* Each item's width, when they are not all the same, use this property, Put `NSNumber` in this array.
*/
@property (nonatomic, copy) NSArray<NSNumber *> * _Nullable itemsWidths;
/**
* 导航栏背景色
* The background color of menu view
*/
@property (nonatomic, strong) UIColor * _Nullable menuBGColor;
/**
* Menu view 的样式,默认为无下划线
* Menu view's style, now has two different styles, 'Line','default'
*/
@property (nonatomic, assign) WMMenuViewStyle menuViewStyle;
@property (nonatomic, assign) WMMenuViewLayoutMode menuViewLayoutMode;
/**
* 进度条的颜色,默认和选中颜色一致(如果 style 为 Default,则该属性无用)
* The progress's color,the default color is same with `titleColorSelected`.If you want to have a different color, set this property.
*/
@property (nonatomic, strong) UIColor * _Nullable progressColor;
/**
* 定制进度条在各个 item 下的宽度
*/
@property (nonatomic, strong) NSArray * _Nullable progressViewWidths;
/// 定制进度条,若每个进度条长度相同,可设置该属性
@property (nonatomic, assign) CGFloat progressWidth;
/// 调皮效果,用于实现腾讯视频新效果,请设置一个较小的 progressWidth
@property (nonatomic, assign) BOOL progressViewIsNaughty;
/**
* 是否发送在创建控制器或者视图完全展现在用户眼前时通知观察者,默认为不开启,如需利用通知请开启
* Whether notify observer when finish init or fully displayed to user, the default is NO.
* See `WMPageConst.h` for more information.
*/
@property (nonatomic, assign) BOOL postNotification;
/**
* 是否记录 Controller 的位置,并在下次回来的时候回到相应位置,默认为 NO (若当前缓存中存在不会触发)
* Whether to remember controller's positon if it's a kind of scrollView controller,like UITableViewController,The default value is NO.
* 比如 `UITabelViewController`, 当然你也可以在自己的控制器中自行设置, 如果将 Controller.view 替换为 scrollView 或者在Controller.view 上添加了一个和自身 bounds 一样的 scrollView 也是OK的
*/
@property (nonatomic, assign) BOOL rememberLocation __deprecated_msg("Because of the cache policy,this property can abondon now.");
/** 缓存的机制,默认为无限制 (如果收到内存警告, 会自动切换) */
@property (nonatomic, assign) WMPageControllerCachePolicy cachePolicy;
/** 预加载机制,在停止滑动的时候预加载 n 页 */
@property (nonatomic, assign) WMPageControllerPreloadPolicy preloadPolicy;
/** Whether ContentView bounces */
@property (nonatomic, assign) BOOL bounces;
/**
* 是否作为 NavigationBar 的 titleView 展示,默认 NO
* Whether to show on navigation bar, the default value is `NO`
*/
@property (assign, nonatomic) BOOL showOnNavigationBar;
/**
* 用代码设置 contentView 的 contentOffset 之前,请设置 startDragging = YES
* Set startDragging = YES before set contentView.contentOffset = xxx;
*/
@property (nonatomic, assign) BOOL startDragging;
/** 下划线进度条的高度 */
@property (nonatomic, assign) CGFloat progressHeight;
/** WMPageController View' frame */
@property (nonatomic, assign) CGRect viewFrame;
/**
* Menu view items' margin / make sure it's count is equal to (controllers' count + 1),default is 0
顶部菜单栏各个 item 的间隙,因为包括头尾两端,所以确保它的数量等于控制器数量 + 1, 默认间隙为 0
*/
@property (nonatomic, copy) NSArray<NSNumber *> * _Nullable itemsMargins;
/**
* set itemMargin if all margins are the same, default is 0
如果各个间隙都想同,设置该属性,默认为 0
*/
@property (nonatomic, assign) CGFloat itemMargin;
/** 顶部 menuView 和 scrollView 之间的间隙 */
@property (nonatomic, assign) CGFloat menuViewBottomSpace;
/** progressView 到 menuView 底部的距离 */
@property (nonatomic, assign) CGFloat progressViewBottomSpace;
/** progressView's cornerRadius */
@property (nonatomic, assign) CGFloat progressViewCornerRadius;
/** 顶部导航栏 */
@property (nonatomic, weak) WMMenuView * _Nullable menuView;
/** 内部容器 */
@property (nonatomic, weak) WMScrollView * _Nullable scrollView;
/** MenuView 内部视图与左右的间距 */
@property (nonatomic, assign) CGFloat menuViewContentMargin;
/**
* 左滑时同时启用其他手势,比如系统左滑、sidemenu左滑。默认 NO
(会引起一个小问题,第一个和最后一个控制器会变得可以斜滑, 还未解决)
*/
@property (assign, nonatomic) BOOL otherGestureRecognizerSimultaneously;
/**
* 构造方法,请使用该方法创建控制器. 或者实现数据源方法. /
* Init methodrecommend to use this instead of `-init`. Or you can implement datasource by yourself.
*
* @param classes 子控制器的 class,确保数量与 titles 的数量相等
* @param titles 各个子控制器的标题,用 NSString 描述
*
* @return instancetype
*/
- (instancetype _Nullable)initWithViewControllerClasses:(NSArray<Class> * _Nonnull)classes andTheirTitles:(NSArray<NSString *> * _Nonnull)titles;
/**
* A method in order to reload MenuView and child view controllers. If you had set `itemsMargins` or `itemsWidths` `values` and `keys` before, make sure you have update them also before you call this method. And most important, PAY ATTENTION TO THE COUNT OF THOSE ARRAY.
该方法用于重置刷新父控制器,该刷新包括顶部 MenuView 和 childViewControllers.如果之前设置过 `itemsMargins` 和 `itemsWidths` `values` 以及 `keys` 属性,请确保在调用 reload 之前也同时更新了这些属性。并且,最最最重要的,注意数组的个数以防止溢出。
*/
- (void)reloadData;
/**
* Update designated item's title
更新指定序号的控制器的标题
*
* @param title 新的标题
* @param index 目标序号
*/
- (void)updateTitle:(NSString * _Nonnull)title atIndex:(NSInteger)index;
/**
* Update designated item's title and width
更新指定序号的控制器的标题以及他的宽度
*
* @param title 新的标题
* @param index 目标序号
* @param width 对应item的新宽度
*/
- (void)updateTitle:(NSString * _Nonnull)title andWidth:(CGFloat)width atIndex:(NSInteger)index;
/** 当 app 即将进入后台接收到的通知 */
- (void)willResignActive:(NSNotification * _Nonnull)notification;
/** 当 app 即将回到前台接收到的通知 */
- (void)willEnterForeground:(NSNotification * _Nonnull)notification;
@end
@@ -0,0 +1,942 @@
//
// WMPageController.m
// WMPageController
//
// Created by Mark on 15/6/11.
// Copyright (c) 2015年 yq. All rights reserved.
//
#import "WMPageController.h"
#import "WMPageConst.h"
static NSInteger const kWMUndefinedIndex = -1;
static NSInteger const kWMControllerCountUndefined = -1;
@interface WMPageController () {
CGFloat _viewHeight, _viewWidth, _viewX, _viewY, _targetX, _superviewHeight;
BOOL _hasInited, _shouldNotScroll;
NSInteger _initializedIndex, _controllerConut, _markedSelectIndex;
}
@property (nonatomic, strong, readwrite) UIViewController *currentViewController;
// 用于记录子控制器view的frame,用于 scrollView 上的展示的位置
@property (nonatomic, strong) NSMutableArray *childViewFrames;
// 当前展示在屏幕上的控制器,方便在滚动的时候读取 (避免不必要计算)
@property (nonatomic, strong) NSMutableDictionary *displayVC;
// 用于记录销毁的viewController的位置 (如果它是某一种scrollView的Controller的话)
@property (nonatomic, strong) NSMutableDictionary *posRecords;
// 用于缓存加载过的控制器
@property (nonatomic, strong) NSCache *memCache;
@property (nonatomic, strong) NSMutableDictionary *backgroundCache;
// 收到内存警告的次数
@property (nonatomic, assign) int memoryWarningCount;
@property (nonatomic, readonly) NSInteger childControllersCount;
@end
@implementation WMPageController
#pragma mark - Lazy Loading
- (NSMutableDictionary *)posRecords {
if (_posRecords == nil) {
_posRecords = [[NSMutableDictionary alloc] init];
}
return _posRecords;
}
- (NSMutableDictionary *)displayVC {
if (_displayVC == nil) {
_displayVC = [[NSMutableDictionary alloc] init];
}
return _displayVC;
}
- (NSMutableDictionary *)backgroundCache {
if (_backgroundCache == nil) {
_backgroundCache = [[NSMutableDictionary alloc] init];
}
return _backgroundCache;
}
#pragma mark - Public Methods
- (instancetype)initWithViewControllerClasses:(NSArray<Class> *)classes andTheirTitles:(NSArray<NSString *> *)titles {
if (self = [super init]) {
NSParameterAssert(classes.count == titles.count);
_viewControllerClasses = [NSArray arrayWithArray:classes];
_titles = [NSArray arrayWithArray:titles];
[self wm_setup];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self wm_setup];
}
return self;
}
- (instancetype)init {
if (self = [super init]) {
[self wm_setup];
}
return self;
}
- (void)setEdgesForExtendedLayout:(UIRectEdge)edgesForExtendedLayout {
if (self.edgesForExtendedLayout == edgesForExtendedLayout) { return; }
[super setEdgesForExtendedLayout:edgesForExtendedLayout];
if (_hasInited) {
_hasInited = NO;
[self viewDidLayoutSubviews];
}
}
- (void)setScrollEnable:(BOOL)scrollEnable {
_scrollEnable = scrollEnable;
if (!self.scrollView) { return; }
self.scrollView.scrollEnabled = scrollEnable;
}
- (void)setProgressViewCornerRadius:(CGFloat)progressViewCornerRadius {
_progressViewCornerRadius = progressViewCornerRadius;
if (self.menuView) {
self.menuView.progressViewCornerRadius = progressViewCornerRadius;
}
}
- (void)setMenuViewLayoutMode:(WMMenuViewLayoutMode)menuViewLayoutMode {
_menuViewLayoutMode = menuViewLayoutMode;
if (self.menuView.superview) {
[self wm_resetMenuView];
}
}
- (void)setCachePolicy:(WMPageControllerCachePolicy)cachePolicy {
_cachePolicy = cachePolicy;
self.memCache.countLimit = _cachePolicy;
}
- (void)setSelectIndex:(int)selectIndex {
_selectIndex = selectIndex;
_markedSelectIndex = kWMUndefinedIndex;
if (self.menuView && _hasInited) {
[self.menuView selectItemAtIndex:selectIndex];
} else {
_markedSelectIndex = selectIndex;
}
}
- (void)setProgressViewIsNaughty:(BOOL)progressViewIsNaughty {
_progressViewIsNaughty = progressViewIsNaughty;
if (self.menuView) {
self.menuView.progressViewIsNaughty = progressViewIsNaughty;
}
}
- (void)setProgressWidth:(CGFloat)progressWidth {
_progressWidth = progressWidth;
self.progressViewWidths = ({
NSMutableArray *tmp = [NSMutableArray array];
for (int i = 0; i < self.childControllersCount; i++) {
[tmp addObject:@(progressWidth)];
}
tmp.copy;
});
}
- (void)setProgressViewWidths:(NSArray *)progressViewWidths {
_progressViewWidths = progressViewWidths;
if (self.menuView) {
self.menuView.progressWidths = progressViewWidths;
}
}
- (void)setMenuViewContentMargin:(CGFloat)menuViewContentMargin {
_menuViewContentMargin = menuViewContentMargin;
if (self.menuView) {
self.menuView.contentMargin = menuViewContentMargin;
}
}
- (void)setViewFrame:(CGRect)viewFrame {
if (CGRectEqualToRect(viewFrame, _viewFrame)) { return; }
_viewFrame = viewFrame;
if (self.menuView) {
_hasInited = NO;
[self viewDidLayoutSubviews];
}
}
- (void)reloadData {
[self wm_clearDatas];
if (!self.childControllersCount) { return; }
[self wm_resetScrollView];
[self.memCache removeAllObjects];
[self wm_resetMenuView];
[self viewDidLayoutSubviews];
}
- (void)updateTitle:(NSString *)title atIndex:(NSInteger)index {
[self.menuView updateTitle:title atIndex:index andWidth:NO];
}
- (void)updateTitle:(NSString *)title andWidth:(CGFloat)width atIndex:(NSInteger)index {
if (self.itemsWidths && index < self.itemsWidths.count) {
NSMutableArray *mutableWidths = [NSMutableArray arrayWithArray:self.itemsWidths];
mutableWidths[index] = @(width);
self.itemsWidths = [mutableWidths copy];
} else {
NSMutableArray *mutableWidths = [NSMutableArray array];
for (int i = 0; i < self.childControllersCount; i++) {
CGFloat itemWidth = (i == index) ? width : self.menuItemWidth;
[mutableWidths addObject:@(itemWidth)];
}
self.itemsWidths = [mutableWidths copy];
}
[self.menuView updateTitle:title atIndex:index andWidth:YES];
}
#pragma mark - Notification
- (void)willResignActive:(NSNotification *)notification {
for (int i = 0; i < self.childControllersCount; i++) {
id obj = [self.memCache objectForKey:@(i)];
if (obj) {
[self.backgroundCache setObject:obj forKey:@(i)];
}
}
}
- (void)willEnterForeground:(NSNotification *)notification {
for (NSNumber *key in self.backgroundCache.allKeys) {
if (![self.memCache objectForKey:key]) {
[self.memCache setObject:self.backgroundCache[key] forKey:key];
}
}
[self.backgroundCache removeAllObjects];
}
#pragma mark - Delegate
- (NSDictionary *)infoWithIndex:(NSInteger)index {
NSString *title = [self titleAtIndex:index];
return @{@"title": title ? title : @"", @"index": @(index)};
}
- (void)willCachedController:(UIViewController *)vc atIndex:(NSInteger)index {
if (self.childControllersCount && [self.delegate respondsToSelector:@selector(pageController:willCachedViewController:withInfo:)]) {
NSDictionary *info = [self infoWithIndex:index];
[self.delegate pageController:self willCachedViewController:vc withInfo:info];
}
}
- (void)willEnterController:(UIViewController *)vc atIndex:(NSInteger)index {
_selectIndex = (int)index;
if (self.childControllersCount && [self.delegate respondsToSelector:@selector(pageController:willEnterViewController:withInfo:)]) {
NSDictionary *info = [self infoWithIndex:index];
[self.delegate pageController:self willEnterViewController:vc withInfo:info];
}
}
// 完全进入控制器 (即停止滑动后调用)
- (void)didEnterController:(UIViewController *)vc atIndex:(NSInteger)index {
if (!self.childControllersCount) { return; }
NSDictionary *info = [self infoWithIndex:index];
if ([self.delegate respondsToSelector:@selector(pageController:didEnterViewController:withInfo:)]) {
[self.delegate pageController:self didEnterViewController:vc withInfo:info];
}
// 当控制器创建时,调用延迟加载的代理方法
if (_initializedIndex == index && [self.delegate respondsToSelector:@selector(pageController:lazyLoadViewController:withInfo:)]) {
[self.delegate pageController:self lazyLoadViewController:vc withInfo:info];
_initializedIndex = kWMUndefinedIndex;
}
// 根据 preloadPolicy 预加载控制器
if (self.preloadPolicy == WMPageControllerPreloadPolicyNever) { return; }
int start = 0;
int end = (int)self.childControllersCount - 1;
if (index > self.preloadPolicy) {
start = (int)index - self.preloadPolicy;
}
if (self.childControllersCount - 1 > self.preloadPolicy + index) {
end = (int)index + self.preloadPolicy;
}
for (int i = start; i <= end; i++) {
// 如果已存在,不需要预加载
if (![self.memCache objectForKey:@(i)] && !self.displayVC[@(i)]) {
[self wm_addViewControllerAtIndex:i];
[self wm_postAddToSuperViewNotificationWithIndex:i];
}
}
_selectIndex = (int)index;
}
#pragma mark - Data source
- (NSInteger)childControllersCount {
if (_controllerConut == kWMControllerCountUndefined) {
if ([self.dataSource respondsToSelector:@selector(numbersOfChildControllersInPageController:)]) {
_controllerConut = [self.dataSource numbersOfChildControllersInPageController:self];
} else {
_controllerConut = self.viewControllerClasses.count;
}
}
return _controllerConut;
}
- (UIViewController * _Nonnull)initializeViewControllerAtIndex:(NSInteger)index {
if ([self.dataSource respondsToSelector:@selector(pageController:viewControllerAtIndex:)]) {
return [self.dataSource pageController:self viewControllerAtIndex:index];
}
return [[self.viewControllerClasses[index] alloc] init];
}
- (NSString * _Nonnull)titleAtIndex:(NSInteger)index {
NSString *title = nil;
if ([self.dataSource respondsToSelector:@selector(pageController:titleAtIndex:)]) {
title = [self.dataSource pageController:self titleAtIndex:index];
} else {
title = self.titles[index];
}
return (title ? title : @"");
}
#pragma mark - Private Methods
- (void)wm_resetScrollView {
if (self.scrollView) {
[self.scrollView removeFromSuperview];
}
[self wm_addScrollView];
[self wm_addViewControllerAtIndex:self.selectIndex];
self.currentViewController = self.displayVC[@(self.selectIndex)];
}
- (void)wm_clearDatas {
_controllerConut = kWMControllerCountUndefined;
_hasInited = NO;
_selectIndex = self.selectIndex < self.childControllersCount ? self.selectIndex : (int)self.childControllersCount - 1;
if (self.progressWidth > 0) { self.progressWidth = self.progressWidth; }
NSArray *displayingViewControllers = self.displayVC.allValues;
for (UIViewController *vc in displayingViewControllers) {
[vc.view removeFromSuperview];
[vc willMoveToParentViewController:nil];
[vc removeFromParentViewController];
}
self.memoryWarningCount = 0;
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(wm_growCachePolicyAfterMemoryWarning) object:nil];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(wm_growCachePolicyToHigh) object:nil];
self.currentViewController = nil;
[self.posRecords removeAllObjects];
[self.displayVC removeAllObjects];
}
// 当子控制器init完成时发送通知
- (void)wm_postAddToSuperViewNotificationWithIndex:(int)index {
if (!self.postNotification) { return; }
NSDictionary *info = @{
@"index":@(index),
@"title":[self titleAtIndex:index]
};
[[NSNotificationCenter defaultCenter] postNotificationName:WMControllerDidAddToSuperViewNotification
object:info];
}
// 当子控制器完全展示在user面前时发送通知
- (void)wm_postFullyDisplayedNotificationWithCurrentIndex:(int)index {
if (!self.postNotification) { return; }
NSDictionary *info = @{
@"index":@(index),
@"title":[self titleAtIndex:index]
};
[[NSNotificationCenter defaultCenter] postNotificationName:WMControllerDidFullyDisplayedNotification
object:info];
}
// 初始化一些参数,在init中调用
- (void)wm_setup {
_titleSizeSelected = WMTitleSizeSelected;
_titleColorSelected = WMTitleColorSelected;
_titleSizeNormal = WMTitleSizeNormal;
_titleColorNormal = WMTitleColorNormal;
_menuBGColor = WMMenuBGColor;
_menuHeight = WMMenuHeight;
_menuItemWidth = WMMenuItemWidth;
_memCache = [[NSCache alloc] init];
_initializedIndex = kWMUndefinedIndex;
_markedSelectIndex = kWMUndefinedIndex;
_controllerConut = kWMControllerCountUndefined;
_scrollEnable = YES;
self.automaticallyCalculatesItemWidths = NO;
self.automaticallyAdjustsScrollViewInsets = NO;
self.preloadPolicy = WMPageControllerPreloadPolicyNever;
self.cachePolicy = WMPageControllerCachePolicyNoLimit;
self.delegate = self;
self.dataSource = self;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}
// 包括宽高,子控制器视图 frame
- (void)wm_calculateSize {
CGFloat navigationHeight = CGRectGetMaxY(self.navigationController.navigationBar.frame);
UIView *tabBar = self.tabBarController.tabBar ? self.tabBarController.tabBar : self.navigationController.toolbar;
CGFloat height = (tabBar && !tabBar.hidden) ? CGRectGetHeight(tabBar.frame) : 0;
CGFloat tarBarHeight = self.hidesBottomBarWhenPushed == YES ? 0 : height;
// 计算相对 window 的绝对 frame (self.view.window 可能为 nil)
UIWindow *mainWindow = [[UIApplication sharedApplication].delegate window];
CGRect absoluteRect = [self.view.superview convertRect:self.view.frame toView:mainWindow];
navigationHeight -= absoluteRect.origin.y;
tarBarHeight -= mainWindow.frame.size.height - CGRectGetMaxY(absoluteRect);
_viewX = self.viewFrame.origin.x;
_viewY = self.viewFrame.origin.y;
if (CGRectEqualToRect(self.viewFrame, CGRectZero)) {
_viewWidth = self.view.frame.size.width;
_viewHeight = self.view.frame.size.height - self.menuHeight - self.menuViewBottomSpace - navigationHeight - tarBarHeight;
_viewY += navigationHeight;
} else {
_viewWidth = self.viewFrame.size.width;
_viewHeight = self.viewFrame.size.height - self.menuHeight - self.menuViewBottomSpace;
}
if (self.showOnNavigationBar && self.navigationController.navigationBar) {
_viewHeight += self.menuHeight;
} // 重新计算各个控制器视图的宽高
_childViewFrames = [NSMutableArray array];
for (int i = 0; i < self.childControllersCount; i++) {
CGRect frame = CGRectMake(i*_viewWidth, 0, _viewWidth, _viewHeight);
[_childViewFrames addObject:[NSValue valueWithCGRect:frame]];
}
}
- (void)wm_addScrollView {
WMScrollView *scrollView = [[WMScrollView alloc] init];
scrollView.scrollsToTop = NO;
scrollView.pagingEnabled = YES;
scrollView.backgroundColor = [UIColor whiteColor];
scrollView.delegate = self;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.bounces = self.bounces;
scrollView.otherGestureRecognizerSimultaneously = self.otherGestureRecognizerSimultaneously;
scrollView.scrollEnabled = self.scrollEnable;
[self.view addSubview:scrollView];
self.scrollView = scrollView;
if (!self.navigationController) { return; }
for (UIGestureRecognizer *gestureRecognizer in scrollView.gestureRecognizers) {
[gestureRecognizer requireGestureRecognizerToFail:self.navigationController.interactivePopGestureRecognizer];
}
}
- (void)wm_addMenuView {
CGFloat menuY = _viewY;
if (self.showOnNavigationBar && self.navigationController.navigationBar) {
CGFloat navHeight = self.navigationController.navigationBar.frame.size.height;
CGFloat menuHeight = self.menuHeight > navHeight ? navHeight : self.menuHeight;
menuY = (navHeight - menuHeight) / 2;
}
CGRect frame = CGRectMake(_viewX, menuY, _viewWidth, self.menuHeight);
WMMenuView *menuView = [[WMMenuView alloc] initWithFrame:frame];
menuView.backgroundColor = self.menuBGColor;
menuView.delegate = self;
menuView.dataSource = self;
menuView.style = self.menuViewStyle;
menuView.layoutMode = self.menuViewLayoutMode;
menuView.progressHeight = self.progressHeight;
menuView.contentMargin = self.menuViewContentMargin;
menuView.progressViewBottomSpace = self.progressViewBottomSpace;
menuView.progressWidths = self.progressViewWidths;
menuView.progressViewIsNaughty = self.progressViewIsNaughty;
menuView.progressViewCornerRadius = self.progressViewCornerRadius;
if (self.titleFontName) {
menuView.fontName = self.titleFontName;
}
if (self.progressColor) {
menuView.lineColor = self.progressColor;
}
if (self.showOnNavigationBar && self.navigationController.navigationBar) {
self.navigationItem.titleView = menuView;
} else {
[self.view addSubview:menuView];
}
self.menuView = menuView;
}
- (void)wm_layoutChildViewControllers {
int currentPage = (int)self.scrollView.contentOffset.x / _viewWidth;
int start = currentPage == 0 ? currentPage : (currentPage - 1);
int end = (currentPage == self.childControllersCount - 1) ? currentPage : (currentPage + 1);
for (int i = start; i <= end; i++) {
CGRect frame = [self.childViewFrames[i] CGRectValue];
UIViewController *vc = [self.displayVC objectForKey:@(i)];
if ([self wm_isInScreen:frame]) {
if (vc == nil) {
[self wm_initializedControllerWithIndexIfNeeded:i];
}
} else {
if (vc) {
// vc不在视野中且存在,移除他
[self wm_removeViewController:vc atIndex:i];
}
}
}
}
// 创建或从缓存中获取控制器并添加到视图上
- (void)wm_initializedControllerWithIndexIfNeeded:(NSInteger)index {
// 先从 cache 中取
UIViewController *vc = [self.memCache objectForKey:@(index)];
if (vc) {
// cache 中存在,添加到 scrollView 上,并放入display
[self wm_addCachedViewController:vc atIndex:index];
} else {
// cache 中也不存在,创建并添加到display
[self wm_addViewControllerAtIndex:(int)index];
}
[self wm_postAddToSuperViewNotificationWithIndex:(int)index];
}
- (void)wm_removeSuperfluousViewControllersIfNeeded {
[self.displayVC enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull key, UIViewController * _Nonnull vc, BOOL * _Nonnull stop) {
NSInteger index = key.integerValue;
CGRect frame = [self.childViewFrames[index] CGRectValue];
if (![self wm_isInScreen:frame]) {
[self wm_removeViewController:vc atIndex:index];
}
}];
}
- (void)wm_addCachedViewController:(UIViewController *)viewController atIndex:(NSInteger)index {
[self addChildViewController:viewController];
viewController.view.frame = [self.childViewFrames[index] CGRectValue];
[viewController didMoveToParentViewController:self];
[self.scrollView addSubview:viewController.view];
[self willEnterController:viewController atIndex:index];
[self.displayVC setObject:viewController forKey:@(index)];
}
// 创建并添加子控制器
- (void)wm_addViewControllerAtIndex:(int)index {
_initializedIndex = index;
UIViewController *viewController = [self initializeViewControllerAtIndex:index];
if (self.values.count == self.childControllersCount && self.keys.count == self.childControllersCount) {
[viewController setValue:self.values[index] forKey:self.keys[index]];
}
[self addChildViewController:viewController];
CGRect frame = self.childViewFrames.count ? [self.childViewFrames[index] CGRectValue] : self.view.frame;
viewController.view.frame = frame;
[viewController didMoveToParentViewController:self];
[self.scrollView addSubview:viewController.view];
[self willEnterController:viewController atIndex:index];
[self.displayVC setObject:viewController forKey:@(index)];
[self wm_backToPositionIfNeeded:viewController atIndex:index];
}
// 移除控制器,且从display中移除
- (void)wm_removeViewController:(UIViewController *)viewController atIndex:(NSInteger)index {
[self wm_rememberPositionIfNeeded:viewController atIndex:index];
[viewController.view removeFromSuperview];
[viewController willMoveToParentViewController:nil];
[viewController removeFromParentViewController];
[self.displayVC removeObjectForKey:@(index)];
// 放入缓存
if (![self.memCache objectForKey:@(index)]) {
[self willCachedController:viewController atIndex:index];
[self.memCache setObject:viewController forKey:@(index)];
}
}
- (void)wm_backToPositionIfNeeded:(UIViewController *)controller atIndex:(NSInteger)index {
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Wdeprecated-declarations"
if (!self.rememberLocation) return;
#pragma clang diagnostic pop
if ([self.memCache objectForKey:@(index)]) return;
UIScrollView *scrollView = [self wm_isKindOfScrollViewController:controller];
if (scrollView) {
NSValue *pointValue = self.posRecords[@(index)];
if (pointValue) {
CGPoint pos = [pointValue CGPointValue];
[scrollView setContentOffset:pos];
}
}
}
- (void)wm_rememberPositionIfNeeded:(UIViewController *)controller atIndex:(NSInteger)index {
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Wdeprecated-declarations"
if (!self.rememberLocation) return;
#pragma clang diagnostic pop
UIScrollView *scrollView = [self wm_isKindOfScrollViewController:controller];
if (scrollView) {
CGPoint pos = scrollView.contentOffset;
self.posRecords[@(index)] = [NSValue valueWithCGPoint:pos];
}
}
- (UIScrollView *)wm_isKindOfScrollViewController:(UIViewController *)controller {
UIScrollView *scrollView = nil;
if ([controller.view isKindOfClass:[UIScrollView class]]) {
// Controller的view是scrollView的子类(UITableViewController/UIViewController替换view为scrollView)
scrollView = (UIScrollView *)controller.view;
} else if (controller.view.subviews.count >= 1) {
// Controller的view的subViews[0]存在且是scrollView的子类,并且frame等与view得frame(UICollectionViewController/UIViewController添加UIScrollView)
UIView *view = controller.view.subviews[0];
if ([view isKindOfClass:[UIScrollView class]]) {
scrollView = (UIScrollView *)view;
}
}
return scrollView;
}
- (BOOL)wm_isInScreen:(CGRect)frame {
CGFloat x = frame.origin.x;
CGFloat ScreenWidth = self.scrollView.frame.size.width;
CGFloat contentOffsetX = self.scrollView.contentOffset.x;
if (CGRectGetMaxX(frame) > contentOffsetX && x-contentOffsetX < ScreenWidth) {
return YES;
} else {
return NO;
}
}
- (void)wm_resetMenuView {
if (!self.menuView) {
[self wm_addMenuView];
} else {
[self.menuView reload];
if (self.menuView.userInteractionEnabled == NO) {
self.menuView.userInteractionEnabled = YES;
}
if (self.selectIndex != 0) {
[self.menuView selectItemAtIndex:self.selectIndex];
}
[self.view bringSubviewToFront:self.menuView];
}
}
- (void)wm_growCachePolicyAfterMemoryWarning {
self.cachePolicy = WMPageControllerCachePolicyBalanced;
[self performSelector:@selector(wm_growCachePolicyToHigh) withObject:nil afterDelay:2.0 inModes:@[NSRunLoopCommonModes]];
}
- (void)wm_growCachePolicyToHigh {
self.cachePolicy = WMPageControllerCachePolicyHigh;
}
#pragma mark - Adjust Frame
- (void)wm_adjustScrollViewFrame {
// While rotate at last page, set scroll frame will call `-scrollViewDidScroll:` delegate
// It's not my expectation, so I use `_shouldNotScroll` to lock it.
// Wait for a better solution.
_shouldNotScroll = YES;
CGRect scrollFrame = CGRectMake(_viewX, _viewY + self.menuHeight + self.menuViewBottomSpace, _viewWidth, _viewHeight);
CGFloat oldContentOffsetX = self.scrollView.contentOffset.x;
CGFloat contentWidth = self.scrollView.contentSize.width;
scrollFrame.origin.y -= self.showOnNavigationBar && self.navigationController.navigationBar ? self.menuHeight : 0;
self.scrollView.frame = scrollFrame;
self.scrollView.contentSize = CGSizeMake(self.childControllersCount * _viewWidth, 0);
CGFloat xContentOffset = contentWidth == 0 ? self.selectIndex * _viewWidth : oldContentOffsetX / contentWidth * self.childControllersCount * _viewWidth;
[self.scrollView setContentOffset:CGPointMake(xContentOffset, 0)];
_shouldNotScroll = NO;
}
- (void)wm_adjustDisplayingViewControllersFrame {
[self.displayVC enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull key, UIViewController * _Nonnull vc, BOOL * _Nonnull stop) {
NSInteger index = key.integerValue;
CGRect frame = [self.childViewFrames[index] CGRectValue];
vc.view.frame = frame;
}];
}
- (void)wm_adjustMenuViewFrame {
// 根据是否在导航栏上展示调整frame
CGFloat menuHeight = self.menuHeight;
__block CGFloat menuX = _viewX;
__block CGFloat menuY = _viewY;
__block CGFloat rightWidth = 0;
if (self.showOnNavigationBar && self.navigationController.navigationBar) {
[self.navigationController.navigationBar.subviews enumerateObjectsUsingBlock:^(UIView* obj, NSUInteger idx, BOOL *stop) {
if (![obj isKindOfClass:[WMMenuView class]] && ![obj isKindOfClass:NSClassFromString(@"_UINavigationBarBackground")] && obj.alpha != 0 && obj.hidden == NO) {
CGFloat maxX = CGRectGetMaxX(obj.frame);
if (maxX < _viewWidth / 2) {
CGFloat leftWidth = maxX;
menuX = menuX > leftWidth ? menuX : leftWidth;
}
CGFloat minX = CGRectGetMinX(obj.frame);
if (minX > _viewWidth / 2) {
CGFloat width = (_viewWidth - minX);
rightWidth = rightWidth > width ? rightWidth : width;
}
}
}];
CGFloat navHeight = self.navigationController.navigationBar.frame.size.height;
menuHeight = self.menuHeight > navHeight ? navHeight : self.menuHeight;
menuY = (navHeight - menuHeight) / 2;
}
CGFloat menuWidth = _viewWidth - menuX - rightWidth;
self.menuView.frame = CGRectMake(menuX, menuY, menuWidth, menuHeight);
[self.menuView resetFrames];
}
- (CGFloat)wm_calculateItemWithAtIndex:(NSInteger)index {
NSString *title = [self titleAtIndex:index];
UIFont *titleFont = self.titleFontName ? [UIFont fontWithName:self.titleFontName size:self.titleSizeSelected] : [UIFont systemFontOfSize:self.titleSizeSelected];
NSDictionary *attrs = @{NSFontAttributeName: titleFont};
CGFloat itemWidth = [title boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:attrs context:nil].size.width;
return ceil(itemWidth);
}
- (void)wm_delaySelectIndexIfNeeded {
if (_markedSelectIndex != kWMUndefinedIndex) {
self.selectIndex = (int)_markedSelectIndex;
}
}
#pragma mark - Life Cycle
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
if (!self.childControllersCount) return;
[self wm_calculateSize];
[self wm_addScrollView];
[self wm_addViewControllerAtIndex:self.selectIndex];
self.currentViewController = self.displayVC[@(self.selectIndex)];
[self wm_addMenuView];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (!self.childControllersCount) return;
CGFloat oldSuperviewHeight = _superviewHeight;
_superviewHeight = self.view.frame.size.height;
if ((_hasInited && _superviewHeight == oldSuperviewHeight) || !self.view.window) return;
// 计算宽高及子控制器的视图frame
[self wm_calculateSize];
[self wm_adjustScrollViewFrame];
[self wm_adjustMenuViewFrame];
[self wm_adjustDisplayingViewControllersFrame];
[self wm_removeSuperfluousViewControllersIfNeeded];
_hasInited = YES;
[self.view layoutIfNeeded];
[self wm_delaySelectIndexIfNeeded];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (!self.childControllersCount) { return; }
[self wm_postFullyDisplayedNotificationWithCurrentIndex:self.selectIndex];
[self didEnterController:self.currentViewController atIndex:self.selectIndex];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
self.memoryWarningCount++;
self.cachePolicy = WMPageControllerCachePolicyLowMemory;
// 取消正在增长的 cache 操作
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(wm_growCachePolicyAfterMemoryWarning) object:nil];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(wm_growCachePolicyToHigh) object:nil];
[self.memCache removeAllObjects];
[self.posRecords removeAllObjects];
self.posRecords = nil;
// 如果收到内存警告次数小于 3,一段时间后切换到模式 Balanced
if (self.memoryWarningCount < 3) {
[self performSelector:@selector(wm_growCachePolicyAfterMemoryWarning) withObject:nil afterDelay:3.0 inModes:@[NSRunLoopCommonModes]];
}
}
#pragma mark - UIScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (![scrollView isKindOfClass:WMScrollView.class]) { return; }
if (_shouldNotScroll || !_hasInited) { return; }
[self wm_layoutChildViewControllers];
if (_startDragging) {
CGFloat contentOffsetX = scrollView.contentOffset.x;
if (contentOffsetX < 0) {
contentOffsetX = 0;
}
if (contentOffsetX > scrollView.contentSize.width - _viewWidth) {
contentOffsetX = scrollView.contentSize.width - _viewWidth;
}
CGFloat rate = contentOffsetX / _viewWidth;
[self.menuView slideMenuAtProgress:rate];
}
// Fix scrollView.contentOffset.y -> (-20) unexpectedly.
if (scrollView.contentOffset.y == 0) { return; }
CGPoint contentOffset = scrollView.contentOffset;
contentOffset.y = 0.0;
scrollView.contentOffset = contentOffset;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
if (![scrollView isKindOfClass:WMScrollView.class]) { return; }
_startDragging = YES;
self.menuView.userInteractionEnabled = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if (![scrollView isKindOfClass:WMScrollView.class]) { return; }
self.menuView.userInteractionEnabled = YES;
_selectIndex = (int)scrollView.contentOffset.x / _viewWidth;
[self wm_removeSuperfluousViewControllersIfNeeded];
self.currentViewController = self.displayVC[@(self.selectIndex)];
[self wm_postFullyDisplayedNotificationWithCurrentIndex:self.selectIndex];
[self didEnterController:self.currentViewController atIndex:self.selectIndex];
[self.menuView deselectedItemsIfNeeded];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
if (![scrollView isKindOfClass:WMScrollView.class]) { return; }
self.currentViewController = self.displayVC[@(self.selectIndex)];
[self wm_removeSuperfluousViewControllersIfNeeded];
[self wm_postFullyDisplayedNotificationWithCurrentIndex:self.selectIndex];
[self didEnterController:self.currentViewController atIndex:self.selectIndex];
[self.menuView deselectedItemsIfNeeded];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (![scrollView isKindOfClass:WMScrollView.class]) { return; }
if (!decelerate) {
self.menuView.userInteractionEnabled = YES;
CGFloat rate = _targetX / _viewWidth;
[self.menuView slideMenuAtProgress:rate];
[self.menuView deselectedItemsIfNeeded];
}
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
if (![scrollView isKindOfClass:WMScrollView.class]) { return; }
_targetX = targetContentOffset->x;
}
#pragma mark - WMMenuView Delegate
- (void)menuView:(WMMenuView *)menu didSelesctedIndex:(NSInteger)index currentIndex:(NSInteger)currentIndex {
if (!_hasInited) { return; }
_selectIndex = (int)index;
_startDragging = NO;
CGPoint targetP = CGPointMake(_viewWidth*index, 0);
[self.scrollView setContentOffset:targetP animated:self.pageAnimatable];
if (!self.pageAnimatable) {
// 由于不触发 -scrollViewDidScroll: 手动处理控制器
[self wm_removeSuperfluousViewControllersIfNeeded];
UIViewController *currentViewController = self.displayVC[@(currentIndex)];
if (currentViewController) {
[self wm_removeViewController:currentViewController atIndex:currentIndex];
}
[self wm_layoutChildViewControllers];
self.currentViewController = self.displayVC[@(self.selectIndex)];
[self wm_postFullyDisplayedNotificationWithCurrentIndex:(int)index];
[self didEnterController:self.currentViewController atIndex:index];
}
}
- (CGFloat)menuView:(WMMenuView *)menu widthForItemAtIndex:(NSInteger)index {
if (self.automaticallyCalculatesItemWidths) {
return [self wm_calculateItemWithAtIndex:index];
}
if (self.itemsWidths.count == self.childControllersCount) {
return [self.itemsWidths[index] floatValue];
}
return self.menuItemWidth;
}
- (CGFloat)menuView:(WMMenuView *)menu itemMarginAtIndex:(NSInteger)index {
if (self.itemsMargins.count == self.childControllersCount + 1) {
return [self.itemsMargins[index] floatValue];
}
return self.itemMargin;
}
- (CGFloat)menuView:(WMMenuView *)menu titleSizeForState:(WMMenuItemState)state {
switch (state) {
case WMMenuItemStateSelected: {
return self.titleSizeSelected;
break;
}
case WMMenuItemStateNormal: {
return self.titleSizeNormal;
break;
}
}
}
- (UIColor *)menuView:(WMMenuView *)menu titleColorForState:(WMMenuItemState)state {
switch (state) {
case WMMenuItemStateSelected: {
return self.titleColorSelected;
break;
}
case WMMenuItemStateNormal: {
return self.titleColorNormal;
break;
}
}
}
#pragma mark - WMMenuViewDataSource
- (NSInteger)numbersOfTitlesInMenuView:(WMMenuView *)menu {
return self.childControllersCount;
}
- (NSString *)menuView:(WMMenuView *)menu titleAtIndex:(NSInteger)index {
return [self titleAtIndex:index];
}
@end