Commit 3d31d0a9c4abc190cedabf511b126b9ad6b46584
1 parent
32f6fdde
首页背景图跟随卡片滑动进行平滑切换
Showing
4 changed files
with
214 additions
and
7 deletions
metaCode/Classes/Main/HomePage/Controller/CNMetaCodeHomePageController.swift
| @@ -14,7 +14,8 @@ let Height: CGFloat = UIScreen.main.bounds.size.height | @@ -14,7 +14,8 @@ let Height: CGFloat = UIScreen.main.bounds.size.height | ||
| 14 | 14 | ||
| 15 | class CNMetaCodeHomePageController:CNLiveBaseViewController, NewPagedFlowViewDelegate, NewPagedFlowViewDataSource, CNCustomIndexViewDelegate{ | 15 | class CNMetaCodeHomePageController:CNLiveBaseViewController, NewPagedFlowViewDelegate, NewPagedFlowViewDataSource, CNCustomIndexViewDelegate{ |
| 16 | 16 | ||
| 17 | - | 17 | + ///NewPagedFlowView 开始滑动时的偏移量 |
| 18 | + private var beginOffset = 0.0 | ||
| 18 | 19 | ||
| 19 | private var tabBarY:CGFloat = 0.0 | 20 | private var tabBarY:CGFloat = 0.0 |
| 20 | 21 | ||
| @@ -57,7 +58,7 @@ class CNMetaCodeHomePageController:CNLiveBaseViewController, NewPagedFlowViewDel | @@ -57,7 +58,7 @@ class CNMetaCodeHomePageController:CNLiveBaseViewController, NewPagedFlowViewDel | ||
| 57 | self.tabBarY = (self.tabBarController?.tabBar.top)! | 58 | self.tabBarY = (self.tabBarController?.tabBar.top)! |
| 58 | 59 | ||
| 59 | 60 | ||
| 60 | - for i in (0..<6) { | 61 | + for i in (0..<5) { |
| 61 | self.dataArray.add("122") | 62 | self.dataArray.add("122") |
| 62 | } | 63 | } |
| 63 | 64 | ||
| @@ -102,14 +103,75 @@ class CNMetaCodeHomePageController:CNLiveBaseViewController, NewPagedFlowViewDel | @@ -102,14 +103,75 @@ class CNMetaCodeHomePageController:CNLiveBaseViewController, NewPagedFlowViewDel | ||
| 102 | bannerView?.superScrollView = flowView.scrollView | 103 | bannerView?.superScrollView = flowView.scrollView |
| 103 | bannerView?.associatedVCView = self.view | 104 | bannerView?.associatedVCView = self.view |
| 104 | bannerView?.delegate = self | 105 | bannerView?.delegate = self |
| 105 | - return bannerView | 106 | + return bannerView |
| 106 | } | 107 | } |
| 107 | 108 | ||
| 108 | func sizeForPage(in flowView: NewPagedFlowView!) -> CGSize { | 109 | func sizeForPage(in flowView: NewPagedFlowView!) -> CGSize { |
| 109 | return CGSize(width: Width-50, height: Height) | 110 | return CGSize(width: Width-50, height: Height) |
| 110 | } | 111 | } |
| 111 | 112 | ||
| 113 | + ///NewPagedFlowView 开始滑动 | ||
| 114 | + func willBeginScroll(_ scrollView: UIScrollView!) { | ||
| 115 | + beginOffset = scrollView.contentOffset.x | ||
| 116 | + } | ||
| 117 | + ///NewPagedFlowView 滑动中 | ||
| 118 | + func didScroll(_ scrollView: UIScrollView!) { | ||
| 119 | + | ||
| 120 | + let currentOffset:CGFloat = scrollView.contentOffset.x | ||
| 121 | + | ||
| 122 | + if(currentOffset > beginOffset){ | ||
| 123 | + | ||
| 124 | + | ||
| 125 | + if let rightView = self.backgroupView.getRightView(){ | ||
| 126 | + | ||
| 127 | + if let currentApearView = self.backgroupView.currentApearView{ | ||
| 128 | + self.backgroupView.insertSubview(rightView, belowSubview: currentApearView) | ||
| 129 | + } | ||
| 130 | + | ||
| 131 | + self.backgroupView.currentApearView?.maskLayer.right = KSreenWidth - (currentOffset-beginOffset)*2.5 | ||
| 132 | + }else{ | ||
| 133 | + | ||
| 134 | + } | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + if(currentOffset < beginOffset){ | ||
| 138 | + if let leftView = self.backgroupView.getLeftView(){ | ||
| 139 | + | ||
| 140 | + if let currentApearView = self.backgroupView.currentApearView{ | ||
| 141 | + self.backgroupView.insertSubview(leftView, belowSubview: currentApearView) | ||
| 142 | + } | ||
| 143 | + | ||
| 144 | + self.backgroupView.currentApearView?.maskLayer.left = (beginOffset-currentOffset)*2.5 | ||
| 145 | + }else{ | ||
| 146 | + | ||
| 147 | + } | ||
| 148 | + } | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + ///NewPagedFlowView 滑动将要结束 | ||
| 152 | + func willEndScroll(_ scrollView: UIScrollView!) { | ||
| 153 | + | ||
| 154 | + //暂时禁止再次滑动,后期优化通过自定义UIScrollView 结束动画来实现 | ||
| 155 | + scrollView.isScrollEnabled = false | ||
| 156 | + } | ||
| 157 | + ///NewPagedFlowView 滑动结束 | ||
| 158 | + func didEndScroll(_ scrollView: UIScrollView!) { | ||
| 159 | + print("scrollView.contentOffset.x = ",scrollView.contentOffset.x) | ||
| 160 | + let toIndex:Int = Int(scrollView.contentOffset.x/(Width-50)) | ||
| 161 | + | ||
| 162 | + if toIndex == self.backgroupView.currentApearViewIndex{ | ||
| 163 | + return | ||
| 164 | + } | ||
| 165 | + | ||
| 166 | + self.backgroupView.replaceCurerrentApearView(fromIndex: self.backgroupView.currentApearViewIndex, toIndex: toIndex) | ||
| 167 | + | ||
| 168 | + scrollView.isScrollEnabled = true | ||
| 169 | + | ||
| 170 | + } | ||
| 171 | + | ||
| 172 | + | ||
| 112 | //MARK: - CNCustomIndexView 代理方法 | 173 | //MARK: - CNCustomIndexView 代理方法 |
| 174 | + | ||
| 113 | func customIndexViewWillBeiginScroll(scrollView: UIScrollView) { | 175 | func customIndexViewWillBeiginScroll(scrollView: UIScrollView) { |
| 114 | self.view.height = KSreenHeight | 176 | self.view.height = KSreenHeight |
| 115 | } | 177 | } |
metaCode/Classes/Main/HomePage/View/CNHomePageBackgroupView.swift
| @@ -9,7 +9,12 @@ import Foundation | @@ -9,7 +9,12 @@ import Foundation | ||
| 9 | import UIKit | 9 | import UIKit |
| 10 | class CNHomePageBackgroupSubView : UIView | 10 | class CNHomePageBackgroupSubView : UIView |
| 11 | { | 11 | { |
| 12 | + ///前面的视图 | ||
| 13 | + var preSubView: CNHomePageBackgroupSubView? | ||
| 14 | + ///后面的视图 | ||
| 15 | + var nextSubView: CNHomePageBackgroupSubView? | ||
| 12 | 16 | ||
| 17 | + var name: String = "" | ||
| 13 | lazy var imageView: UIImageView = { | 18 | lazy var imageView: UIImageView = { |
| 14 | let imageView: UIImageView = UIImageView(image: UIImage(named: "homePage_backgroup_blue")) | 19 | let imageView: UIImageView = UIImageView(image: UIImage(named: "homePage_backgroup_blue")) |
| 15 | imageView.frame = self.bounds | 20 | imageView.frame = self.bounds |
| @@ -23,6 +28,16 @@ class CNHomePageBackgroupSubView : UIView | @@ -23,6 +28,16 @@ class CNHomePageBackgroupSubView : UIView | ||
| 23 | return titleLabel | 28 | return titleLabel |
| 24 | }() | 29 | }() |
| 25 | 30 | ||
| 31 | + ///自定义mask | ||
| 32 | + lazy var maskLayer: CALayer = { | ||
| 33 | + let maskLayer = CALayer.init() | ||
| 34 | + maskLayer.frame = self.bounds; | ||
| 35 | + maskLayer.backgroundColor = UIColor.gray.cgColor | ||
| 36 | + self.layer.mask = maskLayer | ||
| 37 | + | ||
| 38 | + return maskLayer | ||
| 39 | + }() | ||
| 40 | + | ||
| 26 | 41 | ||
| 27 | override init(frame: CGRect) { | 42 | override init(frame: CGRect) { |
| 28 | super.init(frame: frame) | 43 | super.init(frame: frame) |
| @@ -42,13 +57,27 @@ class CNHomePageBackgroupSubView : UIView | @@ -42,13 +57,27 @@ class CNHomePageBackgroupSubView : UIView | ||
| 42 | self.titleLabel.text = title | 57 | self.titleLabel.text = title |
| 43 | } | 58 | } |
| 44 | ///设置图片 | 59 | ///设置图片 |
| 45 | - func setImage(image:UIImage)->Void{ | ||
| 46 | - self.imageView.image = image | 60 | + func setImage(image:UIImage?)->Void{ |
| 61 | + if let tempImage = image{ | ||
| 62 | + self.imageView.image = tempImage | ||
| 63 | + }else{ | ||
| 64 | + | ||
| 65 | + } | ||
| 47 | } | 66 | } |
| 48 | } | 67 | } |
| 49 | 68 | ||
| 50 | class CNHomePageBackgroupView: UIView{ | 69 | class CNHomePageBackgroupView: UIView{ |
| 51 | 70 | ||
| 71 | + ///当前显示的view所处的位置 | ||
| 72 | + var currentApearViewIndex: Int = 0 | ||
| 73 | + ///当前在屏幕显示的view | ||
| 74 | + var currentApearView: CNHomePageBackgroupSubView? | ||
| 75 | + ///数据源 | ||
| 76 | + lazy var dataArray:Array<CNHomePageBackgroupSubView> = { | ||
| 77 | + let dataArray = Array<CNHomePageBackgroupSubView>() | ||
| 78 | + return dataArray | ||
| 79 | + }() | ||
| 80 | + | ||
| 52 | // MARK: - 初始化 | 81 | // MARK: - 初始化 |
| 53 | override init(frame: CGRect) { | 82 | override init(frame: CGRect) { |
| 54 | super.init(frame: frame) | 83 | super.init(frame: frame) |
| @@ -61,11 +90,116 @@ class CNHomePageBackgroupView: UIView{ | @@ -61,11 +90,116 @@ class CNHomePageBackgroupView: UIView{ | ||
| 61 | 90 | ||
| 62 | //创建UI | 91 | //创建UI |
| 63 | func initUI(){ | 92 | func initUI(){ |
| 93 | + | ||
| 94 | + for i in (0..<5){ | ||
| 95 | + | ||
| 96 | + let subView: CNHomePageBackgroupSubView = CNHomePageBackgroupSubView.init(frame: self.bounds) | ||
| 97 | + subView.frame = self.bounds | ||
| 98 | + | ||
| 99 | + | ||
| 100 | + if (i == 0) { | ||
| 101 | + subView.setTitle(title: "志愿者") | ||
| 102 | + subView.setImage(image: UIImage(named: "homePage_backgroup_red")) | ||
| 103 | + subView.name = "志愿者" | ||
| 104 | + } | ||
| 105 | + else if(i == 1){ | ||
| 106 | + subView.setTitle(title: "退伍军人") | ||
| 107 | + subView.setImage(image: UIImage(named: "homePage_backgroup_blue")) | ||
| 108 | + subView.name = "退伍军人" | ||
| 109 | + | ||
| 110 | + } | ||
| 111 | + else if(i == 2){ | ||
| 112 | + subView.setTitle(title: "慈善家") | ||
| 113 | + subView.setImage(image: UIImage(named: "homePage_backgroup_red")) | ||
| 114 | + subView.name = "慈善家" | ||
| 115 | + | ||
| 116 | + } | ||
| 117 | + else if(i == 3){ | ||
| 118 | + subView.setTitle(title: "退休干部") | ||
| 119 | + subView.setImage(image: UIImage(named: "homePage_backgroup_blue")) | ||
| 120 | + subView.name = "退休干部" | ||
| 121 | + | ||
| 122 | + } | ||
| 123 | + else if(i == 4){ | ||
| 124 | + subView.setTitle(title: "退休医生") | ||
| 125 | + subView.setImage(image: UIImage(named: "homePage_backgroup_red")) | ||
| 126 | + subView.name = "退伍军人" | ||
| 127 | + | ||
| 128 | + } | ||
| 129 | + | ||
| 130 | + //更新双向链表 | ||
| 131 | + let lastSubView: CNHomePageBackgroupSubView? = self.dataArray.last | ||
| 132 | + lastSubView?.nextSubView = subView | ||
| 133 | + subView.preSubView = lastSubView | ||
| 134 | + | ||
| 135 | + self.dataArray.append(subView) | ||
| 136 | + | ||
| 137 | + if i==0 { | ||
| 138 | + self.currentApearView = subView | ||
| 139 | + self.currentApearViewIndex = i | ||
| 140 | + } | ||
| 141 | + } | ||
| 142 | + | ||
| 143 | + //倒序添加子view | ||
| 144 | + for i in self.dataArray.enumerated().reversed() { | ||
| 145 | + | ||
| 146 | + let subview: CNHomePageBackgroupSubView = i.element | ||
| 147 | + self.addSubview(subview) | ||
| 148 | + | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + | ||
| 64 | let subView: CNHomePageBackgroupSubView = CNHomePageBackgroupSubView.init(frame: self.bounds) | 152 | let subView: CNHomePageBackgroupSubView = CNHomePageBackgroupSubView.init(frame: self.bounds) |
| 65 | subView.frame = self.bounds | 153 | subView.frame = self.bounds |
| 66 | - subView.setTitle(title: "志愿者") | ||
| 67 | 154 | ||
| 68 | - self.addSubview(subView) | 155 | + } |
| 156 | + | ||
| 157 | + //MARK: - 公开方法 | ||
| 158 | + ///获取当前视图左边的子视图 | ||
| 159 | + func getLeftView()->CNHomePageBackgroupSubView?{ | ||
| 160 | + if(currentApearViewIndex == 0){ | ||
| 161 | + return nil | ||
| 162 | + } | ||
| 163 | + return dataArray[currentApearViewIndex - 1] | ||
| 164 | + } | ||
| 165 | + ///获取当前视图右边的子视图 | ||
| 166 | + func getRightView()->CNHomePageBackgroupSubView?{ | ||
| 167 | + if(currentApearViewIndex == self.dataArray.count - 1){ | ||
| 168 | + return nil | ||
| 169 | + } | ||
| 170 | + return dataArray[currentApearViewIndex + 1] | ||
| 171 | + } | ||
| 172 | + | ||
| 173 | + /// 替换当前显示的子view | ||
| 174 | + /// - Parameters: | ||
| 175 | + /// - fromIndex: 原来显示的view索引 | ||
| 176 | + /// - toIndex: 将要显示的view索引 | ||
| 177 | + /// - Returns: 无返回值 | ||
| 178 | + func replaceCurerrentApearView(fromIndex:Int ,toIndex:Int)->Void{ | ||
| 179 | + | ||
| 180 | + //先保存之前显示的view | ||
| 181 | + let tempSubView: CNHomePageBackgroupSubView? = self.currentApearView | ||
| 182 | + | ||
| 183 | + //找到下一个要显示的view | ||
| 184 | + if(toIndex < fromIndex){ | ||
| 185 | + self.currentApearView = self.currentApearView?.preSubView | ||
| 186 | + } | ||
| 187 | + else{ | ||
| 188 | + self.currentApearView = self.currentApearView?.nextSubView | ||
| 189 | + } | ||
| 190 | + | ||
| 191 | + //把要显示的view放到最顶层 | ||
| 192 | + if let subView = self.currentApearView { | ||
| 193 | + self.bringSubviewToFront(subView) | ||
| 194 | + } | ||
| 195 | + //更新当前显示的view所处的位置 | ||
| 196 | + self.currentApearViewIndex = toIndex | ||
| 197 | + | ||
| 198 | + | ||
| 199 | + //重置之前的currentApearView的Left 和 right | ||
| 200 | + tempSubView?.maskLayer.left = 0; | ||
| 201 | + tempSubView?.maskLayer.right = KSreenWidth; | ||
| 202 | + | ||
| 69 | } | 203 | } |
| 70 | 204 | ||
| 71 | } | 205 | } |
metaCode/Classes/Vendors/NewPagedFlowView.h
| @@ -182,6 +182,11 @@ typedef NS_ENUM (NSUInteger, NewPagedFlowViewOrientation) { | @@ -182,6 +182,11 @@ typedef NS_ENUM (NSUInteger, NewPagedFlowViewOrientation) { | ||
| 182 | /// @param scrollView 滚动的scrollView | 182 | /// @param scrollView 滚动的scrollView |
| 183 | - (void)didScroll:(UIScrollView *)scrollView; | 183 | - (void)didScroll:(UIScrollView *)scrollView; |
| 184 | 184 | ||
| 185 | + | ||
| 186 | +/// 将要结束滚动 | ||
| 187 | +/// @param scrollView 滚动的scrollView | ||
| 188 | +- (void)willEndScroll:(UIScrollView *)scrollView; | ||
| 189 | + | ||
| 185 | /// 已经结束滚动 | 190 | /// 已经结束滚动 |
| 186 | /// @param scrollView 滚动的scrollView | 191 | /// @param scrollView 滚动的scrollView |
| 187 | - (void)didEndScroll:(UIScrollView *)scrollView; | 192 | - (void)didEndScroll:(UIScrollView *)scrollView; |
metaCode/Classes/Vendors/NewPagedFlowView.m
| @@ -664,11 +664,17 @@ static NSString *subviewClassName; | @@ -664,11 +664,17 @@ static NSString *subviewClassName; | ||
| 664 | #pragma mark --将要开始拖拽 | 664 | #pragma mark --将要开始拖拽 |
| 665 | - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { | 665 | - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { |
| 666 | [self stopTimer]; | 666 | [self stopTimer]; |
| 667 | + if(self.delegate && [self.delegate respondsToSelector:@selector(willBeginScroll:)]){ | ||
| 668 | + [self.delegate willBeginScroll:scrollView]; | ||
| 669 | + } | ||
| 667 | } | 670 | } |
| 668 | 671 | ||
| 669 | #pragma mark --结束拖拽 | 672 | #pragma mark --结束拖拽 |
| 670 | - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { | 673 | - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { |
| 671 | [self startTimer]; | 674 | [self startTimer]; |
| 675 | + if(self.delegate && [self.delegate respondsToSelector:@selector(willEndScroll:)]){ | ||
| 676 | + [self.delegate willEndScroll:scrollView]; | ||
| 677 | + } | ||
| 672 | } | 678 | } |
| 673 | //结束滑动 | 679 | //结束滑动 |
| 674 | - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView | 680 | - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView |