NCCollectionViewCommonPinchGesture.swift 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // NCCollectionViewCommonPinchGesture.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 23/09/24.
  6. // Copyright © 2024 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import UIKit
  25. extension NCCollectionViewCommon {
  26. @objc func handlePinchGesture(_ gestureRecognizer: UIPinchGestureRecognizer) {
  27. guard isLayoutPhoto else { return }
  28. func updateNumberOfColumns() {
  29. let originalColumns = numberOfColumns
  30. transitionColumns = true
  31. if currentScale < 1 && numberOfColumns < maxColumns {
  32. numberOfColumns += 1
  33. } else if currentScale > 1 && numberOfColumns > 1 {
  34. numberOfColumns -= 1
  35. }
  36. if originalColumns != numberOfColumns {
  37. self.collectionView.transform = .identity
  38. self.currentScale = 1.0
  39. UIView.transition(with: self.collectionView, duration: 0.20, options: .transitionCrossDissolve) {
  40. self.collectionView.reloadData()
  41. self.collectionView.collectionViewLayout.invalidateLayout()
  42. } completion: { _ in
  43. if let layoutForView = self.database.getLayoutForView(account: self.session.account, key: NCGlobal.shared.layoutViewFiles, serverUrl: self.serverUrl) {
  44. layoutForView.columnPhoto = self.numberOfColumns
  45. self.database.setLayoutForView(layoutForView: layoutForView)
  46. }
  47. }
  48. }
  49. DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) {
  50. self.transitionColumns = false
  51. }
  52. }
  53. switch gestureRecognizer.state {
  54. case .began:
  55. lastScale = gestureRecognizer.scale
  56. lastNumberOfColumns = numberOfColumns
  57. case .changed:
  58. guard !transitionColumns else {
  59. return
  60. }
  61. let scale = gestureRecognizer.scale
  62. let scaleChange = scale / lastScale
  63. currentScale *= scaleChange
  64. currentScale = max(0.5, min(currentScale, 2.0))
  65. updateNumberOfColumns()
  66. if numberOfColumns > 1 && numberOfColumns < maxColumns {
  67. collectionView.transform = CGAffineTransform(scaleX: currentScale, y: currentScale)
  68. }
  69. lastScale = scale
  70. case .ended:
  71. UIView.animate(withDuration: 0.30) {
  72. self.currentScale = 1.0
  73. self.collectionView.transform = .identity
  74. }
  75. default:
  76. break
  77. }
  78. }
  79. }