diff --git a/Podfile b/Podfile index b483f89..195ef0a 100644 --- a/Podfile +++ b/Podfile @@ -33,6 +33,8 @@ target 'Thimra' do pod 'Kingfisher' #图片加载 pod 'EmptyStateKit' #空数据页面 pod 'ReachabilitySwift' #网络状态监控 + pod 'WMZPageController' #分页控制器 + diff --git a/Podfile.lock b/Podfile.lock index 9506ec1..4c78339 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -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 diff --git a/Thimra/AppDelegate/AppDelegate+Config.swift b/Thimra/AppDelegate/AppDelegate+Config.swift index fbef84c..b399337 100644 --- a/Thimra/AppDelegate/AppDelegate+Config.swift +++ b/Thimra/AppDelegate/AppDelegate+Config.swift @@ -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 diff --git a/Thimra/Base/Controller/SPTabBarController.swift b/Thimra/Base/Controller/SPTabBarController.swift index 921647b..aae513e 100644 --- a/Thimra/Base/Controller/SPTabBarController.swift +++ b/Thimra/Base/Controller/SPTabBarController.swift @@ -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")) diff --git a/Thimra/Base/Controller/SPViewController.swift b/Thimra/Base/Controller/SPViewController.swift index a88e1f2..02a0a15 100644 --- a/Thimra/Base/Controller/SPViewController.swift +++ b/Thimra/Base/Controller/SPViewController.swift @@ -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) { diff --git a/Thimra/Base/Extension/UIColor+SPAdd.swift b/Thimra/Base/Extension/UIColor+SPAdd.swift index 3a07a02..87c0b0e 100644 --- a/Thimra/Base/Extension/UIColor+SPAdd.swift +++ b/Thimra/Base/Extension/UIColor+SPAdd.swift @@ -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) + } } diff --git a/Thimra/Base/Networking/Base/SPURLPath.swift b/Thimra/Base/Networking/Base/SPURLPath.swift index 3735547..1943545 100644 --- a/Thimra/Base/Networking/Base/SPURLPath.swift +++ b/Thimra/Base/Networking/Base/SPURLPath.swift @@ -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" diff --git a/Thimra/Class/Explore/Controller/SPExplorePageController.swift b/Thimra/Class/Explore/Controller/SPExplorePageController.swift new file mode 100644 index 0000000..44fa9c6 --- /dev/null +++ b/Thimra/Class/Explore/Controller/SPExplorePageController.swift @@ -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() + } + + + + +} diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01.imageset/Contents.json b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01.imageset/Contents.json index b60f645..6aa90f6 100644 --- a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01.imageset/Contents.json +++ b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01.imageset/Contents.json @@ -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" } diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01.imageset/Frame@2x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01.imageset/Frame@2x.png new file mode 100644 index 0000000..2898f0e Binary files /dev/null and b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01.imageset/Frame@2x.png differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01.imageset/Frame@3x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01.imageset/Frame@3x.png new file mode 100644 index 0000000..fa3e82a Binary files /dev/null and b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01.imageset/Frame@3x.png differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01.imageset/home@2x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01.imageset/home@2x.png deleted file mode 100644 index c2c1c52..0000000 Binary files a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01.imageset/home@2x.png and /dev/null differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01.imageset/home@3x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01.imageset/home@3x.png deleted file mode 100644 index ffe5557..0000000 Binary files a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01.imageset/home@3x.png and /dev/null differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01_selected.imageset/Contents.json b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01_selected.imageset/Contents.json index b60f645..6aa90f6 100644 --- a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01_selected.imageset/Contents.json +++ b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01_selected.imageset/Contents.json @@ -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" } diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01_selected.imageset/Frame@2x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01_selected.imageset/Frame@2x.png new file mode 100644 index 0000000..2216b26 Binary files /dev/null and b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01_selected.imageset/Frame@2x.png differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01_selected.imageset/Frame@3x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01_selected.imageset/Frame@3x.png new file mode 100644 index 0000000..62b72c5 Binary files /dev/null and b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01_selected.imageset/Frame@3x.png differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01_selected.imageset/home@2x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01_selected.imageset/home@2x.png deleted file mode 100644 index c41c38c..0000000 Binary files a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01_selected.imageset/home@2x.png and /dev/null differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01_selected.imageset/home@3x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01_selected.imageset/home@3x.png deleted file mode 100644 index e346704..0000000 Binary files a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_01_selected.imageset/home@3x.png and /dev/null differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02.imageset/Contents.json b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02.imageset/Contents.json index 08d1e99..6aa90f6 100644 --- a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02.imageset/Contents.json +++ b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02.imageset/Contents.json @@ -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" } diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02.imageset/Explore@2x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02.imageset/Explore@2x.png deleted file mode 100644 index 86d500b..0000000 Binary files a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02.imageset/Explore@2x.png and /dev/null differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02.imageset/Explore@3x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02.imageset/Explore@3x.png deleted file mode 100644 index 9feb3f6..0000000 Binary files a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02.imageset/Explore@3x.png and /dev/null differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02.imageset/Frame@2x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02.imageset/Frame@2x.png new file mode 100644 index 0000000..6024d38 Binary files /dev/null and b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02.imageset/Frame@2x.png differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02.imageset/Frame@3x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02.imageset/Frame@3x.png new file mode 100644 index 0000000..fa87279 Binary files /dev/null and b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02.imageset/Frame@3x.png differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02_selected.imageset/Contents.json b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02_selected.imageset/Contents.json index 08d1e99..6aa90f6 100644 --- a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02_selected.imageset/Contents.json +++ b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02_selected.imageset/Contents.json @@ -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" } diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02_selected.imageset/Explore@2x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02_selected.imageset/Explore@2x.png deleted file mode 100644 index 8acae82..0000000 Binary files a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02_selected.imageset/Explore@2x.png and /dev/null differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02_selected.imageset/Explore@3x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02_selected.imageset/Explore@3x.png deleted file mode 100644 index 04d80d0..0000000 Binary files a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02_selected.imageset/Explore@3x.png and /dev/null differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02_selected.imageset/Frame@2x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02_selected.imageset/Frame@2x.png new file mode 100644 index 0000000..4adc73d Binary files /dev/null and b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02_selected.imageset/Frame@2x.png differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02_selected.imageset/Frame@3x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02_selected.imageset/Frame@3x.png new file mode 100644 index 0000000..3a0b978 Binary files /dev/null and b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_02_selected.imageset/Frame@3x.png differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04.imageset/Contents.json b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04.imageset/Contents.json index 14066fc..6aa90f6 100644 --- a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04.imageset/Contents.json +++ b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04.imageset/Contents.json @@ -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" } diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04.imageset/Frame@2x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04.imageset/Frame@2x.png new file mode 100644 index 0000000..5a35b02 Binary files /dev/null and b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04.imageset/Frame@2x.png differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04.imageset/Frame@3x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04.imageset/Frame@3x.png new file mode 100644 index 0000000..b3785af Binary files /dev/null and b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04.imageset/Frame@3x.png differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04.imageset/My list@2x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04.imageset/My list@2x.png deleted file mode 100644 index 1621f00..0000000 Binary files a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04.imageset/My list@2x.png and /dev/null differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04.imageset/My list@3x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04.imageset/My list@3x.png deleted file mode 100644 index 883a1b8..0000000 Binary files a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04.imageset/My list@3x.png and /dev/null differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04_selected.imageset/Contents.json b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04_selected.imageset/Contents.json index 14066fc..6aa90f6 100644 --- a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04_selected.imageset/Contents.json +++ b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04_selected.imageset/Contents.json @@ -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" } diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04_selected.imageset/Frame@2x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04_selected.imageset/Frame@2x.png new file mode 100644 index 0000000..58cacb6 Binary files /dev/null and b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04_selected.imageset/Frame@2x.png differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04_selected.imageset/Frame@3x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04_selected.imageset/Frame@3x.png new file mode 100644 index 0000000..e847c05 Binary files /dev/null and b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04_selected.imageset/Frame@3x.png differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04_selected.imageset/My list@2x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04_selected.imageset/My list@2x.png deleted file mode 100644 index 0450fbd..0000000 Binary files a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04_selected.imageset/My list@2x.png and /dev/null differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04_selected.imageset/My list@3x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04_selected.imageset/My list@3x.png deleted file mode 100644 index 8c8ca56..0000000 Binary files a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_04_selected.imageset/My list@3x.png and /dev/null differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05.imageset/Contents.json b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05.imageset/Contents.json index ceb7e15..6aa90f6 100644 --- a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05.imageset/Contents.json +++ b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05.imageset/Contents.json @@ -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" } diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05.imageset/Frame@2x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05.imageset/Frame@2x.png new file mode 100644 index 0000000..cfc6ef0 Binary files /dev/null and b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05.imageset/Frame@2x.png differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05.imageset/Frame@3x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05.imageset/Frame@3x.png new file mode 100644 index 0000000..68cf9d3 Binary files /dev/null and b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05.imageset/Frame@3x.png differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05.imageset/Me@2x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05.imageset/Me@2x.png deleted file mode 100644 index f0c1586..0000000 Binary files a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05.imageset/Me@2x.png and /dev/null differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05.imageset/Me@3x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05.imageset/Me@3x.png deleted file mode 100644 index 77446bd..0000000 Binary files a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05.imageset/Me@3x.png and /dev/null differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05_selected.imageset/Contents.json b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05_selected.imageset/Contents.json index ceb7e15..6aa90f6 100644 --- a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05_selected.imageset/Contents.json +++ b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05_selected.imageset/Contents.json @@ -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" } diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05_selected.imageset/Frame@2x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05_selected.imageset/Frame@2x.png new file mode 100644 index 0000000..eb2f99b Binary files /dev/null and b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05_selected.imageset/Frame@2x.png differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05_selected.imageset/Frame@3x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05_selected.imageset/Frame@3x.png new file mode 100644 index 0000000..8b3799b Binary files /dev/null and b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05_selected.imageset/Frame@3x.png differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05_selected.imageset/Me@2x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05_selected.imageset/Me@2x.png deleted file mode 100644 index c6518d0..0000000 Binary files a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05_selected.imageset/Me@2x.png and /dev/null differ diff --git a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05_selected.imageset/Me@3x.png b/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05_selected.imageset/Me@3x.png deleted file mode 100644 index 8998f5e..0000000 Binary files a/Thimra/Source/Assets.xcassets/TabBar/tabbar_icon_05_selected.imageset/Me@3x.png and /dev/null differ diff --git a/Thimra/Source/Assets.xcassets/icon/collect_icon_01.imageset/Contents.json b/Thimra/Source/Assets.xcassets/icon/collect_icon_01.imageset/Contents.json index 2fcc7b2..5c4d3b1 100644 --- a/Thimra/Source/Assets.xcassets/icon/collect_icon_01.imageset/Contents.json +++ b/Thimra/Source/Assets.xcassets/icon/collect_icon_01.imageset/Contents.json @@ -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" } diff --git a/Thimra/Source/Assets.xcassets/icon/collect_icon_01.imageset/Frame@2x.png b/Thimra/Source/Assets.xcassets/icon/collect_icon_01.imageset/Frame@2x.png new file mode 100644 index 0000000..71c1a96 Binary files /dev/null and b/Thimra/Source/Assets.xcassets/icon/collect_icon_01.imageset/Frame@2x.png differ diff --git a/Thimra/Source/Assets.xcassets/icon/collect_icon_01.imageset/Frame@3x.png b/Thimra/Source/Assets.xcassets/icon/collect_icon_01.imageset/Frame@3x.png new file mode 100644 index 0000000..e8a2772 Binary files /dev/null and b/Thimra/Source/Assets.xcassets/icon/collect_icon_01.imageset/Frame@3x.png differ diff --git a/Thimra/Source/Assets.xcassets/icon/collect_icon_01.imageset/收藏@2x.png b/Thimra/Source/Assets.xcassets/icon/collect_icon_01.imageset/收藏@2x.png deleted file mode 100644 index b1c64bf..0000000 Binary files a/Thimra/Source/Assets.xcassets/icon/collect_icon_01.imageset/收藏@2x.png and /dev/null differ diff --git a/Thimra/Source/Assets.xcassets/icon/collect_icon_01.imageset/收藏@3x.png b/Thimra/Source/Assets.xcassets/icon/collect_icon_01.imageset/收藏@3x.png deleted file mode 100644 index 0b8fb79..0000000 Binary files a/Thimra/Source/Assets.xcassets/icon/collect_icon_01.imageset/收藏@3x.png and /dev/null differ diff --git a/Thimra/Source/Assets.xcassets/icon/collect_icon_01_selected.imageset/Contents.json b/Thimra/Source/Assets.xcassets/icon/collect_icon_01_selected.imageset/Contents.json index 2fcc7b2..5c4d3b1 100644 --- a/Thimra/Source/Assets.xcassets/icon/collect_icon_01_selected.imageset/Contents.json +++ b/Thimra/Source/Assets.xcassets/icon/collect_icon_01_selected.imageset/Contents.json @@ -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" } diff --git a/Thimra/Source/Assets.xcassets/icon/collect_icon_01_selected.imageset/Frame@2x.png b/Thimra/Source/Assets.xcassets/icon/collect_icon_01_selected.imageset/Frame@2x.png new file mode 100644 index 0000000..1b1b2f3 Binary files /dev/null and b/Thimra/Source/Assets.xcassets/icon/collect_icon_01_selected.imageset/Frame@2x.png differ diff --git a/Thimra/Source/Assets.xcassets/icon/collect_icon_01_selected.imageset/Frame@3x.png b/Thimra/Source/Assets.xcassets/icon/collect_icon_01_selected.imageset/Frame@3x.png new file mode 100644 index 0000000..c789355 Binary files /dev/null and b/Thimra/Source/Assets.xcassets/icon/collect_icon_01_selected.imageset/Frame@3x.png differ diff --git a/Thimra/Source/Assets.xcassets/icon/collect_icon_01_selected.imageset/收藏@2x.png b/Thimra/Source/Assets.xcassets/icon/collect_icon_01_selected.imageset/收藏@2x.png deleted file mode 100644 index f1e659f..0000000 Binary files a/Thimra/Source/Assets.xcassets/icon/collect_icon_01_selected.imageset/收藏@2x.png and /dev/null differ diff --git a/Thimra/Source/Assets.xcassets/icon/collect_icon_01_selected.imageset/收藏@3x.png b/Thimra/Source/Assets.xcassets/icon/collect_icon_01_selected.imageset/收藏@3x.png deleted file mode 100644 index 74c35e7..0000000 Binary files a/Thimra/Source/Assets.xcassets/icon/collect_icon_01_selected.imageset/收藏@3x.png and /dev/null differ diff --git a/Thimra/Source/Thimra-Bridging-Header.h b/Thimra/Source/Thimra-Bridging-Header.h index e40e6ad..30f41af 100644 --- a/Thimra/Source/Thimra-Bridging-Header.h +++ b/Thimra/Source/Thimra-Bridging-Header.h @@ -13,3 +13,5 @@ #import #import #import +#import +#import "WMPageController.h" diff --git a/Thimra/Thirdparty/WMPageController/UIViewController+WMPageController.h b/Thimra/Thirdparty/WMPageController/UIViewController+WMPageController.h new file mode 100644 index 0000000..2f789e0 --- /dev/null +++ b/Thimra/Thirdparty/WMPageController/UIViewController+WMPageController.h @@ -0,0 +1,20 @@ +// +// UIViewController+WMPageController.h +// WMPageController +// +// Created by Mark on 15/6/11. +// Copyright (c) 2015年 yq. All rights reserved. +// + +#import + +@class WMPageController; + +@interface UIViewController (WMPageController) + +/** + 获取控制器所在的WMPageController + */ +@property (nonatomic, nullable, strong, readonly) WMPageController *wm_pageController; + +@end diff --git a/Thimra/Thirdparty/WMPageController/UIViewController+WMPageController.m b/Thimra/Thirdparty/WMPageController/UIViewController+WMPageController.m new file mode 100644 index 0000000..13f4e91 --- /dev/null +++ b/Thimra/Thirdparty/WMPageController/UIViewController+WMPageController.m @@ -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 diff --git a/Thimra/Thirdparty/WMPageController/WMMenuView/WMMenuItem.h b/Thimra/Thirdparty/WMPageController/WMMenuView/WMMenuItem.h new file mode 100755 index 0000000..bf1da29 --- /dev/null +++ b/Thimra/Thirdparty/WMPageController/WMMenuView/WMMenuItem.h @@ -0,0 +1,37 @@ +// +// WMMenuItem.h +// WMPageController +// +// Created by Mark on 15/4/26. +// Copyright (c) 2015年 yq. All rights reserved. +// + +#import +@class WMMenuItem; + +typedef NS_ENUM(NSUInteger, WMMenuItemState) { + WMMenuItemStateSelected, + WMMenuItemStateNormal, +}; + +NS_ASSUME_NONNULL_BEGIN +@protocol WMMenuItemDelegate +@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 delegate; +@property (nonatomic, assign, readonly) BOOL selected; + +- (void)setSelected:(BOOL)selected withAnimation:(BOOL)animation; + +@end +NS_ASSUME_NONNULL_END diff --git a/Thimra/Thirdparty/WMPageController/WMMenuView/WMMenuItem.m b/Thimra/Thirdparty/WMPageController/WMMenuView/WMMenuItem.m new file mode 100755 index 0000000..9d747c3 --- /dev/null +++ b/Thimra/Thirdparty/WMPageController/WMMenuView/WMMenuItem.m @@ -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 diff --git a/Thimra/Thirdparty/WMPageController/WMMenuView/WMMenuView.h b/Thimra/Thirdparty/WMPageController/WMMenuView/WMMenuView.h new file mode 100755 index 0000000..f3ca76b --- /dev/null +++ b/Thimra/Thirdparty/WMPageController/WMMenuView/WMMenuView.h @@ -0,0 +1,110 @@ +// +// WMMenuView.h +// WMPageController +// +// Created by Mark on 15/4/26. +// Copyright (c) 2015年 yq. All rights reserved. +// + +#import +#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 +@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 + +@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 当前的 menuView,frame 也是不确定的 + * @param initialMenuItem 初始化完成的 menuItem + * @param index Item 所属的位置; + * + * @return 定制完成的 MenuItem + */ +- (WMMenuItem *)menuView:(WMMenuView *)menu initialMenuItem:(WMMenuItem *)initialMenuItem atIndex:(NSInteger)index; + +@end + +@interface WMMenuView : UIView +@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 delegate; +@property (nonatomic, weak) id 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 diff --git a/Thimra/Thirdparty/WMPageController/WMMenuView/WMMenuView.m b/Thimra/Thirdparty/WMPageController/WMMenuView/WMMenuView.m new file mode 100755 index 0000000..d2d9a1f --- /dev/null +++ b/Thimra/Thirdparty/WMPageController/WMMenuView/WMMenuView.m @@ -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]; + } +} + +// 计算所有item的frame值,主要是为了适配所有item的宽度之和小于屏幕宽的情况 +// 这里与后面的 `-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 diff --git a/Thimra/Thirdparty/WMPageController/WMMenuView/WMProgressView.h b/Thimra/Thirdparty/WMPageController/WMMenuView/WMProgressView.h new file mode 100755 index 0000000..f406db6 --- /dev/null +++ b/Thimra/Thirdparty/WMPageController/WMMenuView/WMProgressView.h @@ -0,0 +1,29 @@ +// +// WMProgressView.h +// WMPageController +// +// Created by Mark on 15/6/20. +// Copyright (c) 2015年 yq. All rights reserved. +// + +#import + +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 diff --git a/Thimra/Thirdparty/WMPageController/WMMenuView/WMProgressView.m b/Thimra/Thirdparty/WMPageController/WMMenuView/WMProgressView.m new file mode 100755 index 0000000..40923fa --- /dev/null +++ b/Thimra/Thirdparty/WMPageController/WMMenuView/WMProgressView.m @@ -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 diff --git a/Thimra/Thirdparty/WMPageController/WMMenuView/WMScrollView.h b/Thimra/Thirdparty/WMPageController/WMMenuView/WMScrollView.h new file mode 100644 index 0000000..b4b25b3 --- /dev/null +++ b/Thimra/Thirdparty/WMPageController/WMMenuView/WMScrollView.h @@ -0,0 +1,13 @@ +// +// WMScrollView.h +// WMPageController +// +// Created by lh on 15/11/21. +// Copyright (c) 2015年 yq. All rights reserved. +// + +#import + +@interface WMScrollView : UIScrollView + +@end diff --git a/Thimra/Thirdparty/WMPageController/WMMenuView/WMScrollView.m b/Thimra/Thirdparty/WMPageController/WMMenuView/WMScrollView.m new file mode 100644 index 0000000..8de0ac6 --- /dev/null +++ b/Thimra/Thirdparty/WMPageController/WMMenuView/WMScrollView.m @@ -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 - + +- (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 diff --git a/Thimra/Thirdparty/WMPageController/WMPageController.h b/Thimra/Thirdparty/WMPageController/WMPageController.h new file mode 100755 index 0000000..0836c95 --- /dev/null +++ b/Thimra/Thirdparty/WMPageController/WMPageController.h @@ -0,0 +1,360 @@ +// +// WMPageController.h +// WMPageController +// +// Created by Mark on 15/6/11. +// Copyright (c) 2015年 yq. All rights reserved. +// + +#import +#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 +@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 +@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 + +@property (nonatomic, weak) id delegate; +@property (nonatomic, weak) id 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 *values; +@property (nonatomic, nullable, strong) NSMutableArray *keys; + +/** + * 各个控制器的 class, 例如:[UITableViewController class] + * Each controller's class, example:[UITableViewController class] + */ +@property (nonatomic, nullable, copy) NSArray *viewControllerClasses; + +/** + * 各个控制器标题 + * Titles of view controllers in page controller. + */ +@property (nonatomic, nullable, copy) NSArray *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 *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 *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 method,recommend 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 *)classes andTheirTitles:(NSArray *)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 diff --git a/Thimra/Thirdparty/WMPageController/WMPageController.m b/Thimra/Thirdparty/WMPageController/WMPageController.m new file mode 100755 index 0000000..296cda0 --- /dev/null +++ b/Thimra/Thirdparty/WMPageController/WMPageController.m @@ -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; +// 用于记录子控制器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)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 *)classes andTheirTitles:(NSArray *)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]]) { + // 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; + 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