From 55734f1579a35bb29dcf3d8e390003f707ce4e8c Mon Sep 17 00:00:00 2001 From: haranicle Date: Tue, 24 May 2016 13:51:40 +0900 Subject: [PATCH 1/3] working --- .../AlbumListViewController.swift | 12 +++++++++--- .../AssetDetailListViewController.swift | 18 +++++++++--------- .../AssetListViewController.swift | 18 ++++++++++++------ NohanaImagePicker/MomentViewController.swift | 8 ++------ .../NohanaImagePickerController.swift | 6 +++--- NohanaImagePicker/Size.swift | 9 ++++++++- 6 files changed, 43 insertions(+), 28 deletions(-) diff --git a/NohanaImagePicker/AlbumListViewController.swift b/NohanaImagePicker/AlbumListViewController.swift index a773c58..538b5b5 100644 --- a/NohanaImagePicker/AlbumListViewController.swift +++ b/NohanaImagePicker/AlbumListViewController.swift @@ -51,6 +51,11 @@ class AlbumListViewController: UITableViewController, EmptyIndicatable, Activity tableView.deselectRowAtIndexPath(indexPathForSelectedRow, animated: true) } } + + override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { + super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator) + tableView.reloadData() + } // MARK: - UITableViewDelegate @@ -160,9 +165,10 @@ class AlbumListViewController: UITableViewController, EmptyIndicatable, Activity mediaType: nohanaImagePickerController.mediaType, shouldShowEmptyAlbum: nohanaImagePickerController.shouldShowEmptyAlbum, handler: { () -> Void in - dispatch_async(dispatch_get_main_queue(), { () -> Void in - momentViewController.isLoading = false - momentViewController.collectionView?.reloadData() + dispatch_async(dispatch_get_main_queue(), { [weak momentViewController] in + momentViewController?.isLoading = false + momentViewController?.collectionView?.reloadData() + momentViewController?.scrollCollectionViewToInitialPosition() }) }) case .Albums: diff --git a/NohanaImagePicker/AssetDetailListViewController.swift b/NohanaImagePicker/AssetDetailListViewController.swift index d6355d9..9cfba15 100644 --- a/NohanaImagePicker/AssetDetailListViewController.swift +++ b/NohanaImagePicker/AssetDetailListViewController.swift @@ -36,10 +36,6 @@ class AssetDetailListViewController: AssetListViewController { forState: [.Normal, .Selected]) } - override func viewWillAppear(animated: Bool) { - super.viewWillAppear(animated) - } - override func updateTitle() { self.title = "" } @@ -51,16 +47,20 @@ class AssetDetailListViewController: AssetListViewController { nohanaImagePickerController.delegate?.nohanaImagePicker?(nohanaImagePickerController, assetDetailListViewController: self, didChangeAssetDetailPage: indexPath, photoKitAsset: asset.originalAsset) } + override func scrollCollectionView(to indexPath: NSIndexPath) { + guard photoKitAssetList.count > 0 else { + return + } + collectionView?.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: false) + } + override func scrollCollectionViewToInitialPosition() { guard isFirstAppearance else { return } - isFirstAppearance = false - guard photoKitAssetList.count > 0 else { - return - } let indexPath = NSIndexPath(forRow: currentIndexPath.item, inSection: 0) - collectionView?.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: false) + scrollCollectionView(to: indexPath) + isFirstAppearance = false } // MARK: - IBAction diff --git a/NohanaImagePicker/AssetListViewController.swift b/NohanaImagePicker/AssetListViewController.swift index 66f5b54..b1370fc 100644 --- a/NohanaImagePicker/AssetListViewController.swift +++ b/NohanaImagePicker/AssetListViewController.swift @@ -45,22 +45,28 @@ class AssetListViewController: UICollectionViewController { scrollCollectionViewToInitialPosition() } + override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { + super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator) + collectionView?.reloadData() + scrollCollectionViewToInitialPosition() + } + var isFirstAppearance = true func updateTitle() { title = photoKitAssetList.title } + func scrollCollectionView(to indexPath: NSIndexPath) { + collectionView?.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: false) + } + func scrollCollectionViewToInitialPosition() { guard isFirstAppearance else { return } - guard photoKitAssetList.count > 0 else { - return - } - let index = NSIndexPath(forRow: self.photoKitAssetList.count - 1, inSection: 0) - collectionView?.scrollToItemAtIndexPath(index, atScrollPosition: .Bottom, animated: false) - + let indexPath = NSIndexPath(forRow: self.photoKitAssetList.count - 1, inSection: 0) + scrollCollectionView(to: indexPath) isFirstAppearance = false } diff --git a/NohanaImagePicker/MomentViewController.swift b/NohanaImagePicker/MomentViewController.swift index 89c3b3a..f46b2c6 100644 --- a/NohanaImagePicker/MomentViewController.swift +++ b/NohanaImagePicker/MomentViewController.swift @@ -26,18 +26,14 @@ class MomentViewController: AssetListViewController, ActivityIndicatable { guard isFirstAppearance else { return } - guard let collectionView = collectionView else { - return - } let lastSection = momentAlbumList.count - 1 guard lastSection >= 0 else { return } - let index = NSIndexPath(forItem: momentAlbumList[lastSection].count - 1, inSection: lastSection) - collectionView.scrollToItemAtIndexPath(index, atScrollPosition: .Bottom, animated: false) + let indexPath = NSIndexPath(forItem: momentAlbumList[lastSection].count - 1, inSection: lastSection) + scrollCollectionView(to: indexPath) isFirstAppearance = false - } // MARK: - UICollectionViewDataSource diff --git a/NohanaImagePicker/NohanaImagePickerController.swift b/NohanaImagePicker/NohanaImagePickerController.swift index 135d805..c95b766 100644 --- a/NohanaImagePicker/NohanaImagePickerController.swift +++ b/NohanaImagePicker/NohanaImagePickerController.swift @@ -100,10 +100,10 @@ public class NohanaImagePickerController: UIViewController { assetCollectionSubtypes: assetCollectionSubtypes, mediaType: mediaType, shouldShowEmptyAlbum: shouldShowEmptyAlbum, - handler: { + handler: { [weak albumListViewController] in dispatch_async(dispatch_get_main_queue(), { () -> Void in - albumListViewController.isLoading = false - albumListViewController.tableView.reloadData() + albumListViewController?.isLoading = false + albumListViewController?.tableView.reloadData() }) }) albumListViewController.nohanaImagePickerController = self diff --git a/NohanaImagePicker/Size.swift b/NohanaImagePicker/Size.swift index ed56025..f1f427b 100644 --- a/NohanaImagePicker/Size.swift +++ b/NohanaImagePicker/Size.swift @@ -8,7 +8,14 @@ struct Size { - static let statusBarHeight = UIApplication.sharedApplication().statusBarFrame.size.height + static var statusBarHeight: CGFloat { + get { + if UIApplication.sharedApplication().statusBarHidden { + return 0 + } + return UIApplication.sharedApplication().statusBarFrame.size.height + } + } static func navigationBarHeight(viewController: UIViewController) -> CGFloat { return viewController.navigationController?.navigationBar.frame.size.height ?? CGFloat(44) From 6bc31921a959045c865494fa2fdcfc7f4b04aa6e Mon Sep 17 00:00:00 2001 From: haranicle Date: Thu, 26 May 2016 14:39:07 +0900 Subject: [PATCH 2/3] remove codes which is not used --- NohanaImagePicker/AssetCell.swift | 2 +- NohanaImagePicker/AssetDetailListViewController.swift | 3 +-- NohanaImagePicker/AssetListViewController.swift | 3 +-- NohanaImagePicker/MomentViewController.swift | 3 +-- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/NohanaImagePicker/AssetCell.swift b/NohanaImagePicker/AssetCell.swift index 957bd1e..0ed54d9 100644 --- a/NohanaImagePicker/AssetCell.swift +++ b/NohanaImagePicker/AssetCell.swift @@ -29,7 +29,7 @@ class AssetCell: UICollectionViewCell { } @IBAction func didPushPickButton(sender: UIButton) { - guard let asset = asset, nohanaImagePickerController = nohanaImagePickerController else { + guard let asset = asset else { return } if pickButton.selected { diff --git a/NohanaImagePicker/AssetDetailListViewController.swift b/NohanaImagePicker/AssetDetailListViewController.swift index 9cfba15..9d4cc45 100644 --- a/NohanaImagePicker/AssetDetailListViewController.swift +++ b/NohanaImagePicker/AssetDetailListViewController.swift @@ -81,8 +81,7 @@ class AssetDetailListViewController: AssetListViewController { // MARK: - UICollectionViewDelegate override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { - guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AssetDetailCell", forIndexPath: indexPath) as? AssetDetailCell, - nohanaImagePickerController = nohanaImagePickerController + guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AssetDetailCell", forIndexPath: indexPath) as? AssetDetailCell else { fatalError("failed to dequeueReusableCellWithIdentifier(\"AssetDetailCell\")") } diff --git a/NohanaImagePicker/AssetListViewController.swift b/NohanaImagePicker/AssetListViewController.swift index b1370fc..6c39e93 100644 --- a/NohanaImagePicker/AssetListViewController.swift +++ b/NohanaImagePicker/AssetListViewController.swift @@ -83,8 +83,7 @@ class AssetListViewController: UICollectionViewController { } override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { - guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AssetCell", forIndexPath: indexPath) as? AssetCell, - nohanaImagePickerController = nohanaImagePickerController else { + guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AssetCell", forIndexPath: indexPath) as? AssetCell else { fatalError("failed to dequeueReusableCellWithIdentifier(\"AssetCell\")") } cell.tag = indexPath.item diff --git a/NohanaImagePicker/MomentViewController.swift b/NohanaImagePicker/MomentViewController.swift index f46b2c6..c782912 100644 --- a/NohanaImagePicker/MomentViewController.swift +++ b/NohanaImagePicker/MomentViewController.swift @@ -53,8 +53,7 @@ class MomentViewController: AssetListViewController, ActivityIndicatable { // MARK: - UICollectionViewDelegate override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { - guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AssetCell", forIndexPath: indexPath) as? AssetCell, - nohanaImagePickerController = nohanaImagePickerController else { + guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AssetCell", forIndexPath: indexPath) as? AssetCell else { return UICollectionViewCell(frame: CGRectZero) } From 0a9ff31c41f7356aef0187902b00b397296fbdc1 Mon Sep 17 00:00:00 2001 From: haranicle Date: Thu, 16 Jun 2016 18:23:10 +0900 Subject: [PATCH 3/3] fix crash when rotate screen --- .../AnimatableNavigationController.swift | 2 +- .../AssetDetailListViewController.swift | 26 +++++++++++++++++-- .../AssetListViewController.swift | 20 +++++++++++--- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/NohanaImagePicker/AnimatableNavigationController.swift b/NohanaImagePicker/AnimatableNavigationController.swift index a39020c..d75f9a5 100644 --- a/NohanaImagePicker/AnimatableNavigationController.swift +++ b/NohanaImagePicker/AnimatableNavigationController.swift @@ -8,7 +8,7 @@ import UIKit -class AnimatableNavigationController: UINavigationController, UINavigationControllerDelegate, UIViewControllerTransitioningDelegate { +class AnimatableNavigationController: UINavigationController, UINavigationControllerDelegate { let swipeInteractionController = SwipeInteractionController() diff --git a/NohanaImagePicker/AssetDetailListViewController.swift b/NohanaImagePicker/AssetDetailListViewController.swift index 9d4cc45..e5e92db 100644 --- a/NohanaImagePicker/AssetDetailListViewController.swift +++ b/NohanaImagePicker/AssetDetailListViewController.swift @@ -36,6 +36,20 @@ class AssetDetailListViewController: AssetListViewController { forState: [.Normal, .Selected]) } + override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { + let indexPath = currentIndexPath + view.hidden = true + coordinator.animateAlongsideTransition(nil) { _ in + self.view.invalidateIntrinsicContentSize() + for subView in self.view.subviews { + subView.invalidateIntrinsicContentSize() + } + self.collectionView?.reloadData() + self.scrollCollectionView(to: indexPath) + self.view.hidden = false + } + } + override func updateTitle() { self.title = "" } @@ -51,7 +65,8 @@ class AssetDetailListViewController: AssetListViewController { guard photoKitAssetList.count > 0 else { return } - collectionView?.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: false) + let toIndexPath = NSIndexPath(forItem: indexPath.item, inSection: 0) + collectionView?.scrollToItemAtIndexPath(toIndexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: false) } override func scrollCollectionViewToInitialPosition() { @@ -85,6 +100,7 @@ class AssetDetailListViewController: AssetListViewController { else { fatalError("failed to dequeueReusableCellWithIdentifier(\"AssetDetailCell\")") } + cell.invalidateIntrinsicContentSize() cell.scrollView.zoomScale = 1 cell.tag = indexPath.item @@ -114,7 +130,13 @@ class AssetDetailListViewController: AssetListViewController { return } let row = Int((collectionView.contentOffset.x + cellSize.width * 0.5) / cellSize.width) - currentIndexPath = NSIndexPath(forRow: row, inSection: currentIndexPath.section) + if row < 0 { + currentIndexPath = NSIndexPath(forRow: 0, inSection: currentIndexPath.section) + } else if row >= collectionView.numberOfItemsInSection(0) { + currentIndexPath = NSIndexPath(forRow: collectionView.numberOfItemsInSection(0) - 1, inSection: currentIndexPath.section) + } else { + currentIndexPath = NSIndexPath(forRow: row, inSection: currentIndexPath.section) + } } // MARK: - UICollectionViewDelegateFlowLayout diff --git a/NohanaImagePicker/AssetListViewController.swift b/NohanaImagePicker/AssetListViewController.swift index 6c39e93..fd50a83 100644 --- a/NohanaImagePicker/AssetListViewController.swift +++ b/NohanaImagePicker/AssetListViewController.swift @@ -41,14 +41,27 @@ class AssetListViewController: UICollectionViewController { override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) setToolbarTitle(nohanaImagePickerController) + view.invalidateIntrinsicContentSize() + for subView in view.subviews { + subView.invalidateIntrinsicContentSize() + } collectionView?.reloadData() + scrollCollectionViewToInitialPosition() } override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator) - collectionView?.reloadData() - scrollCollectionViewToInitialPosition() + view.hidden = true + coordinator.animateAlongsideTransition(nil) { _ in + // http://saygoodnight.com/2015/06/18/openpics-swift-rotation.html + if self.navigationController?.visibleViewController != self { + self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, size.width, size.height) + } + self.collectionView?.reloadData() + self.scrollCollectionViewToInitialPosition() + self.view.hidden = false + } } var isFirstAppearance = true @@ -70,8 +83,7 @@ class AssetListViewController: UICollectionViewController { isFirstAppearance = false } - // MARK: - UICollectionViewDataSource - + // MARK: - UICollectionViewDataSource override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return photoKitAssetList.count }