我有一个带有UIScrollView的collectionViewCell,它包含一个UIImageView。在这个scrollView中,我可以放大和缩小图片。我想知道如何将我看到的状态传递给另一个视图控制器?
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)
}
}