Created
July 13, 2018 06:57
-
-
Save hisoka0917/30e8f59014284b49f793cfa8295f8df7 to your computer and use it in GitHub Desktop.
Change UIView anchor point in Swift
This file contains 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 UIView { | |
func setAnchorPoint(_ point: CGPoint) { | |
var newPoint = CGPoint(x: bounds.size.width * point.x, y: bounds.size.height * point.y) | |
var oldPoint = CGPoint(x: bounds.size.width * layer.anchorPoint.x, y: bounds.size.height * layer.anchorPoint.y) | |
newPoint = newPoint.applying(transform) | |
oldPoint = oldPoint.applying(transform) | |
var position = layer.position | |
position.x -= oldPoint.x | |
position.x += newPoint.x | |
position.y -= oldPoint.y | |
position.y += newPoint.y | |
layer.position = position | |
layer.anchorPoint = point | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment