iOS/swift之图片浏览器

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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值