iOS/swift之图片浏览器

1.*********简易版的图片浏览器********

/***
 图片预览
 使用:
 let looAlbum = LYBlookAlbumVC()
 looAlbum.imageArr=[UIImage.init(named: "comm_btn_checkmark"),UIImage.init(named: "fenxiangcai"),UIImage.init(named: "appstart")] as! [UIImage]
 self.present(looAlbum, animated: true, completion: nil)
 */
import UIKit

class LYBlookAlbumVC: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource{
    //图片数组
    var imageArr:[UIImage]=[UIImage.init(named: "comm_btn_checkmark")!]{
        didSet{
            collec?.reloadData()
        }
    }
     var pagectr:UIPageControl?
    lazy var sureAndCancelView:UIView={
        let v:UIView=UIView.init(frame: CGRect.init(x: 0, y: StateBarHigh, width: Int(WIDTH), height: 50))
        v.backgroundColor=UIColor.clear
        let cancelBtn:UIButton=UIButton.init(frame: CGRect.init(x: 20, y: 0, width: 100, height: 50))
        cancelBtn.backgroundColor=UIColor.clear
        cancelBtn.setTitle("取消", for: UIControl.State.normal)
        cancelBtn.addTarget(self, action: #selector(cancelClick), for: UIControl.Event.touchUpInside)
        cancelBtn.setTitleColor(UIColor.black, for: UIControl.State.normal)
        v.addSubview(cancelBtn)
        
        let sureBtn:UIButton=UIButton.init(frame: CGRect.init(x:WIDTH-120, y: 0, width: 100, height: 50))
        sureBtn.backgroundColor=UIColor.clear
        sureBtn.setTitle("确定", for: UIControl.State.normal)
        sureBtn.addTarget(self, action: #selector(sureClick), for: UIControl.Event.touchUpInside)
        sureBtn.setTitleColor(UIColor.black, for: UIControl.State.normal)
        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()
    }
    
    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(LYBImagePreviewBrowsercollectioncell.classForCoder(), forCellWithReuseIdentifier: "LYBImagePreviewBrowsercollectioncell")
        view.addSubview(collec!)
         view.addSubview(sureAndCancelView)
       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:LYBImagePreviewBrowsercollectioncell  = collectionView.dequeueReusableCell(withReuseIdentifier: "LYBImagePreviewBrowsercollectioncell", for: indexPath) as! LYBImagePreviewBrowsercollectioncell
        
        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 LYBImagePreviewBrowsercollectioncell: 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)
    }
}

猜你喜欢

转载自blog.csdn.net/u011146511/article/details/86512201