1.*********简易版的图片浏览器********
/***
图片预览
使用:
let imagepreviewvc = LYBIMagePreviewVC()
imagepreviewvc.imageArr=[UIImage.init(named: "lunboone"),UIImage.init(named: "lunbotwo"),UIImage.init(named: "lunbothree")] as! [UIImage]
imagepreviewvc.modalPresentationStyle=UIModalPresentationStyle(rawValue: 0)!
self.present(imagepreviewvc, animated: true, completion: nil)
*/
import UIKit
class LYBIMagePreviewVC: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource{
//图片数组
var imageArr:[UIImage]=[UIImage.init(named: "lunboone")!]{
willSet(image){
}
didSet{
collec?.reloadData()
}
}
var pagectr:UIPageControl?
lazy var sureAndCancelView:UIView={
let v:UIView=UIView.init(frame: CGRect.init(x: 0, y: 0, width: Int(WIDTH), height: TopSpaceHigh))
v.backgroundColor=UIColor.clear
// let cancelBtn:UIButton=UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 100, height: 50))
// cancelBtn.backgroundColor=UIColor.clear
// cancelBtn.setTitle("取消", for: UIControl.State.normal)
// cancelBtn.setTitleColor(UIColor.white, for: UIControl.State.normal)
// cancelBtn.addTarget(self, action: #selector(cancelClick), for: UIControl.Event.touchUpInside)
// v.addSubview(cancelBtn)
let sureBtn:UIButton=UIButton.init(frame: CGRect.init(x:Int(WIDTH)-TopSpaceHigh/2-10, y: TopSpaceHigh/2-5, width: TopSpaceHigh/2, height: TopSpaceHigh/2))
sureBtn.backgroundColor=UIColor.clear
sureBtn.layer.borderColor=UIColor.white.cgColor
sureBtn.layer.borderWidth=1
sureBtn.setTitle("X", for: UIControl.State.normal)
sureBtn.setTitleColor(UIColor.white, for: UIControl.State.normal)
sureBtn.addTarget(self, action: #selector(sureClick), for: UIControl.Event.touchUpInside)
v.addSubview(sureBtn)
return v
}()
//确定按钮
@objc func sureClick(sender:UIButton){
print("确定")
self.dismiss(animated: true, completion: nil)
}
//取消按钮
// @objc func cancelClick(sender:UIButton){
// print("取消")
// self.dismiss(animated: true, completion: nil)
// }
var collec:UICollectionView?
override func viewDidLoad() {
super.viewDidLoad()
createCollectionView()
self.view.addSubview(sureAndCancelView)
}
func createCollectionView(){
let flowLayout = UICollectionViewFlowLayout.init()
flowLayout.itemSize=CGSize.init(width: WIDTH, height:HEIGHT-CGFloat(bottomSafeHeight)-CGFloat(TopSpaceHigh))
flowLayout.minimumLineSpacing=0
flowLayout.minimumInteritemSpacing=0
flowLayout.scrollDirection = UICollectionView.ScrollDirection.horizontal
collec = UICollectionView.init(frame: CGRect.init(x: 0, y: TopSpaceHigh, width: Int(WIDTH), height: Int(HEIGHT)-Int(bottomSafeHeight)-Int(TopSpaceHigh)), collectionViewLayout: flowLayout)
collec?.showsHorizontalScrollIndicator=false
collec?.backgroundColor=UIColor.white
collec?.delegate=self
collec?.dataSource=self
collec?.register(LYBImagePreviewcollectioncell.classForCoder(), forCellWithReuseIdentifier: "LYBImagePreviewcollectioncell")
view.addSubview(collec!)
collec?.isPagingEnabled=true//分页
/**
pageCcontrol
*/
pagectr=UIPageControl.init(frame: CGRect.init(x: 50, y: Int(HEIGHT)-30-Int(bottomSafeHeight), width: Int(WIDTH-100), height: 30))
pagectr?.numberOfPages=imageArr.count; pagectr?.currentPageIndicatorTintColor=UIColor.red
pagectr?.pageIndicatorTintColor=UIColor.gray
pagectr?.currentPage=0
view.addSubview(pagectr!)
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArr.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell:LYBImagePreviewcollectioncell = collectionView.dequeueReusableCell(withReuseIdentifier: "LYBImagePreviewcollectioncell", for: indexPath) as! LYBImagePreviewcollectioncell
cell.image = imageArr[indexPath.item]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.dismiss(animated: true, completion: nil)
collectionView.deselectItem(at: indexPath, animated: true)//取消选中,和显示按钮两回事
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let scrollindex=Int(scrollView.contentOffset.x/WIDTH)//获取当前的偏移量
pagectr?.currentPage=scrollindex
}
}
***********
/**
图片浏览器cell
*/
import UIKit
class LYBImagePreviewcollectioncell: UICollectionViewCell {
var imageV:UIImageView!
var image:UIImage=UIImage.init(named: "appstart")!{
willSet(image) {
}
didSet {
imageV.image=image
}
}
override init(frame: CGRect) {
super.init(frame: frame)
createCell()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func createCell(){
imageV = UIImageView.init(frame:CGRect.init(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))
imageV.image=UIImage.init(named: "appstart")
addSubview(imageV)
}
}