-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
221 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// | ||
// AlbumDetailController.swift | ||
// LPAlbum | ||
// | ||
// Created by 郜宇 on 2017/9/12. | ||
// Copyright © 2017年 Loopeer. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
class PhotosPreviewController: UIViewController { | ||
|
||
var assetModels: [AssetModel]! | ||
var currentIndex: Int! | ||
|
||
var chooseAction: ((AssetModel, Int) -> Void)? | ||
|
||
fileprivate var collectionView: UICollectionView! | ||
fileprivate let itemPadding: CGFloat = 20.0 | ||
fileprivate let chooseButton = UIButton() | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
setupUI() | ||
} | ||
} | ||
|
||
extension PhotosPreviewController { | ||
|
||
func setupUI(){ | ||
// 将collectionView多余的部分切到 | ||
view.clipsToBounds = true | ||
|
||
chooseButton.frame = CGRect(x: 0, y: 0, width: 25, height: 25) | ||
chooseButton.setBackgroundImage(LPAlbum.Style.selectedBox, for: .selected) | ||
chooseButton.setBackgroundImage(LPAlbum.Style.normalBox, for: .normal) | ||
chooseButton.isSelected = assetModels[currentIndex].isSelect | ||
chooseButton.addTarget(self, action: #selector(chooseClick), for: .touchUpInside) | ||
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: chooseButton) | ||
|
||
let layout = UICollectionViewFlowLayout() | ||
let itemSize = CGSize(width: view.bounds.width + 20, height: view.bounds.height - 64) | ||
layout.itemSize = itemSize | ||
layout.minimumLineSpacing = 0 | ||
layout.minimumInteritemSpacing = 0 | ||
layout.scrollDirection = .horizontal | ||
collectionView = UICollectionView(frame: CGRect(x: -10, y: 0, width: itemSize.width, height: itemSize.height), | ||
collectionViewLayout: layout) | ||
collectionView.isPagingEnabled = true | ||
collectionView.showsHorizontalScrollIndicator = false | ||
collectionView.delegate = self | ||
collectionView.dataSource = self | ||
view.addSubview(collectionView) | ||
collectionView.register(PhotoPreviewCell.self, forCellWithReuseIdentifier: PhotoPreviewCell.description()) | ||
collectionView.performBatchUpdates(nil){ _ in | ||
self.collectionView.setContentOffset(CGPoint(x: itemSize.width * self.currentIndex.cgFloat, y: 0), animated: false) | ||
} | ||
} | ||
func chooseClick() { | ||
chooseButton.isSelected = !chooseButton.isSelected | ||
assetModels[currentIndex].isSelect = chooseButton.isSelected | ||
|
||
var model = assetModels[currentIndex] | ||
model.isSelect = chooseButton.isSelected | ||
chooseAction?(model, currentIndex) | ||
} | ||
} | ||
|
||
extension PhotosPreviewController: UICollectionViewDelegate, UICollectionViewDataSource { | ||
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | ||
return assetModels.count | ||
} | ||
|
||
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | ||
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PhotoPreviewCell.description(), for: indexPath) as! PhotoPreviewCell | ||
cell.assetModel = assetModels[indexPath.row] | ||
return cell | ||
} | ||
func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { | ||
(cell as? PhotoPreviewCell)?.resetZoomScale() | ||
} | ||
|
||
func scrollViewDidScroll(_ scrollView: UIScrollView) { | ||
currentIndex = (scrollView.contentOffset.x / scrollView.bounds.width).roundInt | ||
chooseButton.isSelected = assetModels[currentIndex].isSelect | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// | ||
// PhotoPreviewCell.swift | ||
// LPAlbum | ||
// | ||
// Created by 郜宇 on 2017/9/14. | ||
// Copyright © 2017年 Loopeer. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
class PhotoPreviewCell: UICollectionViewCell { | ||
|
||
var assetModel: AssetModel! { | ||
didSet{ | ||
|
||
AlbumManager.getPhoto(asset: assetModel.asset, targetSize: bounds.size) { (image, _) in | ||
self.photoView.image = image | ||
} | ||
} | ||
} | ||
func resetZoomScale() { scrollView.setZoomScale(1.0, animated: false) } | ||
|
||
fileprivate let photoView = UIImageView() | ||
fileprivate let scrollView = UIScrollView() | ||
|
||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
|
||
scrollView.frame = bounds.insetBy(dx: 10, dy: 0) | ||
scrollView.minimumZoomScale = 1 | ||
scrollView.maximumZoomScale = 3 | ||
scrollView.bouncesZoom = true | ||
scrollView.delaysContentTouches = true | ||
scrollView.canCancelContentTouches = true | ||
scrollView.delegate = self | ||
|
||
photoView.contentMode = .scaleAspectFit | ||
photoView.frame = scrollView.bounds | ||
scrollView.addSubview(photoView) | ||
contentView.addSubview(scrollView) | ||
|
||
} | ||
|
||
required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
} | ||
|
||
extension PhotoPreviewCell: UIScrollViewDelegate { | ||
func viewForZooming(in scrollView: UIScrollView) -> UIView? { | ||
return photoView | ||
} | ||
|
||
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) { | ||
scrollView.contentInset = .zero | ||
} | ||
|
||
func scrollViewDidZoom(_ scrollView: UIScrollView) { | ||
// let scrollViewW = scrollView.frame.width | ||
// let scrollViewH = scrollView.frame.height | ||
// let contentSizeW = scrollView.contentSize.width | ||
// let contentSizeH = scrollView.contentSize.height | ||
// | ||
// let offsetX = scrollViewW > contentSizeW ? scrollViewW - contentSizeW * 0.5 : 0 | ||
// let offsetY = scrollViewH > contentSizeH ? scrollViewH - contentSizeH * 0.5 : 0 | ||
// photoView.center = CGPoint(x: contentSizeW * 0.5 + offsetX, y: contentSizeH * 0.5 + offsetY) | ||
} | ||
|
||
} | ||
|
||
|