Skip to content

Commit

Permalink
Merge pull request #32 from tungvoduc/develop
Browse files Browse the repository at this point in the history
Version 1.2.5
  • Loading branch information
tungvoduc committed Dec 26, 2018
2 parents be93e3a + fa1a242 commit 6be0b3d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 23 deletions.
2 changes: 1 addition & 1 deletion DTPhotoViewerController.podspec
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = "DTPhotoViewerController"
s.version = "1.2.4"
s.version = "1.2.5"
s.summary = "DTPhotoViewerController provides a Facebook-like photo viewer."
s.platform = :ios, '8.3'
s.swift_version = '4.2'
Expand Down
11 changes: 1 addition & 10 deletions DTPhotoViewerController/Classes/DTImageView.swift
Expand Up @@ -8,16 +8,7 @@

import UIKit

class DTImageView: UIImageView {

/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/

class DTImageView: UIImageView {
override var image: UIImage? {
didSet {
imageChangeBlock?(image)
Expand Down
69 changes: 61 additions & 8 deletions DTPhotoViewerController/Classes/DTPhotoCollectionViewCell.swift
Expand Up @@ -18,6 +18,36 @@ open class DTPhotoCollectionViewCell: UICollectionViewCell {
public private(set) var scrollView: DTScrollView!
public private(set) var imageView: UIImageView!

// default is 1.0
open var minimumZoomScale: CGFloat = 1.0 {
willSet {
if imageView.image == nil {
scrollView.minimumZoomScale = 1.0
} else {
scrollView.minimumZoomScale = newValue
}
}

didSet {
correctCurrentZoomScaleIfNeeded()
}
}

// default is 3.0.
open var maximumZoomScale: CGFloat = 3.0 {
willSet {
if imageView.image == nil {
scrollView.maximumZoomScale = 1.0
} else {
scrollView.maximumZoomScale = newValue
}
}

didSet {
correctCurrentZoomScaleIfNeeded()
}
}

weak var delegate: DTPhotoCollectionViewCellDelegate?

public override init(frame: CGRect) {
Expand All @@ -34,42 +64,65 @@ open class DTPhotoCollectionViewCell: UICollectionViewCell {
backgroundColor = UIColor.clear
isUserInteractionEnabled = true
scrollView = DTScrollView(frame: CGRect.zero)
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 3.0
scrollView.minimumZoomScale = minimumZoomScale
scrollView.maximumZoomScale = 1.0 // Not allow zooming when there is no image

let imageView = DTImageView(frame: CGRect.zero)
// Layout subviews every time getting new image
imageView.imageChangeBlock = {[weak self](image: UIImage?) -> Void in
// Update image frame whenever image changes
if let strongSelf = self, let _ = image {
strongSelf.setNeedsLayout()
if let strongSelf = self {
if image == nil {
strongSelf.scrollView.minimumZoomScale = 1.0
strongSelf.scrollView.maximumZoomScale = 1.0
} else {
strongSelf.scrollView.minimumZoomScale = strongSelf.minimumZoomScale
strongSelf.scrollView.maximumZoomScale = strongSelf.maximumZoomScale
strongSelf.setNeedsLayout()
}
strongSelf.correctCurrentZoomScaleIfNeeded()
}
}
imageView.contentMode = .scaleAspectFit
self.imageView = imageView
scrollView.delegate = self
addSubview(scrollView)
scrollView.addSubview(imageView)

if #available(iOS 11.0, *) {
scrollView.contentInsetAdjustmentBehavior = .automatic
}
}

private func correctCurrentZoomScaleIfNeeded() {
if scrollView.zoomScale < scrollView.minimumZoomScale {
scrollView.zoomScale = scrollView.minimumZoomScale
}

if scrollView.zoomScale > scrollView.maximumZoomScale {
scrollView.zoomScale = scrollView.maximumZoomScale
}
}

override open func layoutSubviews() {
super.layoutSubviews()
scrollView.zoomScale = 1.0
scrollView.frame = bounds

//Set the aspect ration of the image
if let image = imageView.image {
let size = image.size
let horizontalScale = size.width / bounds.width
let verticalScale = size.height / bounds.height
let horizontalScale = size.width / scrollView.frame.width
let verticalScale = size.height / scrollView.frame.height
let factor = max(horizontalScale, verticalScale)

//Divide the size by the greater of the vertical or horizontal shrinkage factor
let width = size.width / factor
let height = size.height / factor

//Then figure out offset to center vertically or horizontally
let x = (bounds.width - width) / 2
let y = (bounds.height - height) / 2
let x = (scrollView.frame.width - width) / 2
let y = (scrollView.frame.height - height) / 2

imageView.frame = CGRect(x: x, y: y, width: width, height: height)
}
Expand Down
6 changes: 3 additions & 3 deletions DTPhotoViewerController/Classes/DTPhotoViewerController.swift
Expand Up @@ -344,15 +344,15 @@ open class DTPhotoViewerController: UIViewController {

if (cell.scrollView.zoomScale == cell.scrollView.maximumZoomScale) {
// Zoom out
cell.scrollView.minimumZoomScale = 1.0
cell.minimumZoomScale = 1.0
cell.scrollView.setZoomScale(cell.scrollView.minimumZoomScale, animated: true)

} else {
let location = gesture.location(in: view)
let center = cell.imageView.convert(location, from: view)

// Zoom in
cell.scrollView.minimumZoomScale = 1.0
cell.minimumZoomScale = 1.0
let rect = zoomRect(for: cell.imageView, withScale: cell.scrollView.maximumZoomScale, withCenter: center)
cell.scrollView.zoom(to: rect, animated: true)
}
Expand Down Expand Up @@ -385,7 +385,7 @@ open class DTPhotoViewerController: UIViewController {
let heightScale = size.height / cell.imageView.bounds.height
let zoomScale = min(widthScale, heightScale)

cell.scrollView.maximumZoomScale = zoomScale
cell.maximumZoomScale = zoomScale
}

fileprivate func zoomRect(for imageView: UIImageView, withScale scale: CGFloat, withCenter center: CGPoint) -> CGRect {
Expand Down
2 changes: 1 addition & 1 deletion DTPhotoViewerController/Classes/DTScrollView.swift
Expand Up @@ -28,7 +28,7 @@ public class DTScrollView: UIScrollView {
}
}

//MARK:
//MARK: UIGestureRecognizerDelegate
extension DTScrollView: UIGestureRecognizerDelegate {
public override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer == panGestureRecognizer {
Expand Down

0 comments on commit 6be0b3d

Please sign in to comment.