新UI样式搭建

This commit is contained in:
2025-04-21 18:27:31 +08:00
parent 6819c7c9ee
commit 6dd3e19b8a
71 changed files with 2525 additions and 28 deletions

View File

@ -33,6 +33,8 @@ target 'Thimra' do
pod 'Kingfisher' #图片加载
pod 'EmptyStateKit' #空数据页面
pod 'ReachabilitySwift' #网络状态监控
pod 'WMZPageController' #分页控制器

View File

@ -15,6 +15,7 @@ PODS:
- SmartCodable (4.3.2)
- SnapKit (5.7.1)
- Toast (4.1.1)
- WMZPageController (1.5.5)
- YYKit (1.0.9):
- YYKit/no-arc (= 1.0.9)
- YYKit/no-arc (1.0.9)
@ -33,6 +34,7 @@ DEPENDENCIES:
- SmartCodable
- SnapKit
- Toast
- WMZPageController
- YYKit
- ZFPlayer/AVPlayer
@ -50,6 +52,7 @@ SPEC REPOS:
- SmartCodable
- SnapKit
- Toast
- WMZPageController
- YYKit
- ZFPlayer
@ -66,9 +69,10 @@ SPEC CHECKSUMS:
SmartCodable: 88fbf3d65207c2376fdbce4b080a3d578cb51be8
SnapKit: d612e99e678a2d3b95bf60b0705ed0a35c03484a
Toast: 1f5ea13423a1e6674c4abdac5be53587ae481c4e
WMZPageController: 87dd82d1e3528cd362de19b9a74fd6890d6e1906
YYKit: 7cda43304a8dc3696c449041e2cb3107b4e236e7
ZFPlayer: 5cf39e8d9f0c2394a014b0db4767b5b5a6bffe13
PODFILE CHECKSUM: 1ac1768b638088622600b82e65d3eb429873c448
PODFILE CHECKSUM: 6569d2af22714fb67d77646500c1ce1c95d63b3c
COCOAPODS: 1.16.2

View File

