Created
May 11, 2016 08:10
-
-
Save westerlund/2c07b9559a8f2bbf1692b0dcc7d4aea8 to your computer and use it in GitHub Desktop.
CGRect + resize keeping aspect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension CGRect { | |
func resized(size: CGSize, aspectFill: Bool) -> CGRect { | |
var resizedRect = CGRect.zero; | |
let aspectWidth = size.width / self.size.width | |
let aspectHeight = size.height / self.size.height | |
let aspectRatio = aspectFill ? max(aspectWidth, aspectHeight) : min(aspectWidth, aspectHeight) | |
resizedRect.size.width = self.size.width * aspectRatio | |
resizedRect.size.height = self.size.height * aspectRatio | |
resizedRect.origin.x = (size.width - resizedRect.size.width) / 2 | |
resizedRect.origin.y = (size.height - resizedRect.size.height) / 2 | |
return resizedRect | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment