1

I have a collectionViewCell with a UIScrollView which contains a UIImageView. In this scrollView I can zoom in and out of a picture. I want to know how I can pass the image with the state I see in to the another view controller??

class ScrollableSelectedPhoto: UICollectionViewCell, UIScrollViewDelegate {

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var selectedPhoto: UIImageView!
    @IBOutlet weak var selectedPhotoBottomConstraint: NSLayoutConstraint!
    @IBOutlet weak var selectedPhotoTopConstraint: NSLayoutConstraint!
    @IBOutlet weak var selectedPhotoLeadingConstraint: NSLayoutConstraint!
    @IBOutlet weak var selectedPhotoTrailingConstraint: NSLayoutConstraint!

    override func awakeFromNib() {
        self.scrollView.minimumZoomScale = 1.5
        self.scrollView.maximumZoomScale = 3.5
        self.scrollView.delegate = self

        updateMinZoomScaleForSize(size: self.bounds.size)
    }

    func updateMinZoomScaleForSize(size: CGSize) {
        let widthScale = size.width / selectedPhoto.bounds.width
        let heightScale = size.height / selectedPhoto.bounds.height
        let minScale = min(widthScale, heightScale)

        scrollView.minimumZoomScale = minScale

        scrollView.zoomScale = minScale
    }

    func updateConstraintsForSize(size: CGSize) {

        let yOffset = max(0, (size.height - selectedPhoto.frame.height) / 2)
        selectedPhotoTopConstraint.constant = yOffset
        selectedPhotoBottomConstraint.constant = yOffset

        let xOffset = max(0, (size.width - selectedPhoto.frame.width) / 2)
        selectedPhotoLeadingConstraint.constant = xOffset
        selectedPhotoTrailingConstraint.constant = xOffset

        self.layoutIfNeeded()
    }

    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return self.selectedPhoto
    }

    func scrollViewDidZoom(_ scrollView: UIScrollView) {
        updateConstraintsForSize(size: self.bounds.size)
    }
}
Fuad Adetoro
  • 244
  • 4
  • 10

0 Answers0