宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-04-11 4356615a9252a987a62469331b1fcf91c102e24c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
//  AdvertisingView.swift
//  OKProject
//
//  Created by alvin_y on 2020/5/27.
//  Copyright © 2020 yangwang. All rights reserved.
//
 
import UIKit
import RxCocoa
import RxSwift
import NSObject_Rx
class AdvertisinCell: UICollectionViewCell {
    
    /// 封面图
    var image_cover = UIImageView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
        defineLayouts()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupViews()
        defineLayouts()
    }
    
    //MARK: - UI
    func setupViews() {
        image_cover.layer.cornerRadius = 8
        image_cover.layer.masksToBounds = true
        image_cover.contentMode = .scaleAspectFill
        self.contentView.addSubview(image_cover)
    }
    
    //MARK: - Layouts
    func defineLayouts() {
        image_cover.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
}
 
 
/// 广告页
class AdvertisingView: YYView {
    
    /// UICollectionView
    private var collectionView: UICollectionView!
    
    /// UIPageControl
    private var pageControl: UIPageControl!
    
    /// 宽度
    private let w = screenW * 272 / 375
    
    /// 高度
    private let h = screenH * 363 / 667
    
    /// 图片数组
    var items = BehaviorRelay<[AdvertisingModel]>(value: [])
    
    /// 关闭按钮
    private var button_close: UIButton = {
       let button = UIButton()
        button.setImage(UIImage.init(named: "icon_close_c"), for: .normal)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
        defineLayouts()
        bindRx()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupViews()
        defineLayouts()
        bindRx()
    }
    
 
    //MARK: - UI
    func setupViews() {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: w, height: h)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: layout)
        layout.scrollDirection = .horizontal
        collectionView.register(AdvertisinCell.self, forCellWithReuseIdentifier: "item")
        collectionView.isPagingEnabled = true
        collectionView.backgroundColor = .clear
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
        pageControl = UIPageControl.init(frame: CGRect.zero)
        pageControl.pageIndicatorTintColor = UIColor.color(hexString: "#FFFFFF", 0.3)
        pageControl.currentPageIndicatorTintColor = UIColor.color(hexString: "#FFFFFF")
        self.frame = UIScreen.main.bounds
        self.backgroundColor = UIColor.color(hexString: "#000000",0.5)
        self.addSubview(collectionView)
        self.addSubview(pageControl)
        self.addSubview(button_close)
    }
    
    //MARK: - Layouts
    func defineLayouts() {
        collectionView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalTo(w)
            make.height.equalTo(h)
        }
        pageControl.snp.makeConstraints { (make) in
            make.top.equalTo(collectionView.snp.bottom).offset(18)
            make.centerX.equalToSuperview()
            make.width.equalTo(screenW)
            make.height.equalTo(15)
        }
        button_close.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(pageControl.snp.bottom).offset(21)
            make.size.equalTo(CGSize(width: 30, height: 30))
        }
    }
    
    //MARK: - Rx
    func bindRx() {
        items.subscribe(onNext: {[unowned self] (_) in
            self.update()
        }).disposed(by: rx.disposeBag)
        
        button_close.rx.tap.subscribe(onNext: {[unowned self] (_) in
            self.dismiss()
        }).disposed(by: rx.disposeBag)
    }
    
    /// 显示
    func show()  {
        app.window?.addSubview(self)
        
    }
    
    /// 隐藏
    func dismiss()  {
        self.removeFromSuperview()
    }
    
    /// 更新
    func update()  {
        self.pageControl.isHidden = items.value.count == 1
        self.pageControl.numberOfPages = items.value.count
        self.pageControl.currentPage = 0
        self.collectionView.reloadData()
    }
}
 
//MARK: - UICollectionViewDelegate,UICollectionViewDataSource
extension AdvertisingView: UICollectionViewDelegate,UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let model = items.value[indexPath.row]
        if model.isJump == 1{
            switch model.jumpType {
            case 1:// 外部
                openURL(url: model.jumpUrl)
                break
            case 2:// 内部
                dismiss()
                let vc = YYWebView()
                vc.name = model.name
                vc.url = model.content
                UIViewController.base()?.navigationController?.pushViewController(vc)
                break
            default:
                break
            }
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.value.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "item", for: indexPath) as! AdvertisinCell
        cell.image_cover.load(url: items.value[indexPath.row].imgUrl)
        cell.layer.cornerRadius = 8
        cell.layer.masksToBounds = true
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        self.pageControl.currentPage = indexPath.row
    }
    
    func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    }
}