// // ImageCell.swift // Paiai_iOS // // Created by FFIB on 16/4/9. // Copyright © 2016年 FFIB. All rights reserved. // import UIKit import PaiaiDataKit import PaiaiUIKit final class ImageCell: UICollectionViewCell, UIScrollViewDelegate { @IBOutlet weak var scrollView: UIScrollView! var photoImage = UIImageView() private var angle: CGFloat = 0 private var scale: CGFloat = 1 private var scaleRatio: CGFloat = 1 private var aspectFitSize = CGSize.zero func setModel(url: String) { photoImage.frame = CGRect(x: 0, y: 0, width: width, height: height) photoImage.contentMode = .scaleAspectFit scrollView.contentSize = size scrollView.addSubview(photoImage) photoImage.setImage(url, placeholder: UIImage.photoPlaceholder) let tapGr = UITapGestureRecognizer(target: self, action: #selector(ImageCell.doubleTap(_:))) tapGr.numberOfTapsRequired = 2 scrollView.addGestureRecognizer(tapGr) } func viewForZooming(in scrollView: UIScrollView) -> UIView? { return photoImage } // MARK: zoom @objc func doubleTap(_ gestureRecognizer: UITapGestureRecognizer) { if scrollView.zoomScale > scrollView.minimumZoomScale { scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true) } else { //Zoom to rect let tapPt = gestureRecognizer.location(in: scrollView) var zoomRect = CGRect.zero zoomRect.size.width = frame.width / scrollView.maximumZoomScale zoomRect.size.height = frame.height / scrollView.maximumZoomScale zoomRect.origin.x = tapPt.x - zoomRect.width / 2 zoomRect.origin.y = tapPt.y - zoomRect.height / 2 if zoomRect.origin.x < 0 { zoomRect.origin.x = 0 } else if zoomRect.maxX > photoImage.frame.maxX { let delta = zoomRect.maxX - photoImage.frame.maxX zoomRect.origin.x -= delta } if zoomRect.origin.y < 0 { zoomRect.origin.y = 0 } else if zoomRect.maxY > photoImage.frame.maxY { let delta = zoomRect.maxY - photoImage.frame.maxY zoomRect.origin.y -= delta } scrollView.zoom(to: zoomRect, animated: true) } } override func prepareForReuse() { super.prepareForReuse() scrollView.zoomScale = 1 clearRotation() } func rotate() { if let imageSize = photoImage.image?.size, aspectFitSize == CGSize.zero { let ratio = size.width / imageSize.width aspectFitSize = CGSize(width: size.width, height: ratio * imageSize.height) scaleRatio = size.width / aspectFitSize.height } let animation1 = CABasicAnimation(keyPath: "transform.rotation.z") animation1.fromValue = angle animation1.toValue = angle + CGFloat.pi * 0.5 let animation2 = CABasicAnimation(keyPath: "transform.scale") let (toScale, fromScale) = scale == 1 ? (scaleRatio, 1) : (1, scaleRatio) animation2.fromValue = CGPoint(x: fromScale, y: fromScale) animation2.toValue = CGPoint(x: toScale, y: toScale) let animationGroup = CAAnimationGroup() animationGroup.animations = [animation1, animation2] animationGroup.duration = 0.5 animationGroup.fillMode = .forwards animationGroup.isRemovedOnCompletion = false photoImage.layer.add(animationGroup, forKey: "") angle += CGFloat.pi * 0.5 scale = toScale } func clearRotation() { angle = 0 scale = 1 } }