文章目录:
平面旋转
顺时针旋转90度
顺时针旋转180度
逆时针旋转90度
持续旋转
3D旋转
向量为(1, 0, 0)时
向量为(0, 1, 0)时
向量为(0, 1, 0)时
持续旋转
平面旋转:
顺时针旋转90度:
UIView.animate(withDuration: 1) {
self.captainLabel.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2)
}
顺时针旋转180度:
UIView.animate(withDuration: 1) {
self.captainLabel.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
}
逆时针旋转90度:
需要用到
@available(iOS 2.0, *)
public func inverted() -> CGAffineTransform
事例代码
UIView.animate(withDuration: 1) {
self.captainLabel.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2).inverted()
}
持续旋转:
需要用到
@available(iOS 2.0, *)
public func concatenating(_ t2: CGAffineTransform) -> CGAffineTransform
事例代码
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { (_) in
UIView.animate(withDuration: 1) {
self.captainLabel.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2).inverted().concatenating(self.captainLabel.transform)
}
}
3D旋转:
需要用到
@available(iOS 2.0, *)
public func CATransform3DRotate(_ t: CATransform3D, _ angle: CGFloat, _ x: CGFloat, _ y: CGFloat, _ z: CGFloat) -> CATransform3D
向量为(1, 0, 0)时:
UIView.animate(withDuration: 1) {
self.captainLabel.layer.transform = CATransform3DMakeRotation(CGFloat.pi, 1, 0, 0)
}
向量为(0, 1, 0)时:
UIView.animate(withDuration: 1) {
self.captainLabel.layer.transform = CATransform3DMakeRotation(CGFloat.pi, 0, 1, 0)
}
向量为(0, 0, 1)时:
UIView.animate(withDuration: 1) {
self.captainLabel.layer.transform = CATransform3DMakeRotation(CGFloat.pi, 0, 0, 1)
}
持续旋转:
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { (_) in
UIView.animate(withDuration: 1) {
self.captainLabel.layer.transform = CATransform3DRotate(self.captainLabel.layer.transform, CGFloat.pi / 2, 1, 0, 0)
}
}