Skip to content

Instantly share code, notes, and snippets.

@wtsnz
Created June 12, 2019 23:45
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wtsnz/f14575f4765568d2824ff350f521d1b3 to your computer and use it in GitHub Desktop.
Save wtsnz/f14575f4765568d2824ff350f521d1b3 to your computer and use it in GitHub Desktop.
Scale transform around anchor point
func scaleTransform(for view: UIView, scaledBy scale: CGPoint, aroundAnchorPoint relativeAnchorPoint: CGPoint) -> CGAffineTransform {
let bounds = view.bounds
let anchorPoint = CGPoint(x: bounds.width * relativeAnchorPoint.x, y: bounds.height * relativeAnchorPoint.y)
return CGAffineTransform.identity
.translatedBy(x: anchorPoint.x, y: anchorPoint.y)
.scaledBy(x: scale.x, y: scale.y)
.translatedBy(x: -anchorPoint.x, y: -anchorPoint.y)
}
@Wilsonilo
Copy link

Thanks for this!
Interesting using translated x 2 to regulate, is this your own inspiration?

@wtsnz
Copy link
Author

wtsnz commented Aug 9, 2023

@Wilsonilo That is a very good question... 4 years ago was a long time and I was deep in the context of graphics then so I can't be sure how I came up with it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment