swift-相冊瀏覽.gif
今天給大家分享一個(gè)簡單的swift版的相冊瀏覽的小demo,如有不足澡绩,請大家指教(o)肺稀。
首先,在ViewController中的代碼:
import UIKit
class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
var collectionView : UICollectionView?
var dataArray : [String]?
var imageView : UIImageView?
override func viewDidLoad() {
super.viewDidLoad()
self.dataArray = [String]()
for i in 1..<12 {
let imageStr = String.init(format: "aion%02d.jpg", i)
dataArray?.append(imageStr)
}
print(dataArray!)
//初始化布局酌毡,創(chuàng)建layout,必須設(shè)置
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = UICollectionViewScrollDirection.Vertical
//設(shè)置垂直方向得最小距離
layout.minimumInteritemSpacing = 10
//設(shè)置水平方向得最小距離
layout.minimumLineSpacing = 10
//創(chuàng)建collectionView蕾管,并添加layout枷踏,必須添加
self.collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
self.view.addSubview(collectionView!)
//設(shè)置背景顏色
self.collectionView?.backgroundColor = UIColor.whiteColor()
//簽署代理和數(shù)據(jù)源
self.collectionView?.delegate = self
self.collectionView?.dataSource = self
//注冊單元格
self.collectionView?.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "newCell")
}
//設(shè)置單元格的個(gè)數(shù),在collectionView稱其為item
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataArray!.count;
}
//創(chuàng)建cell
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("newCell", forIndexPath: indexPath)
self.imageView = UIImageView(frame: cell.bounds)
cell.contentView.addSubview(imageView!)
self.imageView!.image = UIImage(named: dataArray![indexPath.item])
if self.imageView!.image == nil {
self.imageView!.image = UIImage(named: "aion01.jpg")
}
return cell;
}
//代理方法事項(xiàng)單元格得大小,單元格顯示的大小
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(self.view.bounds.size.width/4-10, self.view.bounds.size.height/4-20);
}
//點(diǎn)擊item
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let photoVC = ActiviPhotoViewController()
photoVC.imageArray = self.dataArray
photoVC.currentIndex = indexPath.item
self.navigationController?.pushViewController(photoVC, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
自定義ActiviPhotoViewController來實(shí)現(xiàn)點(diǎn)擊圖片的顯示
import UIKit
class ActiviPhotoViewController: UIViewController {
var imageArray : [String]?
var currentIndex : NSInteger?
override func viewDidLoad() {
super.viewDidLoad()
self.title = "個(gè)人圖片"
let jumCollection = JumCollection(frame: self.view.bounds)
jumCollection.backgroundColor = UIColor.whiteColor()
//傳圖片內(nèi)容
jumCollection.array = self.imageArray
jumCollection.getIndex(self.currentIndex!)
self.view.addSubview(jumCollection)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.hidden = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
自定義JumCollectionViewCell來實(shí)現(xiàn)圖片的展示
import UIKit
class JumCollectionViewCell: UICollectionViewCell,UIScrollViewDelegate {
var index : NSInteger?
var scroll : UIScrollView?
var imageView : UIImageView?
override init(frame: CGRect) {
super.init(frame: frame)
self.scrollView()
self.hand()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func scrollView() -> Void {
scroll = UIScrollView(frame: self.contentView.bounds)
scroll?.backgroundColor = UIColor.whiteColor()
//創(chuàng)建圖片視圖
imageView = UIImageView(frame:self.contentView.frame)
imageView?.contentMode = .ScaleAspectFit
scroll?.addSubview(imageView!)
imageView?.userInteractionEnabled = true
//設(shè)置代理
scroll?.delegate = self
//設(shè)置方法倍數(shù)
scroll?.minimumZoomScale = 1.0
scroll?.maximumZoomScale = 2.0
self.addSubview(scroll!)
}
//代理方法 返回一個(gè)放大縮小的視圖
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView;
}
//添加手勢
func hand() {
let tapGes = UITapGestureRecognizer(target: self, action: #selector(JumCollectionViewCell.tapAction(_:)))
tapGes.numberOfTapsRequired = 1 //設(shè)置點(diǎn)擊次數(shù)
tapGes.numberOfTouchesRequired = 1 //設(shè)置觸摸點(diǎn)
imageView?.addGestureRecognizer(tapGes)
let tapGes1 = UITapGestureRecognizer(target: self, action: #selector(JumCollectionViewCell.tapAction(_:)))
tapGes1.numberOfTapsRequired = 2 //設(shè)置點(diǎn)擊次數(shù)
tapGes1.numberOfTouchesRequired = 1 //設(shè)置觸摸點(diǎn)
imageView?.addGestureRecognizer(tapGes1)
}
func tapAction(tap:UITapGestureRecognizer) {
switch tap.numberOfTapsRequired {
case 1:
//點(diǎn)擊一次隱藏navBar
self.hiddenBar()
break;
case 2:
if scroll?.zoomScale == 1.0 {
scroll?.setZoomScale(2.0, animated: true)
}else if scroll?.zoomScale == 2.0 {
scroll?.setZoomScale(1.0, animated: true)
}
break;
default:
break;
}
}
func hiddenBar() {
//獲取點(diǎn)擊視圖
let bar = self.viewController()!.navigationController!.navigationBar
let isHidden = !bar.hidden
self.viewController()!.navigationController?.setNavigationBarHidden(isHidden, animated: true)
}
//scroll恢復(fù)原樣
func bringView() {
scroll?.setZoomScale(1.0, animated: true)
}
}
自定義JumCollection
import UIKit
class JumCollection: UICollectionView,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
var currentIndex : NSInteger?
var array : [String]?
override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
array = [String]()
//初始化布局類掰曾,uicollectiongViewLayout得子類
let flowLayout = UICollectionViewFlowLayout()
//設(shè)置滾動(dòng)方向
flowLayout.scrollDirection = .Horizontal
//設(shè)置垂直方向得最小距離
flowLayout.minimumInteritemSpacing = 0
//設(shè)置水平方向得最小距離
flowLayout.minimumLineSpacing = 0
//設(shè)置四周得邊緣距離
//初始化collectionView
super.init(frame: frame, collectionViewLayout: flowLayout)
self.delegate = self
self.dataSource = self
self.pagingEnabled = true
//注冊單元格
self.registerClass(JumCollectionViewCell.self, forCellWithReuseIdentifier: "new")
self.showsVerticalScrollIndicator = false
self.showsHorizontalScrollIndicator = false
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func getIndex(index: NSInteger) {
self.currentIndex = index
self.contentOffset = CGPointMake(self.frame.size.width*CGFloat(self.currentIndex!), self.contentOffset.y)
print(self.currentIndex!)
}
//創(chuàng)建item得個(gè)數(shù)
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return array!.count;
}
//創(chuàng)建item得內(nèi)容
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("new", forIndexPath: indexPath) as! JumCollectionViewCell
cell.imageView?.image = UIImage(named: array![indexPath.item])
return cell
}
//代理方法事項(xiàng)單元格得大小
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(self.bounds.size.width,self.bounds.size.height);
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0, 0, 0, 0);
}
func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
JumCollectionViewCell().bringView()
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
}
對UIView進(jìn)行擴(kuò)展
import UIKit
extension UIView {
func viewController() -> UIViewController? {
//通過響應(yīng)者鏈旭蠕,取得此視圖所在的視圖控制器
var next = self.nextResponder()
while(next != nil) {
//判斷響應(yīng)者對象是否是視圖控制器類型
if next!.isKindOfClass(UIViewController.self) {
return next as? UIViewController;
}
next = next!.nextResponder()
}
return nil;
}
}
大家如果想下載代碼,請加群:512847147,讓我們一起快樂的學(xué)習(xí)swift吧(o)掏熬。