@ -28,12 +28,12 @@ extension AppDelegate {
let font = UIFont.fontRegular(ofSize: 10)
let normalColor = UIColor.color7F7F80()
let selectedColor = UIColor.colorFF0089()
let normalColor = UIColor.color828284()
let selectedColor = UIColor.colorFFFFFF()
let backgroundImage = UIImage(color: .clear)
let backgroundColor = UIColor.black
let backgroundColor = UIColor.color201A1A(alpha: 0.93)
let shadowImage = UIImage()
let shadowColor = UIColor.clear

View File

@ -14,7 +14,7 @@ class SPTabBarController: UITabBarController {
let nav1 = createNavigationController(viewController: SPHomePageController(), title: "Home".localized, image: UIImage(named: "tabbar_icon_01"), selectedImage: UIImage(named: "tabbar_icon_01_selected"))
let nav2 = createNavigationController(viewController: SPExploreViewController(), title: "For You".localized, image: UIImage(named: "tabbar_icon_02"), selectedImage: UIImage(named: "tabbar_icon_02_selected"))
let nav2 = createNavigationController(viewController: SPExplorePageController(), title: "For You".localized, image: UIImage(named: "tabbar_icon_02"), selectedImage: UIImage(named: "tabbar_icon_02_selected"))
let nav4 = createNavigationController(viewController: SPMyListViewController(), title: "My list".localized, image: UIImage(named: "tabbar_icon_04"), selectedImage: UIImage(named: "tabbar_icon_04_selected"))

View File

@ -29,11 +29,22 @@ class SPViewController: UIViewController, JYPageChildContollerProtocol {
return imageView;
}()
///
private lazy var toGradientView: SPGradientView = {
let view = SPGradientView()
view.colors = [UIColor.color290D0F().cgColor, UIColor.color230E11().cgColor, UIColor.color181115().cgColor, UIColor.color121317(alpha: 0).cgColor]
view.startPoint = .init(x: 0.5, y: 0)
view.endPoint = .init(x: 0.5, y: 1)
view.locations = [0, 0.33, 0.66, 1]
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
self.isViewDidLoad = true
self.edgesForExtendedLayout = []
setBgImageView()
if let navi = navigationController {
@ -46,8 +57,14 @@ class SPViewController: UIViewController, JYPageChildContollerProtocol {
}
func setBgImageView() {
self.view = self._bgImageView
self.view.backgroundColor = .black
self.view.backgroundColor = .color121317()
self.view.addSubview(toGradientView)
toGradientView.snp.makeConstraints { make in
make.left.right.top.equalToSuperview()
make.height.equalTo(kSPStatusbarHeight + 420)
}
}
override func viewDidAppear(_ animated: Bool) {

View File

@ -84,5 +84,29 @@ extension UIColor {
static func color888888(alpha: CGFloat = 1) -> UIColor {
return color(hex: 0x888888, alpha: alpha)
}
static func color201A1A(alpha: CGFloat = 1) -> UIColor {
return color(hex: 0x201A1A, alpha: alpha)
}
static func color828284(alpha: CGFloat = 1) -> UIColor {
return color(hex: 0x828284, alpha: alpha)
}
static func color121317(alpha: CGFloat = 1) -> UIColor {
return color(hex: 0x121317, alpha: alpha)
}
static func color290D0F(alpha: CGFloat = 1) -> UIColor {
return color(hex: 0x290D0F, alpha: alpha)
}
static func color230E11(alpha: CGFloat = 1) -> UIColor {
return color(hex: 0x230E11, alpha: alpha)
}
static func color181115(alpha: CGFloat = 1) -> UIColor {
return color(hex: 0x181115, alpha: alpha)
}
}

View File

@ -18,11 +18,12 @@ import UIKit
https://api-zyreotv.zyreotv.com/7834f11d/
https://thimratv.com
https://api-thimratv.thimratv.com
*/
#if DEBUG
let SPBaseURL = "https://test1-api.guyantv.com"
//let SPBaseURL = "https://api-thimratv.thimratv.com"
let SPURLPathPrefix = ""
//let SPBaseURL = "https://api-mireotv.mireotv.com"
//let SPURLPathPrefix = "/4da6fd4c"

View File

@ -0,0 +1,138 @@
//
// SPExplorePageController.swift
// Thimra
//
// Created by Overseas on 2025/4/21.
//
import UIKit
class SPExplorePageController: SPViewController {
private lazy var titles: [String] = {
let arr = ["Shorts".localized, "Featured".localized, "All".localized]
return arr
}()
private lazy var itemWidthArr: [NSNumber] = {
var arr: [NSNumber] = []
self.titles.forEach {
var width = $0.size(font: .systemFont(ofSize: 14)).width + 16
if width < 50 {
width = 50
}
arr.append(NSNumber(value: width))
}
return arr
}()
private lazy var menuWidth: CGFloat = {
var width: CGFloat = 0.0
itemWidthArr.forEach {
width += CGFloat($0.floatValue)
}
return width
}()
private lazy var pageView: WMPageController = {
var itemWidthArr: [NSNumber] = []
self.titles.forEach {
var width = $0.size(font: .systemFont(ofSize: 14)).width + 16
if width < 50 {
width = 50
}
itemWidthArr.append(NSNumber(value: width))
}
let pageView = WMPageController()
pageView.delegate = self
pageView.dataSource = self
pageView.titleSizeNormal = 14
pageView.titleSizeSelected = 14
pageView.titleColorNormal = .colorFFFFFF()
pageView.titleColorSelected = .colorFFFFFF()
pageView.progressColor = .colorFFFFFF(alpha: 0.22)
pageView.menuViewStyle = .flood
pageView.menuViewLayoutMode = .center
pageView.itemsWidths = itemWidthArr
pageView.progressHeight = 24
return pageView
}()
private lazy var menuBgView: UIView = {
let view = UIView()
view.addEffectView(style: .light)
view.layer.cornerRadius = 15
view.layer.masksToBounds = true
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
_setupUI()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(true, animated: true)
}
}
extension SPExplorePageController {
private func _setupUI() {
addChild(pageView)
self.view.addSubview(self.pageView.view)
self.pageView.view.addSubview(menuBgView)
self.pageView.view.bringSubviewToFront(self.pageView.menuView!)
self.pageView.view.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
menuBgView.snp.makeConstraints { make in
make.center.equalTo(self.pageView.menuView!)
make.width.equalTo(menuWidth + 8)
make.height.equalTo(30)
}
}
}
//MARK: -------------- WMPageControllerDelegate & WMPageControllerDataSource --------------
extension SPExplorePageController: WMPageControllerDelegate, WMPageControllerDataSource {
func pageController(_ pageController: WMPageController, preferredFrameForContentView contentView: WMScrollView) -> CGRect {
return CGRect(x: 0, y: 0, width: kSPScreenWidth, height: kSPScreenHeight - kSPTabBarHeight)
}
func pageController(_ pageController: WMPageController, preferredFrameFor menuView: WMMenuView) -> CGRect {
return CGRect(x: 0, y: kSPStatusbarHeight + 10, width: kSPScreenWidth, height: 30)
}
func pageController(_ pageController: WMPageController, titleAt index: Int) -> String {
return titles[index]
}
func numbersOfChildControllers(in pageController: WMPageController) -> Int {
return titles.count
}
func pageController(_ pageController: WMPageController, viewControllerAt index: Int) -> UIViewController {
return SPExploreViewController()
}
}

View File

@ -5,12 +5,12 @@
"scale" : "1x"
},
{
"filename" : "home@2x.png",
"filename" : "Frame@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "home@3x.png",
"filename" : "Frame@3x.png",
"idiom" : "universal",
"scale" : "3x"
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 935 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

View File

@ -5,12 +5,12 @@
"scale" : "1x"
},
{
"filename" : "home@2x.png",
"filename" : "Frame@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "home@3x.png",
"filename" : "Frame@3x.png",
"idiom" : "universal",
"scale" : "3x"
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.4 KiB

View File

@ -5,12 +5,12 @@
"scale" : "1x"
},
{
"filename" : "Explore@2x.png",
"filename" : "Frame@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Explore@3x.png",
"filename" : "Frame@3x.png",
"idiom" : "universal",
"scale" : "3x"
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1023 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -5,12 +5,12 @@
"scale" : "1x"
},
{
"filename" : "Explore@2x.png",
"filename" : "Frame@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Explore@3x.png",
"filename" : "Frame@3x.png",
"idiom" : "universal",
"scale" : "3x"
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

View File

@ -5,12 +5,12 @@
"scale" : "1x"
},
{
"filename" : "My list@2x.png",
"filename" : "Frame@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "My list@3x.png",
"filename" : "Frame@3x.png",
"idiom" : "universal",
"scale" : "3x"
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 810 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 872 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -5,12 +5,12 @@
"scale" : "1x"
},
{
"filename" : "My list@2x.png",
"filename" : "Frame@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "My list@3x.png",
"filename" : "Frame@3x.png",
"idiom" : "universal",
"scale" : "3x"
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.2 KiB

View File

@ -5,12 +5,12 @@
"scale" : "1x"
},
{
"filename" : "Me@2x.png",
"filename" : "Frame@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Me@3x.png",
"filename" : "Frame@3x.png",
"idiom" : "universal",
"scale" : "3x"
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

View File

@ -5,12 +5,12 @@
"scale" : "1x"
},
{
"filename" : "Me@2x.png",
"filename" : "Frame@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Me@3x.png",
"filename" : "Frame@3x.png",
"idiom" : "universal",
"scale" : "3x"
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.1 KiB

View File

@ -5,12 +5,12 @@
"scale" : "1x"
},
{
"filename" : "收藏@2x.png",
"filename" : "Frame@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "收藏@3x.png",
"filename" : "Frame@3x.png",
"idiom" : "universal",
"scale" : "3x"
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 836 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.3 KiB

View File

@ -5,12 +5,12 @@
"scale" : "1x"
},
{
"filename" : "收藏@2x.png",
"filename" : "Frame@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "收藏@3x.png",
"filename" : "Frame@3x.png",
"idiom" : "universal",
"scale" : "3x"
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.3 KiB

View File

@ -13,3 +13,5 @@
#import <KTVHTTPCache.h>
#import <HWPanModal/HWPanModal.h>
#import <MJRefresh/MJRefresh.h>
#import <WMZPageController/WMZPageController.h>
#import "WMPageController.h"

View File

@ -0,0 +1,20 @@
//
// UIViewController+WMPageController.h
// WMPageController
//
// Created by Mark on 15/6/11.
// Copyright (c) 2015年 yq. All rights reserved.
//
#import <UIKit/UIKit.h>
@class WMPageController;
@interface UIViewController (WMPageController)
/**
WMPageController
*/
@property (nonatomic, nullable, strong, readonly) WMPageController *wm_pageController;
@end

View File

@ -0,0 +1,25 @@
//
// UIViewController+WMPageController.m
// WMPageController
//
// Created by Mark on 15/6/11.
// Copyright (c) 2015 yq. All rights reserved.
//
#import "UIViewController+WMPageController.h"
#import "WMPageController.h"
@implementation UIViewController (WMPageController)
- (WMPageController *)wm_pageController {
UIViewController *parentViewController = self.parentViewController;
while (parentViewController) {
if ([parentViewController isKindOfClass:[WMPageController class]]) {
return (WMPageController *)parentViewController;
}
parentViewController = parentViewController.parentViewController;
}
return nil;
}
@end

View File

@ -0,0 +1,37 @@
//
// 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,
};
NS_ASSUME_NONNULL_BEGIN
@protocol WMMenuItemDelegate <NSObject>
@optional
- (void)didPressedMenuItem:(WMMenuItem *)menuItem;
@end
@interface WMMenuItem : UILabel
@property (nonatomic, assign) CGFloat rate; ///> 设置 rate, 并刷新标题状态 (0~1)
@property (nonatomic, assign) CGFloat normalSize; ///> Normal状态的字体大小默认大小为15
@property (nonatomic, assign) CGFloat selectedSize; ///> Selected状态的字体大小默认大小为18
@property (nonatomic, strong) UIColor *normalColor; ///> Normal状态的字体颜色默认为黑色 (可动画)
@property (nonatomic, strong) UIColor *selectedColor; ///> Selected状态的字体颜色默认为红色 (可动画)
@property (nonatomic, assign) CGFloat speedFactor; ///> 进度条的速度因数,默认 15越小越快, 必须大于0
@property (nonatomic, nullable, weak) id<WMMenuItemDelegate> delegate;
@property (nonatomic, assign, readonly) BOOL selected;
- (void)setSelected:(BOOL)selected withAnimation:(BOOL)animation;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,108 @@
//
// WMMenuItem.m
// WMPageController
//
// Created by Mark on 15/4/26.
// Copyright (c) 2015 yq. All rights reserved.
//
#import "WMMenuItem.h"
@implementation WMMenuItem {
CGFloat _selectedRed, _selectedGreen, _selectedBlue, _selectedAlpha;
CGFloat _normalRed, _normalGreen, _normalBlue, _normalAlpha;
int _sign;
CGFloat _gap;
CGFloat _step;
__weak CADisplayLink *_link;
}
#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;
[self setupGestureRecognizer];
}
return self;
}
- (CGFloat)speedFactor {
if (_speedFactor <= 0) {
_speedFactor = 15.0;
}
return _speedFactor;
}
- (void)setupGestureRecognizer {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchUpInside:)];
[self addGestureRecognizer:tap];
}
- (void)setSelected:(BOOL)selected withAnimation:(BOOL)animation {
_selected = selected;
if (!animation) {
self.rate = selected ? 1.0 : 0.0;
return;
}
_sign = (selected == YES) ? 1 : -1;
_gap = (selected == YES) ? (1.0 - self.rate) : (self.rate - 0.0);
_step = _gap / self.speedFactor;
if (_link) {
[_link invalidate];
}
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 {
if (rate < 0.0 || rate > 1.0) { return; }
_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)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];
}
- (void)touchUpInside:(id)sender {
if ([self.delegate respondsToSelector:@selector(didPressedMenuItem:)]) {
[self.delegate didPressedMenuItem:self];
}
}
@end

View File

@ -0,0 +1,110 @@
//
// 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
- (BOOL)menuView:(WMMenuView *)menu shouldSelesctedIndex:(NSInteger)index;
- (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 atIndex:(NSInteger)index;
- (UIColor *)menuView:(WMMenuView *)menu titleColorForState:(WMMenuItemState)state atIndex:(NSInteger)index;
- (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, weak) UIScrollView *scrollView;
/** 进度条的速度因数,默认为 15越小越快 大于 0 */
@property (nonatomic, assign) CGFloat speedFactor;
@property (nonatomic, assign) CGFloat progressViewCornerRadius;
@property (nonatomic, assign) BOOL progressViewIsNaughty;
@property (nonatomic, assign) BOOL showOnNavigationBar;
- (void)slideMenuAtProgress:(CGFloat)progress;
- (void)selectItemAtIndex:(NSInteger)index;
- (void)resetFrames;
- (void)reload;
- (void)updateTitle:(NSString *)title atIndex:(NSInteger)index andWidth:(BOOL)update;
- (void)updateAttributeTitle:(NSAttributedString *)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

View File

@ -0,0 +1,578 @@
//
// 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, assign) NSInteger selectIndex;
@property (nonatomic, readonly) NSInteger titlesCount;
@end
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 {
// Adapt iOS 11 if is a titleView
if (@available(iOS 11.0, *)) {
if (self.showOnNavigationBar) { frame.origin.x = 0; }
}
[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 colorForState:WMMenuItemStateSelected atIndex:0];
}
return _lineColor;
}
- (NSMutableArray *)frames {
if (_frames == nil) {
_frames = [NSMutableArray array];
}
return _frames;
}
- (UIColor *)colorForState:(WMMenuItemState)state atIndex:(NSInteger)index {
if ([self.delegate respondsToSelector:@selector(menuView:titleColorForState:atIndex:)]) {
return [self.delegate menuView:self titleColorForState:state atIndex:index];
}
return [UIColor blackColor];
}
- (CGFloat)sizeForState:(WMMenuItemState)state atIndex:(NSInteger)index {
if ([self.delegate respondsToSelector:@selector(menuView:titleSizeForState:atIndex:)]) {
return [self.delegate menuView:self titleSizeForState:state atIndex:index];
}
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 setSelected:NO withAnimation:NO];
self.selItem = currentItem;
[self.selItem setSelected:YES withAnimation:NO];
[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 setSelected:NO withAnimation:NO];
self.selItem = item;
[self.selItem setSelected:YES withAnimation:NO];
[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)updateAttributeTitle:(NSAttributedString *)title atIndex:(NSInteger)index andWidth:(BOOL)update {
if (index >= self.titlesCount || index < 0) { return; }
WMMenuItem *item = (WMMenuItem *)[self viewWithTag:(WMMenuItemTagOffset + index)];
item.attributedText = 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 {
[super willMoveToSuperview:newSuperview];
if (self.scrollView) { return; }
[self addScrollView];
[self addItems];
[self makeStyle];
[self addBadgeViews];
[self resetSelectionIfNeeded];
}
- (void)resetSelectionIfNeeded {
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];
}
- (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 = CGRectZero;
if (self.style == WMMenuViewStyleDefault) { return; }
if (self.style == WMMenuViewStyleLine || self.style == WMMenuViewStyleTriangle) {
self.progressHeight = self.progressHeight > 0 ? self.progressHeight : 2.0;
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;
}
frame.size.width = self.scrollView.contentSize.width;
self.progressView.frame = frame;
self.progressView.cornerRadius = self.progressViewCornerRadius;
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];
NSInteger count = (self.frames.count <= self.progressWidths.count) ? self.frames.count : self.progressWidths.count;
for (int i = 0; i < 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 : 2.0;
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 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 setSelected:NO withAnimation:NO];
}];
}
- (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];
item.tag = (i + WMMenuItemTagOffset);
item.delegate = self;
item.text = [self.dataSource menuView:self titleAtIndex:i];
item.textAlignment = NSTextAlignmentCenter;
item.userInteractionEnabled = YES;
item.backgroundColor = [UIColor clearColor];
item.normalSize = [self sizeForState:WMMenuItemStateNormal atIndex:i];
item.selectedSize = [self sizeForState:WMMenuItemStateSelected atIndex:i];
item.normalColor = [self colorForState:WMMenuItemStateNormal atIndex:i];
item.selectedColor = [self colorForState:WMMenuItemStateSelected atIndex:i];
item.speedFactor = self.speedFactor;
if (self.fontName) {
item.font = [UIFont fontWithName:self.fontName size:item.selectedSize];
} else {
item.font = [UIFont systemFontOfSize:item.selectedSize];
}
if ([self.dataSource respondsToSelector:@selector(menuView:initialMenuItem:atIndex:)]) {
item = [self.dataSource menuView:self initialMenuItem:item atIndex:i];
}
if (i == 0) {
[item setSelected:YES withAnimation:NO];
self.selItem = item;
} else {
[item setSelected:NO withAnimation:NO];
}
[self.scrollView addSubview:item];
}
}
// itemframeitem
// `-addItems`
- (void)calculateItemFrames {
CGFloat contentWidth = [self itemMarginAtIndex:0];
for (int i = 0; i < self.titlesCount; i++) {
CGFloat itemW = 60.0;
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)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.delegate respondsToSelector:@selector(menuView:shouldSelesctedIndex:)]) {
BOOL should = [self.delegate menuView:self shouldSelesctedIndex:menuItem.tag - WMMenuItemTagOffset];
if (!should) {
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];
}
[self.selItem setSelected:NO withAnimation:YES];
[menuItem setSelected:YES withAnimation:YES];
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

