Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. class SNZoomScrollView: UIScrollView {
  2.  
  3. // MARK: Class properties
  4.  
  5. var imageView:UIImageView = UIImageView()
  6. var imageToDisplay:UIImage? = nil{
  7. didSet{
  8. zoomScale = 1.0
  9. minimumZoomScale = 1.0
  10. imageView.image = imageToDisplay
  11. imageView.frame.size = sizeForImageToDisplay()
  12. imageView.center = center
  13. contentSize = imageView.frame.size
  14. contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  15. updateLayout()
  16. }
  17. }
  18. // var gridView:UIView = Bundle.main.loadNibNamed("FAGridView", owner: nil, options: nil)?.first as! UIView
  19.  
  20.  
  21. // MARK : Class Functions
  22.  
  23. override func awakeFromNib() {
  24. super.awakeFromNib()
  25. viewConfigurations()
  26. }
  27.  
  28. func updateLayout() {
  29. imageView.center = center;
  30. var frame:CGRect = imageView.frame;
  31. if (frame.origin.x < 0) { frame.origin.x = 0 }
  32. if (frame.origin.y < 0) { frame.origin.y = 0 }
  33. imageView.frame = frame
  34. }
  35.  
  36. func zoom() {
  37. if (zoomScale <= 1.0) { setZoomScale(zoomScaleWithNoWhiteSpaces(), animated: true) }
  38. else{ setZoomScale(minimumZoomScale, animated: true) }
  39. updateLayout()
  40. }
  41.  
  42.  
  43.  
  44. // MARK: Private Functions
  45.  
  46. private func viewConfigurations() {
  47.  
  48. clipsToBounds = false;
  49. showsVerticalScrollIndicator = false
  50. showsHorizontalScrollIndicator = false
  51. alwaysBounceHorizontal = true
  52. alwaysBounceVertical = true
  53. bouncesZoom = true
  54. decelerationRate = .fast //UIScrollViewDecelerationRateFast
  55. delegate = self
  56. maximumZoomScale = 5.0
  57. addSubview(imageView)
  58.  
  59. // gridView.frame = frame
  60. // gridView.isHidden = true
  61. // gridView.isUserInteractionEnabled = false
  62. // addSubview(gridView)
  63. }
  64.  
  65. private func sizeForImageToDisplay() -> CGSize{
  66.  
  67. var actualWidth:CGFloat = imageToDisplay!.size.width
  68. var actualHeight:CGFloat = imageToDisplay!.size.height
  69. var imgRatio:CGFloat = actualWidth/actualHeight
  70. let maxRatio:CGFloat = frame.size.width/frame.size.height
  71.  
  72. if imgRatio != maxRatio{
  73. if(imgRatio < maxRatio){
  74. imgRatio = frame.size.height / actualHeight
  75. actualWidth = imgRatio * actualWidth
  76. actualHeight = frame.size.height
  77. }
  78. else{
  79. imgRatio = frame.size.width / actualWidth
  80. actualHeight = imgRatio * actualHeight
  81. actualWidth = frame.size.width
  82. }
  83. }
  84. else {
  85. imgRatio = frame.size.width / actualWidth
  86. actualHeight = imgRatio * actualHeight
  87. actualWidth = frame.size.width
  88. }
  89.  
  90. return CGSize(width: actualWidth, height: actualHeight)
  91. }
  92.  
  93. private func zoomScaleWithNoWhiteSpaces() -> CGFloat{
  94.  
  95. let imageViewSize:CGSize = imageView.bounds.size
  96. let scrollViewSize:CGSize = bounds.size;
  97. let widthScale:CGFloat = scrollViewSize.width / imageViewSize.width
  98. let heightScale:CGFloat = scrollViewSize.height / imageViewSize.height
  99. return max(widthScale, heightScale)
  100. }
  101.  
  102. }
  103.  
  104.  
  105.  
  106. extension SNZoomScrollView: UIScrollViewDelegate {
  107.  
  108. func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  109. return imageView
  110. }
  111.  
  112. func scrollViewDidZoom(_ scrollView: UIScrollView) {
  113. updateLayout()
  114. }
  115.  
  116. // func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
  117. // gridView.isHidden = false
  118. // }
  119. //
  120. // func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
  121. // gridView.isHidden = true
  122. // }
  123.  
  124. // func scrollViewDidScroll(_ scrollView: UIScrollView) {
  125. //
  126. // var frame:CGRect = gridView.frame;
  127. // frame.origin.x = scrollView.contentOffset.x
  128. // frame.origin.y = scrollView.contentOffset.y
  129. // gridView.frame = frame
  130. //
  131. // switch scrollView.pinchGestureRecognizer!.state {
  132. // case .changed:
  133. // gridView.isHidden = false
  134. // break
  135. // case .ended:
  136. // gridView.isHidden = true
  137. // break
  138. // default: break
  139. // }
  140. //
  141. // }
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement