(iOS, Swift)iOS控件 平面旋轉(zhuǎn) 和 3D旋轉(zhuǎn)

文章目錄:

平面旋轉(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)
            }
        }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容