很多项目中用都会有相册功能,实现相册中浏览的图片的功能,你可能会想到使用 scrollview ,但我更建议你通过复用机制来实现 . 这里基于 UICollection View来实现,当然你也可以通过 UITableView 来实现.具体看代码:
class BigImageView: UIView,UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate {
var downloadSucBlock:(()->())?
var downloadFailBlock:(()->())?
var dataArray:[String] = []{ //数据源数组
didSet{
collectionView.reloadData()
}
}
//
var collectionView:UICollectionView!
var indexLabel:UILabel!
var downBtn:UIButton!
var curIndex = 0
var curImage :UIImage?
// MARK: - system method
override func awakeFromNib() {
super.awakeFromNib()
self.setUp()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.setUp()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - private method
private func setUp() {
//创建 collectionView
collectionView = self.createHorizontalCollectionView(CGSizeMake(SCREEN_WIDTH, self.frame.height), sectionInset: UIEdgeInsetsZero)
collectionView.backgroundColor = UIColor.blackColor()
collectionView.delegate = self
collectionView.dataSource = self
collectionView.pagingEnabled = true
self.addSubview(collectionView)
//注册 cell
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "CollectionCell")
//创建索引 label
let pointX = (SCREEN_WIDTH-100)/2.0
indexLabel = UILabel.init(frame: CGRectMake(pointX, SCREEN_HEIGHT-40, 100, 20))
indexLabel.textColor = UIColor.whiteColor()
indexLabel.font = UIFont.systemFontOfSize(15)
indexLabel.textAlignment = .Center
self.addSubview(indexLabel)
//创建下载按钮
downBtn = UIButton.init(type: .Custom)
downBtn.frame = CGRectMake(SCREEN_WIDTH - 45, SCREEN_HEIGHT - 45, 25, 25)
downBtn.setImage(UIImage.init(named: "down_image"), forState: .Normal)
downBtn.addTarget(self, action: #selector(downBtnAction), forControlEvents: .TouchUpInside)
self.addSubview(downBtn)
}
func downBtnAction() {
//下载图片到本地
if let curImage = curImage {
UIImageWriteToSavedPhotosAlbum(curImage, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)
}
}
func image(image: UIImage, didFinishSavingWithError: NSError?, contextInfo: AnyObject){
if let didFinishSavingWithError = didFinishSavingWithError {
print(didFinishSavingWithError)
if let downloadFailBlock = downloadFailBlock {
downloadFailBlock()
}
}else{
if let downloadSucBlock = downloadSucBlock {
downloadSucBlock()
}
}
}
func removeSelf() {
self.removeFromSuperview()
}
func getNewFrame(frame:CGRect) -> CGRect {
if frame.width/SCREEN_WIDTH != frame.height/SCREEN_HEIGHT{
let newH = SCREEN_WIDTH*frame.height/frame.width
let newPointY = (SCREEN_HEIGHT-newH)/2.0
if frame.width/SCREEN_WIDTH > frame.height/SCREEN_HEIGHT {
return CGRectMake(0, newPointY, SCREEN_WIDTH, newH)
}else{
return CGRectMake(0, 0, SCREEN_WIDTH, newH)
}
}else{
return CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
}
}
// MARK: - public method
/**
创建横向滚动视图(建议写在 UIView 的类别中)
- parameter itemSize: cell 的大小
- parameter sectionInset: cell 上下左右的间距
- returns: UICollectionView
*/
func createHorizontalCollectionView(itemSize:CGSize,sectionInset:UIEdgeInsets)-> UICollectionView {
//创建布局对象
let layout = UICollectionViewFlowLayout()
//设置 cell 的大小
layout.itemSize = itemSize
//设置 cell 上下左右的间距
layout.sectionInset = sectionInset
//设置横向竖线的最小值
layout.minimumLineSpacing = sectionInset.left
//设置滚动方向
layout.scrollDirection = .Horizontal
//创建网格视图
let collectionView = UICollectionView.init(frame: CGRectMake(0, 0, SCREEN_WIDTH, self.frame.height), collectionViewLayout: layout)
collectionView.scrollsToTop = false
collectionView.backgroundColor = UIColor.whiteColor()
return collectionView
}
/**
展示图片浏览图
- parameter imageArr: 图片 URL 数组
- parameter selCount: 点击的图片索引
*/
func showBigImageView(imageArr:[String],selCount:Int) {
if imageArr.count == 0 {
return
}
dataArray = imageArr
//滚动到指定位置
collectionView.scrollToItemAtIndexPath(NSIndexPath.init(forItem: selCount, inSection: 0), atScrollPosition: .Right, animated: false)
}
// MARK: - UICollectionViewDelegate
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataArray.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionCell", forIndexPath: indexPath)
cell.backgroundColor = UIColor.blackColor()
for subview in cell.subviews {
subview.removeFromSuperview()
}
let image = UIImage.init(named: "ICON108")
curImage = image
let imageView = UIImageView.init(image:image)
imageView.userInteractionEnabled = true
let newFrame = self.getNewFrame(imageView.frame)
imageView.frame = newFrame
if newFrame.height > SCREEN_HEIGHT{
let scrollView = UIScrollView.init(frame: CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT))
scrollView.addSubview(imageView)
scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, newFrame.height)
cell.addSubview(scrollView)
let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(removeFromSuperview))
imageView.addGestureRecognizer(tapGesture)
}else{
cell.addSubview(imageView)
}
indexLabel.text = String(format: "%ld / %ld",indexPath.row+1,dataArray.count)
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.removeSelf()
}
}
期待你的评论建议O(∩_∩)O~