View File

@ -0,0 +1,29 @@
//
// WMProgressView.h
// WMPageController
//
// Created by Mark on 15/6/20.
// Copyright (c) 2015年 yq. All rights reserved.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@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
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,145 @@
//
// 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];
}
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

View File

@ -0,0 +1,13 @@
//
// 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>
@end

View File

@ -0,0 +1,23 @@
//
// 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 shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
//MARK: UITableViewCell
if ([NSStringFromClass(otherGestureRecognizer.view.class) isEqualToString:@"UITableViewWrapperView"] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
return YES;
}
return NO;
}
@end

View File

@ -0,0 +1,360 @@
//
// 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(NSInteger, WMPageControllerCachePolicy) {
WMPageControllerCachePolicyDisabled = -1, // Disable Cache
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.
};
NS_ASSUME_NONNULL_BEGIN
extern NSString *const WMControllerDidAddToSuperViewNotification;
extern NSString *const WMControllerDidFullyDisplayedNotification;
@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 *)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 *)pageController:(WMPageController *)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 *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index;
@required
/**
Implement this datasource method, in order to customize your own contentView's frame
@param pageController The container controller
@param contentView The contentView, each is the superview of the child controllers
@return The frame of the contentView
*/
- (CGRect)pageController:(WMPageController *)pageController preferredFrameForContentView:(WMScrollView *)contentView;
/**
Implement this datasource method, in order to customize your own menuView's frame
@param pageController The container controller
@param menuView The menuView
@return The frame of the menuView
*/
- (CGRect)pageController:(WMPageController *)pageController preferredFrameForMenuView:(WMMenuView *)menuView;
@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 *)pageController lazyLoadViewController:(__kindof UIViewController *)viewController withInfo:(NSDictionary *)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 *)pageController willCachedViewController:(__kindof UIViewController *)viewController withInfo:(NSDictionary *)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 *)pageController willEnterViewController:(__kindof UIViewController *)viewController withInfo:(NSDictionary *)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 *)pageController didEnterViewController:(__kindof UIViewController *)viewController withInfo:(NSDictionary *)info;
@end
@interface WMPageController : UIViewController <WMMenuViewDelegate, WMMenuViewDataSource, UIScrollViewDelegate, WMPageControllerDataSource, WMPageControllerDelegate>
@property (nonatomic, weak) id<WMPageControllerDelegate> delegate;
@property (nonatomic, weak) id<WMPageControllerDataSource> dataSource;
/**
* Values and keys can set properties when initialize child controlelr (it's KVC)
* values keys ( KVC )
使 key ( type keys @"type")
*/
@property (nonatomic, nullable, strong) NSMutableArray<id> *values;
@property (nonatomic, nullable, strong) NSMutableArray<NSString *> *keys;
/**
* class, :[UITableViewController class]
* Each controller's class, example:[UITableViewController class]
*/
@property (nonatomic, nullable, copy) NSArray<Class> *viewControllerClasses;
/**
*
* Titles of view controllers in page controller.
*/
@property (nonatomic, nullable, copy) NSArray<NSString *> *titles;
@property (nonatomic, strong, readonly) UIViewController *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 *titleColorSelected;
/**
* , .
* The title's normal color, the color is animatable.
*/
@property (nonatomic, strong) UIColor *titleColorNormal;
/**
*
* The name of title's font
*/
@property (nonatomic, nullable, copy) NSString *titleFontName;
/**
* 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, nullable, copy) NSArray<NSNumber *> *itemsWidths;
/**
* 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, nullable, strong) UIColor *progressColor;
/**
* item
*/
@property (nonatomic, nullable, strong) NSArray *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;
/**
* Menu view items' margin / make sure it's count is equal to (controllers' count + 1),default is 0
item + 1, 0
*/
@property (nonatomic, nullable, copy) NSArray<NSNumber *> *itemsMargins;
/**
* set itemMargin if all margins are the same, default is 0
0
*/
@property (nonatomic, assign) CGFloat itemMargin;
/** progressView 到 menuView 底部的距离 */
@property (nonatomic, assign) CGFloat progressViewBottomSpace;
/** progressView's cornerRadius */
@property (nonatomic, assign) CGFloat progressViewCornerRadius;
/** 顶部导航栏 */
@property (nonatomic, nullable, weak) WMMenuView *menuView;
/** 内部容器 */
@property (nonatomic, nullable, weak) WMScrollView *scrollView;
/** MenuView 内部视图与左右的间距 */
@property (nonatomic, assign) CGFloat menuViewContentMargin;
/**
* 使. . /
* 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)initWithViewControllerClasses:(NSArray<Class> *)classes andTheirTitles:(NSArray<NSString *> *)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;
/**
Layout all views in WMPageController
@discussion This method will recall `-pageController:preferredFrameForContentView:` and `-pageContoller:preferredFrameForMenuView:`
*/
- (void)forceLayoutSubviews;
/**
* Update designated item's title
*
* @param title
* @param index
*/
- (void)updateTitle:(NSString *)title atIndex:(NSInteger)index;
/**
* Update designated item's title and width
*
* @param title
* @param index
* @param width item的新宽度
*/
- (void)updateTitle:(NSString *)title andWidth:(CGFloat)width atIndex:(NSInteger)index;
- (void)updateAttributeTitle:(NSAttributedString *)title atIndex:(NSInteger)index;
/** 当 app 即将进入后台接收到的通知 */
- (void)willResignActive:(NSNotification *)notification;
/** 当 app 即将回到前台接收到的通知 */
- (void)willEnterForeground:(NSNotification *)notification;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,861 @@
//
// WMPageController.m
// WMPageController
//
// Created by Mark on 15/6/11.
// Copyright (c) 2015 yq. All rights reserved.
//
#import "WMPageController.h"
NSString *const WMControllerDidAddToSuperViewNotification = @"WMControllerDidAddToSuperViewNotification";
NSString *const WMControllerDidFullyDisplayedNotification = @"WMControllerDidFullyDisplayedNotification";
static NSInteger const kWMUndefinedIndex = -1;
static NSInteger const kWMControllerCountUndefined = -1;
@interface WMPageController () {
CGFloat _targetX;
CGRect _contentViewFrame, _menuViewFrame;
BOOL _hasInited, _shouldNotScroll;
NSInteger _initializedIndex, _controllerCount, _markedSelectIndex;
}
@property (nonatomic, strong, readwrite) UIViewController *currentViewController;
// viewframe scrollView
@property (nonatomic, strong) NSMutableArray *childViewFrames;
// 便 ()
@property (nonatomic, strong) NSMutableDictionary *displayVC;
// viewController (scrollViewController)
@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)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self wm_setup];
}
return self;
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
[self wm_setup];
}
return self;
}
- (instancetype)initWithViewControllerClasses:(NSArray<Class> *)classes andTheirTitles:(NSArray<NSString *> *)titles {
if (self = [self initWithNibName:nil bundle:nil]) {
NSParameterAssert(classes.count == titles.count);
_viewControllerClasses = [NSArray arrayWithArray:classes];
_titles = [NSArray arrayWithArray:titles];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(wm_growCachePolicyAfterMemoryWarning) object:nil];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(wm_growCachePolicyToHigh) object:nil];
}
- (void)forceLayoutSubviews {
if (!self.childControllersCount) return;
// frame
[self wm_calculateSize];
[self wm_adjustScrollViewFrame];
[self wm_adjustMenuViewFrame];
[self wm_adjustDisplayingViewControllersFrame];
}
- (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;
if (cachePolicy != WMPageControllerCachePolicyDisabled) {
self.memCache.countLimit = _cachePolicy;
}
}
- (void)setSelectIndex:(int)selectIndex {
_selectIndex = selectIndex;
_markedSelectIndex = kWMUndefinedIndex;
if (self.menuView && _hasInited) {
[self.menuView selectItemAtIndex:selectIndex];
} else {
_markedSelectIndex = selectIndex;
UIViewController *vc = [self.memCache objectForKey:@(selectIndex)];
if (!vc) {
vc = [self initializeViewControllerAtIndex:selectIndex];
[self.memCache setObject:vc forKey:@(selectIndex)];
}
self.currentViewController = vc;
}
}
- (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)reloadData {
[self wm_clearDatas];
if (!self.childControllersCount) return;
[self wm_resetScrollView];
[self.memCache removeAllObjects];
[self wm_resetMenuView];
[self viewDidLayoutSubviews];
[self didEnterController:self.currentViewController atIndex:self.selectIndex];
}
- (void)updateTitle:(NSString *)title atIndex:(NSInteger)index {
[self.menuView updateTitle:title atIndex:index andWidth:NO];
}
- (void)updateAttributeTitle:(NSAttributedString * _Nonnull)title atIndex:(NSInteger)index {
[self.menuView updateAttributeTitle: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];
}
- (void)setShowOnNavigationBar:(BOOL)showOnNavigationBar {
if (_showOnNavigationBar == showOnNavigationBar) {
return;
}
_showOnNavigationBar = showOnNavigationBar;
if (self.menuView) {
[self.menuView removeFromSuperview];
[self wm_addMenuView];
[self forceLayoutSubviews];
[self.menuView slideMenuAtProgress:self.selectIndex];
}
}
#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 ?: @"", @"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;
// Post FullyDisplayedNotification
[self wm_postFullyDisplayedNotificationWithCurrentIndex:self.selectIndex];
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 length = (int)self.preloadPolicy;
int start = 0;
int end = (int)self.childControllersCount - 1;
if (index > length) {
start = (int)index - length;
}
if (self.childControllersCount - 1 > length + index) {
end = (int)index + length;
}
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 (_controllerCount == kWMControllerCountUndefined) {
if ([self.dataSource respondsToSelector:@selector(numbersOfChildControllersInPageController:)]) {
_controllerCount = [self.dataSource numbersOfChildControllersInPageController:self];
} else {
_controllerCount = self.viewControllerClasses.count;
}
}
return _controllerCount;
}
- (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 ?: @"");
}
#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 {
_controllerCount = kWMControllerCountUndefined;
_hasInited = NO;
NSUInteger maxIndex = (self.childControllersCount - 1 > 0) ? (self.childControllersCount - 1) : 0;
_selectIndex = self.selectIndex < self.childControllersCount ? self.selectIndex : (int)maxIndex;
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:self
userInfo:info];
}
// user
- (void)wm_postFullyDisplayedNotificationWithCurrentIndex:(int)index {
if (!self.postNotification) return;
NSDictionary *info = @{
@"index":@(index),
@"title":[self titleAtIndex:index]
};
[[NSNotificationCenter defaultCenter] postNotificationName:WMControllerDidFullyDisplayedNotification
object:self
userInfo:info];
}
// init
- (void)wm_setup {
_titleSizeSelected = 18.0f;
_titleSizeNormal = 15.0f;
_titleColorSelected = [UIColor colorWithRed:168.0/255.0 green:20.0/255.0 blue:4/255.0 alpha:1];
_titleColorNormal = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
_menuItemWidth = 65.0f;
_memCache = [[NSCache alloc] init];
_initializedIndex = kWMUndefinedIndex;
_markedSelectIndex = kWMUndefinedIndex;
_controllerCount = 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 {
_menuViewFrame = [self.dataSource pageController:self preferredFrameForMenuView:self.menuView];
_contentViewFrame = [self.dataSource pageController:self preferredFrameForContentView:self.scrollView];
_childViewFrames = [NSMutableArray array];
for (int i = 0; i < self.childControllersCount; i++) {
CGRect frame = CGRectMake(i * _contentViewFrame.size.width, 0, _contentViewFrame.size.width, _contentViewFrame.size.height);
[_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.scrollEnabled = self.scrollEnable;
if (@available(iOS 11.0, *)) {
scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
[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 {
WMMenuView *menuView = [[WMMenuView alloc] initWithFrame:CGRectZero];
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;
menuView.showOnNavigationBar = self.showOnNavigationBar;
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 / _contentViewFrame.size.width);
int length = (int)self.preloadPolicy;
int left = currentPage - length - 1;
int right = currentPage + length + 1;
for (int i = 0; i < self.childControllersCount; i++) {
UIViewController *vc = [self.displayVC objectForKey:@(i)];
CGRect frame = [self.childViewFrames[i] CGRectValue];
if (!vc) {
if ([self wm_isInScreen:frame]) {
[self wm_initializedControllerWithIndexIfNeeded:i];
}
} else if (i <= left || i >= right) {
if (![self wm_isInScreen:frame]) {
[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_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.cachePolicy == WMPageControllerCachePolicyDisabled) {
return;
}
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]]) {
// ControllerviewscrollView(UITableViewController/UIViewControllerviewscrollView)
scrollView = (UIScrollView *)controller.view;
} else if (controller.view.subviews.count >= 1) {
// ControllerviewsubViews[0]scrollViewframeviewframe(UICollectionViewController/UIViewControllerUIScrollView)
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;
CGFloat oldContentOffsetX = self.scrollView.contentOffset.x;
CGFloat contentWidth = self.scrollView.contentSize.width;
self.scrollView.frame = _contentViewFrame;
self.scrollView.contentSize = CGSizeMake(self.childControllersCount * _contentViewFrame.size.width, 0);
CGFloat xContentOffset = contentWidth == 0 ? self.selectIndex * _contentViewFrame.size.width : oldContentOffsetX / contentWidth * self.childControllersCount * _contentViewFrame.size.width;
[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 {
CGFloat oriWidth = self.menuView.frame.size.width;
self.menuView.frame = _menuViewFrame;
[self.menuView resetFrames];
if (oriWidth != self.menuView.frame.size.width) {
[self.menuView refreshContenOffset];
}
}
- (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_initializedControllerWithIndexIfNeeded:self.selectIndex];
self.currentViewController = self.displayVC[@(self.selectIndex)];
[self wm_addMenuView];
[self didEnterController:self.currentViewController atIndex:self.selectIndex];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (!self.childControllersCount) return;
[self forceLayoutSubviews];
_hasInited = YES;
[self wm_delaySelectIndexIfNeeded];
}
- (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 - _contentViewFrame.size.width) {
contentOffsetX = scrollView.contentSize.width - _contentViewFrame.size.width;
}
CGFloat rate = contentOffsetX / _contentViewFrame.size.width;
[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 / _contentViewFrame.size.width);
self.currentViewController = self.displayVC[@(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 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 / _contentViewFrame.size.width;
[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(_contentViewFrame.size.width * index, 0);
[self.scrollView setContentOffset:targetP animated:self.pageAnimatable];
if (self.pageAnimatable) return;
// -scrollViewDidScroll:
UIViewController *currentViewController = self.displayVC[@(currentIndex)];
if (currentViewController) {
[self wm_removeViewController:currentViewController atIndex:currentIndex];
}
[self wm_layoutChildViewControllers];
self.currentViewController = self.displayVC[@(self.selectIndex)];
[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 atIndex:(NSInteger)index {
switch (state) {
case WMMenuItemStateSelected: return self.titleSizeSelected;
case WMMenuItemStateNormal: return self.titleSizeNormal;
}
}
- (UIColor *)menuView:(WMMenuView *)menu titleColorForState:(WMMenuItemState)state atIndex:(NSInteger)index {
switch (state) {
case WMMenuItemStateSelected: return self.titleColorSelected;
case WMMenuItemStateNormal: return self.titleColorNormal;
}
}
#pragma mark - WMMenuViewDataSource
- (NSInteger)numbersOfTitlesInMenuView:(WMMenuView *)menu {
return self.childControllersCount;
}
- (NSString *)menuView:(WMMenuView *)menu titleAtIndex:(NSInteger)index {
return [self titleAtIndex:index];
}
#pragma mark - WMPageControllerDataSource
- (CGRect)pageController:(WMPageController *)pageController preferredFrameForMenuView:(WMMenuView *)menuView {
NSAssert(0, @"[%@] MUST IMPLEMENT DATASOURCE METHOD `-pageController:preferredFrameForMenuView:`", [self.dataSource class]);
return CGRectZero;
}
- (CGRect)pageController:(WMPageController *)pageController preferredFrameForContentView:(WMScrollView *)contentView {
NSAssert(0, @"[%@] MUST IMPLEMENT DATASOURCE METHOD `-pageController:preferredFrameForContentView:`", [self.dataSource class]);
return CGRectZero;
}
@end