//
|
// 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) {
|
}
|
}
|