很多項(xiàng)目中用都會(huì)有相冊(cè)功能,實(shí)現(xiàn)相冊(cè)中瀏覽的圖片的功能,你可能會(huì)想到使用 scrollview ,但我更建議你通過(guò)復(fù)用機(jī)制來(lái)實(shí)現(xiàn) . 這里基于 UICollection View來(lái)實(shí)現(xiàn),當(dāng)然你也可以通過(guò) UITableView 來(lái)實(shí)現(xiàn).具體看代碼:
class BigImageView: UIView,UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate {
var downloadSucBlock:(()->())?
var downloadFailBlock:(()->())?
var dataArray:[String] = []{ //數(shù)據(jù)源數(shù)組
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() {
//創(chuàng)建 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)
//注冊(cè) cell
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "CollectionCell")
//創(chuàng)建索引 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)
//創(chuàng)建下載按鈕
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
/**
創(chuàng)建橫向滾動(dòng)視圖(建議寫(xiě)在 UIView 的類(lèi)別中)
- parameter itemSize: cell 的大小
- parameter sectionInset: cell 上下左右的間距
- returns: UICollectionView
*/
func createHorizontalCollectionView(itemSize:CGSize,sectionInset:UIEdgeInsets)-> UICollectionView {
//創(chuàng)建布局對(duì)象
let layout = UICollectionViewFlowLayout()
//設(shè)置 cell 的大小
layout.itemSize = itemSize
//設(shè)置 cell 上下左右的間距
layout.sectionInset = sectionInset
//設(shè)置橫向豎線的最小值
layout.minimumLineSpacing = sectionInset.left
//設(shè)置滾動(dòng)方向
layout.scrollDirection = .Horizontal
//創(chuàng)建網(wǎng)格視圖
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 數(shù)組
- parameter selCount: 點(diǎn)擊的圖片索引
*/
func showBigImageView(imageArr:[String],selCount:Int) {
if imageArr.count == 0 {
return
}
dataArray = imageArr
//滾動(dòng)到指定位置
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()
}
}
期待你的評(píng)論建議O(∩_∩)O~