CGAffineTransform+Utils.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435
  1. //
  2. // CGAffineTransform+Utils.swift
  3. // WeScan
  4. //
  5. // Created by Boris Emorine on 2/15/18.
  6. // Copyright © 2018 WeTransfer. All rights reserved.
  7. //
  8. import Foundation
  9. extension CGAffineTransform {
  10. /// Convenience function to easily get a scale `CGAffineTransform` instance.
  11. ///
  12. /// - Parameters:
  13. /// - fromSize: The size that needs to be transformed to fit (aspect fill) in the other given size.
  14. /// - toSize: The size that should be matched by the `fromSize` parameter.
  15. /// - Returns: The transform that will make the `fromSize` parameter fir (aspect fill) inside the `toSize` parameter.
  16. static func scaleTransform(forSize fromSize: CGSize, aspectFillInSize toSize: CGSize) -> CGAffineTransform {
  17. let scale = max(toSize.width / fromSize.width, toSize.height / fromSize.height)
  18. return CGAffineTransform(scaleX: scale, y: scale)
  19. }
  20. /// Convenience function to easily get a translate `CGAffineTransform` instance.
  21. ///
  22. /// - Parameters:
  23. /// - fromRect: The rect which center needs to be translated to the center of the other passed in rect.
  24. /// - toRect: The rect that should be matched.
  25. /// - Returns: The transform that will translate the center of the `fromRect` parameter to the center of the `toRect` parameter.
  26. static func translateTransform(fromCenterOfRect fromRect: CGRect, toCenterOfRect toRect: CGRect) -> CGAffineTransform {
  27. let translate = CGPoint(x: toRect.midX - fromRect.midX, y: toRect.midY - fromRect.midY)
  28. return CGAffineTransform(translationX: translate.x, y: translate.y)
  29. }
  30. }