博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS/swift之图片浏览器
阅读量:4290 次
发布时间:2019-05-27

本文共 5511 字,大约阅读时间需要 18 分钟。

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

 

转载地址:http://zmmgi.baihongyu.com/

你可能感兴趣的文章
2017年3月Java9带来的革新!
查看>>
Log4j容器深入探究
查看>>
记glide框架使用中所遇到的问题
查看>>
学习AOP之透过Spring的Ioc理解Advisor
查看>>
Jquery一个简单的注册验证
查看>>
SpringMVC基础_ControllerAdvice
查看>>
Toast还能显示图片你知道么?
查看>>
安卓三状态切换按钮TriStateToggleButton
查看>>
Spring框架-AOP细节
查看>>
java.lang.Instrument 代理Agent使用
查看>>
Javascript:指针、帽子和女朋友
查看>>
Android中的ALERTDIALOG使用_优就业
查看>>
java使用javacsv读取csv文件 导入Mysql数据库
查看>>
Toolbar 不为人知的助手,ActionMenuView
查看>>
Java回调方法详解
查看>>
如何获取url中的参数并传递给iframe中的报表
查看>>
以前端架构师的思想看问题:解决单页应用,系统角色请求抢占session发送请求问题
查看>>
jsessionid问题解决方案
查看>>
MySQL和Oracle的delete,truncate
查看>>
idea使用心得(2)-安装设置与创建web项目
查看>>