Skip to content

Instantly share code, notes, and snippets.

@hisoka0917
Created July 13, 2018 06:57
Show Gist options
  • Save hisoka0917/30e8f59014284b49f793cfa8295f8df7 to your computer and use it in GitHub Desktop.
Save hisoka0917/30e8f59014284b49f793cfa8295f8df7 to your computer and use it in GitHub Desktop.
Change UIView anchor point in Swift
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