From b8070fc05ed03fd6e62fdc2f720021da1b63d3f2 Mon Sep 17 00:00:00 2001 From: Eugene Mozharovsky Date: Mon, 28 Nov 2016 01:46:41 +0300 Subject: [PATCH] ScaleToFill factor fixes & added childContainerView #4 --- .DS_Store | Bin 8196 -> 10244 bytes .../project.pbxproj | 131 ++++++++++-------- .../CropViewController.swift | 1 + Source/.DS_Store | Bin 8196 -> 8196 bytes Source/CGAffineTransform.swift | 39 ++++++ Source/Cropable.swift | 36 +++-- Source/CropableScrollViewDelegate.swift | 2 +- Source/Radians.swift | 21 +++ 8 files changed, 157 insertions(+), 73 deletions(-) create mode 100644 Source/CGAffineTransform.swift create mode 100644 Source/Radians.swift diff --git a/.DS_Store b/.DS_Store index 03d060069e560ffa82fb73d9dc67a698782b2662..6c25a14cbd0381a6e5bf3bf68f5b1777bf367454 100644 GIT binary patch delta 1074 zcmeHFO-~a+7=GVYN-Nc!mbL;`f|i2`i7AOUW2iMoi7{PhB`tzwKc>ruWoDV(f^ew^ zt{$2_`vW{S97#NQ^5Rc0o;~T!cyo3aME`ZyX|lPsAY3qIsO4N}kq4t)?{0JV*MS9iEBHQwv4V|#wJ`h^fo zBrlDn#?zV06WJ?Q6_U$O<_fuDv2<;!I6ZTHrb{Mu&DgO7?=$lucetoKefq)@w%K$w z!)8|7r?32`eMGw?|5xdNi&bjAyoj|9U0GDP2t<011*I$!Z1j2__1k~&PM0X2A-LnH z+_Qa~v(ARM%@3A+y1UGJTwb|v(}RW^ro1&OJc;K$gWpZ+P0HzDy+Xok_=k7S^fR|<*PBGJrJs2UaqZuw1s#rgmC;Lbm6k6^ZR F?-xA77%2b% delta 159 zcmZn(XmOBWU|?W$DortDU;r^WfEYvza8E20o2aMAD7`UYH$S8FW*z}aR>mKb9|^4D z6tAu}wbW5CGn_n4P?_n_k;%IS9r$L delta 115 zcmZp1XmQveEy$=mSw_&3v2OANL21Dt1_p*1|G@yrVqj35yhqR!BBy)<$k#xU<7aSY xC}PNGC}2orNMgugNM!( Double { + var theta = fabs(angle - 2 * .pi * trunc(angle / .pi / 2) - .pi) + + if theta > .pi / 2 { + theta = fabs(.pi - theta) + } + + let h = Double(contentSize.height) + let H = Double(containerSize.height) + let w = Double(contentSize.width) + let W = Double(containerSize.width) + + let scale1 = (H * cos(theta) + W * sin(theta)) / min(H, h) + let scale2 = (H * sin(theta) + W * cos(theta)) / min(W, w) + + let scalingFactor = max(scale1, scale2) + + return scalingFactor + } + + func scaling(toFill containerSize: CGSize, with contentSize: CGSize, atAngle angle: Double) -> CGAffineTransform { + let factor = CGFloat(CGAffineTransform.scalingFactor(toFill: containerSize, + with: contentSize, + atAngle: angle)) + + return self.scaledBy(x: factor, y: factor) + } +} diff --git a/Source/Cropable.swift b/Source/Cropable.swift index b0e2445..37db875 100644 --- a/Source/Cropable.swift +++ b/Source/Cropable.swift @@ -18,6 +18,10 @@ public protocol Cropable { /// A cropable area containing the content. var cropView: UIScrollView { get set } + /// A view containing the child. It is required to simplify + /// any transformations on the child. + var childContainerView: UIView { get set } + /// A cropable content view. var childView: ChildView { get set } @@ -125,7 +129,8 @@ public extension Cropable where ChildView == UIImageView { cropView.showsVerticalScrollIndicator = false cropView.contentSize = view.bounds.size - cropView.addSubview(childView) + childContainerView.addSubview(childView) + cropView.addSubview(childContainerView) } /// @@ -150,10 +155,14 @@ public extension Cropable where ChildView == UIImageView { childView.image = image if adjustingContent { - childView.sizeToFit() + childView.bounds.size = image.size + childContainerView.bounds.size = image.size + + childContainerView.frame.origin = .zero childView.frame.origin = .zero + cropView.contentOffset = .zero - cropView.contentSize = childView.image!.size + cropView.contentSize = image.size updateContent() highlightArea(false, animated: false) @@ -185,8 +194,9 @@ public extension Cropable { /// Updates the current cropable content area, zoom and scale. /// func updateContent() { - let childViewSize = childView.bounds.size + let childViewSize = childContainerView.bounds.size let scrollViewSize = cropView.bounds.size + let widthScale = scrollViewSize.width / childViewSize.width let heightScale = scrollViewSize.height / childViewSize.height @@ -215,16 +225,16 @@ public extension Cropable { func centerContent(forcing: Bool = false) { var (left, top): (CGFloat, CGFloat) = (0, 0) - if cropView.contentSize.width < cropView.bounds.width { - left = (cropView.bounds.width - cropView.contentSize.width) / 2 + if cropView.contentSize.width < cropView.frame.width { + left = (cropView.frame.width - cropView.contentSize.width) / 2 } else if forcing { - cropView.contentOffset.x = abs(cropView.bounds.width - cropView.contentSize.width) / 2 + cropView.contentOffset.x = abs(cropView.frame.width - cropView.contentSize.width) / 2 } - if cropView.contentSize.height < cropView.bounds.height { - top = (cropView.bounds.height - cropView.contentSize.height) / 2 + if cropView.contentSize.height < cropView.frame.height { + top = (cropView.frame.height - cropView.contentSize.height) / 2 } else if forcing { - cropView.contentOffset.y = abs(cropView.bounds.height - cropView.contentSize.height) / 2 + cropView.contentOffset.y = abs(cropView.frame.height - cropView.contentSize.height) / 2 } cropView.contentInset = UIEdgeInsets(top: top, left: left, bottom: top, right: left) @@ -302,12 +312,12 @@ public extension Cropable { } linesView.frame.size = CGSize( - width: min(cropView.frame.width, childView.frame.width), - height: min(cropView.frame.height, childView.frame.height) + width: min(cropView.frame.width, childContainerView.frame.width), + height: min(cropView.frame.height, childContainerView.frame.height) ) let visibleRect = CGRect(origin: cropView.contentOffset, size: cropView.bounds.size) - let intersection = visibleRect.intersection(childView.frame) + let intersection = visibleRect.intersection(childContainerView.frame) linesView.frame = intersection } } diff --git a/Source/CropableScrollViewDelegate.swift b/Source/CropableScrollViewDelegate.swift index cefaccf..0be9a89 100644 --- a/Source/CropableScrollViewDelegate.swift +++ b/Source/CropableScrollViewDelegate.swift @@ -91,6 +91,6 @@ open class CropableScrollViewDelegate: NSObject, UIScrollViewDelega } open func viewForZooming(in scrollView: UIScrollView) -> UIView? { - return cropable.childView + return cropable.childContainerView } } diff --git a/Source/Radians.swift b/Source/Radians.swift new file mode 100644 index 0000000..4d823f0 --- /dev/null +++ b/Source/Radians.swift @@ -0,0 +1,21 @@ +// +// Radians.swift +// Pods +// +// Created by mac on 27/11/2016. +// +// + +import Foundation + +public extension Double { + public func toRadians() -> Double { + return self * .pi / 180.0 + } +} + +public extension Float { + public func toRadians() -> Float { + return self * .pi / 180 + } +}