|
@@ -0,0 +1,171 @@
|
|
|
+//
|
|
|
+// BrowsePicturesViewController.swift
|
|
|
+// RainbowPlanet
|
|
|
+//
|
|
|
+// Created by 南鑫林 on 2019/6/3.
|
|
|
+// Copyright © 2019 RainbowPlanet. All rights reserved.
|
|
|
+//
|
|
|
+
|
|
|
+import UIKit
|
|
|
+
|
|
|
+class BrowsePicturesViewController: BaseViewController {
|
|
|
+
|
|
|
+ //存储图片数组
|
|
|
+ var images:[String]
|
|
|
+
|
|
|
+ //默认显示的图片索引
|
|
|
+ var index:Int
|
|
|
+
|
|
|
+ //用来放置各个图片单元
|
|
|
+ var collectionView:UICollectionView!
|
|
|
+
|
|
|
+ //collectionView的布局
|
|
|
+ var collectionViewLayout: UICollectionViewFlowLayout!
|
|
|
+
|
|
|
+ //页控制器(小圆点)
|
|
|
+ var pageControl : UIPageControl!
|
|
|
+
|
|
|
+ //初始化
|
|
|
+ init(images:[String], index:Int = 0){
|
|
|
+ self.images = images
|
|
|
+ self.index = index
|
|
|
+
|
|
|
+ super.init(nibName: nil, bundle: nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ required init?(coder aDecoder: NSCoder) {
|
|
|
+ fatalError("init(coder:) has not been implemented")
|
|
|
+ }
|
|
|
+
|
|
|
+ private lazy var backButton: UIButton = {
|
|
|
+ let backButton = UIButton(type: UIButton.ButtonType.custom)
|
|
|
+ backButton.setImage(kImage(name: "nav_back_black_mask"), for: UIControl.State.normal)
|
|
|
+ backButton.rx.tap.subscribe(onNext: {
|
|
|
+ [weak self] (data) in
|
|
|
+ self?.navigationController?.popViewController(animated: true)
|
|
|
+ }).disposed(by: disposeBag)
|
|
|
+ return backButton
|
|
|
+ }()
|
|
|
+
|
|
|
+ //初始化
|
|
|
+ override func viewDidLoad() {
|
|
|
+ super.viewDidLoad()
|
|
|
+ navigationBar.isHidden = true
|
|
|
+
|
|
|
+ //背景设为黑色
|
|
|
+ self.view.backgroundColor = UIColor.black
|
|
|
+ //collectionView尺寸样式设置
|
|
|
+ collectionViewLayout = UICollectionViewFlowLayout()
|
|
|
+ collectionViewLayout.minimumLineSpacing = 0
|
|
|
+ collectionViewLayout.minimumInteritemSpacing = 0
|
|
|
+ //横向滚动
|
|
|
+ collectionViewLayout.scrollDirection = .horizontal
|
|
|
+
|
|
|
+ //collectionView初始化
|
|
|
+ collectionView = UICollectionView(frame: self.view.bounds,
|
|
|
+ collectionViewLayout: collectionViewLayout)
|
|
|
+ collectionView.backgroundColor = UIColor.black
|
|
|
+ collectionView.delegate = self
|
|
|
+ collectionView.dataSource = self
|
|
|
+ collectionView.isPagingEnabled = true
|
|
|
+ //不自动调整内边距,确保全屏
|
|
|
+ if #available(iOS 11.0, *) {
|
|
|
+ collectionView.contentInsetAdjustmentBehavior = .never
|
|
|
+ } else {
|
|
|
+ self.automaticallyAdjustsScrollViewInsets = false
|
|
|
+ }
|
|
|
+ self.view.addSubview(collectionView)
|
|
|
+
|
|
|
+ //将视图滚动到默认图片上
|
|
|
+ let indexPath = IndexPath(item: index, section: 0)
|
|
|
+ collectionView.scrollToItem(at: indexPath, at: .left, animated: false)
|
|
|
+
|
|
|
+ //设置页控制器
|
|
|
+ pageControl = UIPageControl()
|
|
|
+ pageControl.center = CGPoint(x: UIScreen.main.bounds.width/2,
|
|
|
+ y: UIScreen.main.bounds.height - 20 - kSafeTabBarHeight)
|
|
|
+ pageControl.numberOfPages = images.count
|
|
|
+ pageControl.isUserInteractionEnabled = false
|
|
|
+ pageControl.currentPage = index
|
|
|
+ view.addSubview(self.pageControl)
|
|
|
+ view.addSubview(backButton)
|
|
|
+ backButton.snp.makeConstraints { (make) in
|
|
|
+ make.top.equalToSuperview().offset(kSafeStatusBarHeight+11)
|
|
|
+ make.size.equalTo(22)
|
|
|
+ make.left.equalTo(14)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //隐藏状态栏
|
|
|
+ override var prefersStatusBarHidden: Bool {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
+ //将要对子视图布局时调用(横竖屏切换时)
|
|
|
+ override func viewWillLayoutSubviews() {
|
|
|
+ super.viewWillLayoutSubviews()
|
|
|
+
|
|
|
+ //重新设置collectionView的尺寸
|
|
|
+ collectionView.frame.size = self.view.bounds.size
|
|
|
+ collectionView.collectionViewLayout.invalidateLayout()
|
|
|
+
|
|
|
+ //将视图滚动到当前图片上
|
|
|
+ let indexPath = IndexPath(item: self.pageControl.currentPage, section: 0)
|
|
|
+ collectionView.scrollToItem(at: indexPath, at: .left, animated: false)
|
|
|
+
|
|
|
+ //重新设置页控制器的位置
|
|
|
+ pageControl.center = CGPoint(x: UIScreen.main.bounds.width/2,
|
|
|
+ y: UIScreen.main.bounds.height - 20)
|
|
|
+ }
|
|
|
+
|
|
|
+ override func didReceiveMemoryWarning() {
|
|
|
+ super.didReceiveMemoryWarning()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+//ImagePreviewVC的CollectionView相关协议方法实现
|
|
|
+extension BrowsePicturesViewController:UICollectionViewDelegate, UICollectionViewDataSource,
|
|
|
+UICollectionViewDelegateFlowLayout{
|
|
|
+
|
|
|
+ //collectionView单元格创建
|
|
|
+ func collectionView(_ collectionView: UICollectionView,
|
|
|
+ cellForItemAt indexPath: IndexPath)
|
|
|
+ -> UICollectionViewCell {
|
|
|
+ let cell = BrowsePicturesPagerViewCell.cellWith(collectionView: collectionView, indexPath: indexPath)
|
|
|
+ cell.iconImageView.kf.setImage(with: kURLImage(name: images[indexPath.row]), placeholder: kImage(name: ""))
|
|
|
+ return cell
|
|
|
+ }
|
|
|
+
|
|
|
+ //collectionView单元格数量
|
|
|
+ func collectionView(_ collectionView: UICollectionView,
|
|
|
+ numberOfItemsInSection section: Int) -> Int {
|
|
|
+ return self.images.count
|
|
|
+ }
|
|
|
+
|
|
|
+ //collectionView单元格尺寸
|
|
|
+ func collectionView(_ collectionView: UICollectionView,
|
|
|
+ layout collectionViewLayout: UICollectionViewLayout,
|
|
|
+ sizeForItemAt indexPath: IndexPath) -> CGSize {
|
|
|
+ return self.view.bounds.size
|
|
|
+ }
|
|
|
+
|
|
|
+ //collectionView里某个cell将要显示
|
|
|
+ func collectionView(_ collectionView: UICollectionView,
|
|
|
+ willDisplay cell: UICollectionViewCell,
|
|
|
+ forItemAt indexPath: IndexPath) {
|
|
|
+ if let cell = cell as? BrowsePicturesPagerViewCell{
|
|
|
+ //由于单元格是复用的,所以要重置内部元素尺寸
|
|
|
+ cell.resetSize()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //collectionView里某个cell显示完毕
|
|
|
+ func collectionView(_ collectionView: UICollectionView,
|
|
|
+ didEndDisplaying cell: UICollectionViewCell,
|
|
|
+ forItemAt indexPath: IndexPath) {
|
|
|
+ //当前显示的单元格
|
|
|
+ let visibleCell = collectionView.visibleCells[0]
|
|
|
+ //设置页控制器当前页
|
|
|
+ self.pageControl.currentPage = collectionView.indexPath(for: visibleCell)!.item
|
|
|
+ }
|
|
|
+}
|