文章目錄:
平面旋轉(zhuǎn)
順時針旋轉(zhuǎn)90度
順時針旋轉(zhuǎn)180度
逆時針旋轉(zhuǎn)90度
持續(xù)旋轉(zhuǎn)
3D旋轉(zhuǎn)
向量為(1, 0, 0)時
向量為(0, 1, 0)時
向量為(0, 1, 0)時
持續(xù)旋轉(zhuǎn)
平面旋轉(zhuǎn):
順時針旋轉(zhuǎn)90度:

UIView.animate(withDuration: 1) {
self.captainLabel.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2)
}
順時針旋轉(zhuǎn)180度:

UIView.animate(withDuration: 1) {
self.captainLabel.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
}
逆時針旋轉(zhuǎn)90度:

需要用到
/* Invert `t' and return the result. If `t' has zero determinant, then `t'
is returned unchanged. */
/* 對 調(diào)用該方法的 CGAffineTransform實例 進行取反 并返回 */
@available(iOS 2.0, *)
public func inverted() -> CGAffineTransform
事例代碼
UIView.animate(withDuration: 1) {
self.captainLabel.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2).inverted()
}
持續(xù)旋轉(zhuǎn):

需要用到
/* 用來讓 CGAffineTransform實例 關(guān)聯(lián)我們指定的 CGAffineTransform 實例 */
/* Concatenate `t2' to `t1' and return the result:
t' = t1 * t2 */
@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旋轉(zhuǎn):
需要用到
/* 對CATransform3D的實例進行變換(向向量(x, y, z)的方向 同時 以向量為軸 旋轉(zhuǎn) angle 角度)并返回新的CATransform3D實例。*/
/* Rotate 't' by 'angle' radians about the vector '(x, y, z)' and return
* the result. If the vector has zero length the behavior is undefined:
* t' = rotation(angle, x, y, z) * t. */
@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)
}
持續(xù)旋轉(zhuǎn):

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)
}
}