swift-相册浏览.gif
今天给大家分享一个简单的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!)
//初始化布局,创建layout,必须设置
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = UICollectionViewScrollDirection.Vertical
//设置垂直方向得最小距离
layout.minimumInteritemSpacing = 10
//设置水平方向得最小距离
layout.minimumLineSpacing = 10
//创建collectionView,并添加layout,必须添加
self.collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
self.view.addSubview(collectionView!)
//设置背景颜色
self.collectionView?.backgroundColor = UIColor.whiteColor()
//签署代理和数据源
self.collectionView?.delegate = self
self.collectionView?.dataSource = self
//注册单元格
self.collectionView?.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "newCell")
}
//设置单元格的个数,在collectionView称其为item
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataArray!.count;
}
//创建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;
}
//代理方法事项单元格得大小,单元格显示的大小
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);
}
//点击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来实现点击图片的显示
import UIKit
class ActiviPhotoViewController: UIViewController {
var imageArray : [String]?
var currentIndex : NSInteger?
override func viewDidLoad() {
super.viewDidLoad()
self.title = "个人图片"
let jumCollection = JumCollection(frame: self.view.bounds)
jumCollection.backgroundColor = UIColor.whiteColor()
//传图片内容
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来实现图片的展示
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()
//创建图片视图
imageView = UIImageView(frame:self.contentView.frame)
imageView?.contentMode = .ScaleAspectFit
scroll?.addSubview(imageView!)
imageView?.userInteractionEnabled = true
//设置代理
scroll?.delegate = self
//设置方法倍数
scroll?.minimumZoomScale = 1.0
scroll?.maximumZoomScale = 2.0
self.addSubview(scroll!)
}
//代理方法 返回一个放大缩小的视图
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView;
}
//添加手势
func hand() {
let tapGes = UITapGestureRecognizer(target: self, action: #selector(JumCollectionViewCell.tapAction(_:)))
tapGes.numberOfTapsRequired = 1 //设置点击次数
tapGes.numberOfTouchesRequired = 1 //设置触摸点
imageView?.addGestureRecognizer(tapGes)
let tapGes1 = UITapGestureRecognizer(target: self, action: #selector(JumCollectionViewCell.tapAction(_:)))
tapGes1.numberOfTapsRequired = 2 //设置点击次数
tapGes1.numberOfTouchesRequired = 1 //设置触摸点
imageView?.addGestureRecognizer(tapGes1)
}
func tapAction(tap:UITapGestureRecognizer) {
switch tap.numberOfTapsRequired {
case 1:
//点击一次隐藏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() {
//获取点击视图
let bar = self.viewController()!.navigationController!.navigationBar
let isHidden = !bar.hidden
self.viewController()!.navigationController?.setNavigationBarHidden(isHidden, animated: true)
}
//scroll恢复原样
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()
//设置滚动方向
flowLayout.scrollDirection = .Horizontal
//设置垂直方向得最小距离
flowLayout.minimumInteritemSpacing = 0
//设置水平方向得最小距离
flowLayout.minimumLineSpacing = 0
//设置四周得边缘距离
//初始化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!)
}
//创建item得个数
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return array!.count;
}
//创建item得内容
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
}
//代理方法事项单元格得大小
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进行扩展
import UIKit
extension UIView {
func viewController() -> UIViewController? {
//通过响应者链,取得此视图所在的视图控制器
var next = self.nextResponder()
while(next != nil) {
//判断响应者对象是否是视图控制器类型
if next!.isKindOfClass(UIViewController.self) {
return next as? UIViewController;
}
next = next!.nextResponder()
}
return nil;
}
}
大家如果想下载代码,请加群:512847147,让我们一起快乐的学习swift吧(o)。