Skip to content

Commit

Permalink
add 3D Touch
Browse files Browse the repository at this point in the history
  • Loading branch information
gaoyuexit committed Sep 19, 2017
1 parent e6ee024 commit db3a9c1
Showing 1 changed file with 42 additions and 17 deletions.
59 changes: 42 additions & 17 deletions LPAlbum/Controllers/LPAlbum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,27 @@ extension LPAlbum {
cancel()
}
}

func cellDidSelectForPreviewViewController(indexPath: IndexPath) -> UIViewController{
let previewVc = PhotosPreviewController()
let assetModels = albumModels[currentAlbumIndex].assetModels
previewVc.assetModels = assetModels
previewVc.currentIndex = config.hasCamera ? indexPath.row - 1 : indexPath.row
previewVc.chooseAction = {[weak self] (index, button, vc) in
guard let `self` = self else { return }

let willselect = !button.isSelected
guard self.checkoutMaxCount(willselect: willselect, show: vc) else { return }

button.isSelected = willselect
vc.assetModels[index].isSelect = willselect
self.albumModels = self.albumModels.change(assetModel: vc.assetModels[index])

let cellIndex = self.config.hasCamera ? index + 1 : index
self.collectionView.reloadItems(at: [IndexPath(row: cellIndex, section: 0)])
}
return previewVc
}
}


Expand All @@ -203,6 +224,10 @@ extension LPAlbum: UICollectionViewDelegate, UICollectionViewDataSource {
self.albumModels = self.albumModels.change(assetModel: newModel)
self.collectionView.reloadItems(at: [indexPath])
}
if #available(iOS 9.0, *) {
guard traitCollection.forceTouchCapability == .available else { return cell }
registerForPreviewing(with: self, sourceView: cell)
}
return cell
}
}
Expand All @@ -219,23 +244,7 @@ extension LPAlbum: UICollectionViewDelegate, UICollectionViewDataSource {
$0 == .authorized ? self.takePhoto() : self.errorBlock?(self, AlbumError.noCameraPermission)
}
}else{
let previewVc = PhotosPreviewController()
let assetModels = albumModels[currentAlbumIndex].assetModels
previewVc.assetModels = assetModels
previewVc.currentIndex = config.hasCamera ? indexPath.row - 1 : indexPath.row
previewVc.chooseAction = {[weak self] (index, button, vc) in
guard let `self` = self else { return }

let willselect = !button.isSelected
guard self.checkoutMaxCount(willselect: willselect, show: vc) else { return }

button.isSelected = willselect
vc.assetModels[index].isSelect = willselect
self.albumModels = self.albumModels.change(assetModel: vc.assetModels[index])

let cellIndex = self.config.hasCamera ? index + 1 : index
self.collectionView.reloadItems(at: [IndexPath(row: cellIndex, section: 0)])
}
let previewVc = cellDidSelectForPreviewViewController(indexPath: indexPath)
navigationController?.pushViewController(previewVc, animated: true)
}
}
Expand Down Expand Up @@ -285,6 +294,22 @@ extension LPAlbum: UIImagePickerControllerDelegate, UINavigationControllerDelega
}


extension LPAlbum: UIViewControllerPreviewingDelegate {
public func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {

guard let cell = previewingContext.sourceView as? AlbumCollectionCell,
let indexPath = collectionView.indexPath(for: cell) else { return nil }
let previewVc = cellDidSelectForPreviewViewController(indexPath: indexPath)
previewingContext.sourceRect = previewingContext.sourceView.bounds
return previewVc
}

public func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
navigationController?.pushViewController(viewControllerToCommit, animated: true)
}
}





Expand Down

0 comments on commit db3a9c1

Please sign in to comment.