Skip to content

Instantly share code, notes, and snippets.

@brocoo
Last active March 4, 2025 11:23
Show Gist options
  • Save brocoo/47e06b5b34602c67b3bb to your computer and use it in GitHub Desktop.
Save brocoo/47e06b5b34602c67b3bb to your computer and use it in GitHub Desktop.
Flip UIView around any axis
extension CGFloat {
var toRadians: CGFloat {
return self * CGFloat(M_PI) / 180.0
}
var toDegress: CGFloat {
return self * 180 / CGFloat(M_PI)
}
}
public enum Axis {
case X
case Y
case Z
}
extension UIView {
func flip(aroundAxis axis: Axis, byDegrees degrees: CGFloat) {
let coordinates: (x: CGFloat, y: CGFloat, z: CGFloat) = {
switch axis {
case .X: return (1, 0, 0)
case .Y: return (0, 1, 0)
case .Z: return (0, 0, 1)
}
}()
self.layer.transform = CATransform3DMakeRotation(degrees.toRadians, coordinates.x, coordinates.y, coordinates.z)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment