From 77a26b57cac373644399de5603a3026ffa9e0cad Mon Sep 17 00:00:00 2001 From: Muukii Date: Mon, 28 Mar 2022 04:13:47 +0900 Subject: [PATCH] :zap: WIP --- Sources/CGPoint+GeometryKit.swift | 8 ++++ Sources/CGRect+GeometryKit.swift | 26 ++++++++++++ Sources/CGSize+GeometryKit.swift | 67 +++++++++++++++++++++++++++++ Sources/GeometryKit.swift | 70 ++++++------------------------- 4 files changed, 113 insertions(+), 58 deletions(-) create mode 100644 Sources/CGPoint+GeometryKit.swift create mode 100644 Sources/CGRect+GeometryKit.swift create mode 100644 Sources/CGSize+GeometryKit.swift diff --git a/Sources/CGPoint+GeometryKit.swift b/Sources/CGPoint+GeometryKit.swift new file mode 100644 index 0000000..5a5ad55 --- /dev/null +++ b/Sources/CGPoint+GeometryKit.swift @@ -0,0 +1,8 @@ +// +// File.swift +// +// +// Created by Muukii on 2022/03/28. +// + +import Foundation diff --git a/Sources/CGRect+GeometryKit.swift b/Sources/CGRect+GeometryKit.swift new file mode 100644 index 0000000..bd7936f --- /dev/null +++ b/Sources/CGRect+GeometryKit.swift @@ -0,0 +1,26 @@ +import CoreGraphics.CGGeometry + +extension CGRect { + + /// Returns a rectangle that fits inside provided bounding rectangle with respecting aspect ratio. + public func gk_makeRectThatAspectFit( + aspectRatio: CGSize + ) -> CGRect { + + let size = aspectRatio.gk_sizeThatAspectFit(in: self.size) + var origin = origin + origin.x += (size.width - size.width) / 2.0 + origin.y += (size.height - size.height) / 2.0 + return CGRect(origin: origin, size: size) + } + + public func gk_makeRectThatAspectFill(aspectRatio: CGSize) -> CGRect { + + let minimumRect = self + let size = aspectRatio.gk_sizeThatAspectFill(minimumSize: minimumRect.size) + var origin = CGPoint.zero + origin.x = (minimumRect.size.width - size.width) / 2.0 + origin.y = (minimumRect.size.height - size.height) / 2.0 + return CGRect(origin: origin, size: size) + } +} diff --git a/Sources/CGSize+GeometryKit.swift b/Sources/CGSize+GeometryKit.swift new file mode 100644 index 0000000..d8dcabc --- /dev/null +++ b/Sources/CGSize+GeometryKit.swift @@ -0,0 +1,67 @@ +import CoreGraphics.CGGeometry + +extension CGSize { + + public func gk_sizeThatAspectFit(maxSize: CGFloat) -> CGSize { + + guard width >= maxSize || height >= maxSize else { + return self + } + + var s = self + + if width > height { + s.width = maxSize + s.height *= maxSize / width + } else { + s.height = maxSize + s.width *= maxSize / height + } + + s.width.round() + s.height.round() + + return s + } + + public func gk_sizeThatAspectFit(in boundingSize: CGSize) -> CGSize { + + let aspectRatio = self + + let widthRatio = boundingSize.width / aspectRatio.width + let heightRatio = boundingSize.height / aspectRatio.height + var size = boundingSize + + if widthRatio < heightRatio { + size.height = boundingSize.width / aspectRatio.width * aspectRatio.height + } else if heightRatio < widthRatio { + size.width = boundingSize.height / aspectRatio.height * aspectRatio.width + } + + return CGSize( + width: ceil(size.width), + height: ceil(size.height) + ) + } + + public func gk_sizeThatAspectFill(minimumSize: CGSize) -> CGSize { + + let aspectRatio = self + + let widthRatio = minimumSize.width / aspectRatio.width + let heightRatio = minimumSize.height / aspectRatio.height + + var size = minimumSize + + if widthRatio > heightRatio { + size.height = minimumSize.width / aspectRatio.width * aspectRatio.height + } else if heightRatio > widthRatio { + size.width = minimumSize.height / aspectRatio.height * aspectRatio.width + } + + return CGSize( + width: ceil(size.width), + height: ceil(size.height) + ) + } +} diff --git a/Sources/GeometryKit.swift b/Sources/GeometryKit.swift index 2bb866d..b93d12b 100644 --- a/Sources/GeometryKit.swift +++ b/Sources/GeometryKit.swift @@ -6,79 +6,33 @@ public enum Geometry { extension Geometry { - public static func sizeThatAspectFit(size: CGSize, maxPixelSize: CGFloat) -> CGSize { + public static func sizeThatAspectFit( + size: CGSize, + maxPixelSize: CGFloat + ) -> CGSize { - guard size.width >= maxPixelSize || size.height >= maxPixelSize else { - return size - } - - var s = size - - if size.width > size.height { - s.width = maxPixelSize - s.height *= maxPixelSize / size.width - } else { - s.height = maxPixelSize - s.width *= maxPixelSize / size.height - } - - s.width.round() - s.height.round() - - return s + size.gk_sizeThatAspectFit(maxSize: maxPixelSize) } public static func sizeThatAspectFit(aspectRatio: CGSize, boundingSize: CGSize) -> CGSize { - let widthRatio = boundingSize.width / aspectRatio.width - let heightRatio = boundingSize.height / aspectRatio.height - var size = boundingSize - - if widthRatio < heightRatio { - size.height = boundingSize.width / aspectRatio.width * aspectRatio.height - } else if heightRatio < widthRatio { - size.width = boundingSize.height / aspectRatio.height * aspectRatio.width - } - - return CGSize( - width: ceil(size.width), - height: ceil(size.height) - ) + + aspectRatio.gk_sizeThatAspectFit(in: boundingSize) } public static func sizeThatAspectFill(aspectRatio: CGSize, minimumSize: CGSize) -> CGSize { - let widthRatio = minimumSize.width / aspectRatio.width - let heightRatio = minimumSize.height / aspectRatio.height - - var size = minimumSize - - if widthRatio > heightRatio { - size.height = minimumSize.width / aspectRatio.width * aspectRatio.height - } else if heightRatio > widthRatio { - size.width = minimumSize.height / aspectRatio.height * aspectRatio.width - } - - return CGSize( - width: ceil(size.width), - height: ceil(size.height) - ) + + aspectRatio.gk_sizeThatAspectFill(minimumSize: minimumSize) } /// Returns a rectangle that fits inside provided bounding rectangle with respecting aspect ratio. public static func rectThatAspectFit(aspectRatio: CGSize, boundingRect: CGRect) -> CGRect { - let size = sizeThatAspectFit(aspectRatio: aspectRatio, boundingSize: boundingRect.size) - var origin = boundingRect.origin - origin.x += (boundingRect.size.width - size.width) / 2.0 - origin.y += (boundingRect.size.height - size.height) / 2.0 - return CGRect(origin: origin, size: size) + + boundingRect.gk_makeRectThatAspectFit(aspectRatio: aspectRatio) } /// Returns a rectangle that fills inside provided bounding rectangle with respecting aspect ratio. public static func rectThatAspectFill(aspectRatio: CGSize, minimumRect: CGRect) -> CGRect { - let size = sizeThatAspectFill(aspectRatio: aspectRatio, minimumSize: minimumRect.size) - var origin = CGPoint.zero - origin.x = (minimumRect.size.width - size.width) / 2.0 - origin.y = (minimumRect.size.height - size.height) / 2.0 - return CGRect(origin: origin, size: size) + minimumRect.gk_makeRectThatAspectFill(aspectRatio: aspectRatio) } public static func diagonalRatio(to: CGSize, from: CGSize) -> CGFloat {