Skip to content

Instantly share code, notes, and snippets.

@NinoScript
Created March 8, 2018 15:37
Show Gist options
  • Save NinoScript/a2c31192a1d72e41cad8a3db0f3f638d to your computer and use it in GitHub Desktop.
Save NinoScript/a2c31192a1d72e41cad8a3db0f3f638d to your computer and use it in GitHub Desktop.
Rotate UIView with anchorPoint
//: Playground - noun: a place where people can play
import UIKit
let r = UIView(frame: CGRect(
x: 0,
y: 0,
width: 200,
height: 200
))
let posX: CGFloat = 100
let posY: CGFloat = 80
let size: CGFloat = 40
func makeView(color: UIColor) -> UIView {
let frame = CGRect(
x: posX,
y: posY,
width: size,
height: size
)
let view = UIView(frame: frame)
r.addSubview(view)
view.backgroundColor = color
return view
}
let fondo = makeView(color: .lightGray)
let azul = makeView(color: .blue)
let rojo = makeView(color: .red)
// El anchorPoint también es desde donde se dibuja, así que vamos a cambiarle el center para que calce con el original
azul.center = azul.frame.origin
azul.layer.anchorPoint = .zero
import PlaygroundSupport
PlaygroundPage.current.liveView = r
UIView.animate(
withDuration: 2.0,
delay: 0.0,
options: [.repeat, .autoreverse],
animations: ({
azul.transform = azul.transform.rotated(by: CGFloat.pi)
rojo.transform = rojo.transform.rotated(by: CGFloat.pi)
}),
completion: nil
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment