NCMediaPinchGesture.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // MediaZoom.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 13/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 NCMedia {
  26. @objc func handlePinchGesture(_ gestureRecognizer: UIPinchGestureRecognizer) {
  27. func updateNumberOfColumns() {
  28. let originalColumns = numberOfColumns
  29. transitionColumns = true
  30. if currentScale < 1 && numberOfColumns < maxColumns {
  31. numberOfColumns += 1
  32. } else if currentScale > 1 && numberOfColumns > 1 {
  33. numberOfColumns -= 1
  34. }
  35. if originalColumns != numberOfColumns {
  36. self.collectionView.transform = .identity
  37. self.currentScale = 1.0
  38. UIView.transition(with: self.collectionView, duration: 0.20, options: .transitionCrossDissolve) {
  39. self.collectionView.reloadData()
  40. self.collectionView.collectionViewLayout.invalidateLayout()
  41. } completion: { _ in
  42. if let layoutForView = self.database.getLayoutForView(account: self.session.account, key: self.global.layoutViewMedia, serverUrl: "") {
  43. layoutForView.columnPhoto = self.numberOfColumns
  44. self.database.setLayoutForView(layoutForView: layoutForView)
  45. }
  46. }
  47. }
  48. DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) {
  49. self.transitionColumns = false
  50. }
  51. }
  52. switch gestureRecognizer.state {
  53. case .began:
  54. networkRemoveAll()
  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. self.setTitleDate()
  75. }
  76. default:
  77. break
  78. }
  79. }
  80. }