文章目录:
平面旋转
顺时针旋转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度:
需要用到
/* Invert `t' and return the result. If `t' has zero determinant, then `t'
is returned unchanged. */
/* 对 调用该方法的 CGAffineTransform实例 进行取反 并返回 */
@available(iOS 2.0, *)
public func inverted() -> CGAffineTransform
事例代码
UIView.animate(withDuration: 1) {
self.captainLabel.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2).inverted()
}
持续旋转:
需要用到
/* 用来让 CGAffineTransform实例 关联我们指定的 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旋转:
需要用到
/* 对CATransform3D的实例进行变换(向向量(x, y, z)的方向 同时 以向量为轴 旋转 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)
}
持续旋转:
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)
}
}