Skip to content

Instantly share code, notes, and snippets.

@fumiyasac
Last active December 20, 2016 16:48
Show Gist options
  • Save fumiyasac/3b9d4bf5082ba53800f8869fc0765b4f to your computer and use it in GitHub Desktop.
Save fumiyasac/3b9d4bf5082ba53800f8869fc0765b4f to your computer and use it in GitHub Desktop.
FacebookやTwitterのアプリで気になった表現を自分なりにトレースした際の実装ポイントまとめ(タイルレイアウトがサムネイル画像の枚数に応じて変わる表現) ref: http://qiita.com/fumiyasac@github/items/3be1344255b3ebb9f416
//「もっと他の画像を見る」ボタンのアクション
@IBAction func moreImageAction(_ sender: UIButton) {
transitionClosure!(nil)
}
//サムネイル画像のTapGesture発動時に実行されるメソッド
func tapGesture(sender: UITapGestureRecognizer) {
let targetNumber: Int = (sender.view?.tag)!
transitionClosure!(targetNumber)
}
//このコレクションビューのセル内へ写真の配置を行う際の処理
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCell", for: indexPath) as? PhotoCell
//cellへの画像表示
cell?.photoImageView.image = convertAssetThumbnail(asset: photoAssetLists[indexPath.row], rectSize: 100)
UIView.animate(withDuration: 0.28, delay: 0.0, options: UIViewAnimationOptions.curveEaseOut, animations:{
cell?.photoImageView.alpha = 1
}, completion: nil)
return cell!
}
//画像を同期するアクション
@IBAction func photoSyncAction(_ sender: UIButton) {
//画像のアセットリストを一旦クリアして再度写真データを読み込む
photoAssetLists.removeAll()
dispatchPhotoLibraryAndReload()
}
//フォトライブラリを非同期で読み込む処理
fileprivate func dispatchPhotoLibraryAndReload() {
//データの取得はサブスレッドで行う
DispatchQueue.global().async {
self.photoCollectionView.isUserInteractionEnabled = false
self.getPHAssetsForImageLibrary()
//コレクションビューのリロードはメインスレッドで行う
DispatchQueue.main.async {
self.photoCollectionView.isUserInteractionEnabled = true
self.photoCollectionView.reloadData()
}
}
}
//PHAssetクラスを使用して画像を取得する
fileprivate func getPHAssetsForImageLibrary() {
//データの並べ替え条件
let options = PHFetchOptions()
options.sortDescriptors = [
NSSortDescriptor(key: "creationDate", ascending: true)
]
//Photoライブラリから画像を取得する
let assets: PHFetchResult = PHAsset.fetchAssets(with: .image, options: options)
assets.enumerateObjects( { (asset, index, stop) -> Void in
self.photoAssetLists.append(asset as PHAsset)
})
photoAssetLists.reverse()
}
//PHAsset型のデータを表示用に変換する
fileprivate func convertAssetThumbnail(asset: PHAsset, rectSize: Int) -> UIImage {
let manager = PHImageManager.default()
let option = PHImageRequestOptions()
var thumbnail = UIImage()
option.isSynchronous = true
manager.requestImage(for: asset, targetSize: CGSize(width: rectSize, height: rectSize), contentMode: .aspectFill, options: option, resultHandler: {(result, info) -> Void in
thumbnail = result!
})
return thumbnail
}
//テーブルビューのセル設定を行う
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
・・・(省略)・・・
case 1:
・・・(省略)・・・
case 2:
・・・(省略)・・・
//MainContentsCell側に設定したクロージャーの内部の処理を記載する
cell.transitionClosure = { [weak self] num in
if num != nil {
//MainContentsCell側の画像がタップがされた場合は該当画像の表示をポップアップで行う
let toVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ImageController") as! ImageController
toVC.targetImageList = imageList
toVC.targetImageCount = num!
self?.present(toVC, animated: false, completion: nil)
} else {
//MainContentsCell側のボタンがタップがされた場合は画像一覧の表示を行う
let toVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DetailController") as! DetailController
toVC.targetImageList = imageList
self?.navigationController?.pushViewController(toVC, animated: true)
}
}
・・・(省略)・・・
default:
・・・(省略)・・・
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment