Painted painted levels: fostered fostered fostered

Tags: “iOS” “Swift” “CATransform3D” “3D Transform


1. Commonly used 3D transformation methods

CATransform3DScale returns the transformation matrix constructed by scaling the existing transformation. Sx/SY /sz is the scaling ratio in the X, Y, and Z directions

CATransform3D CATransform3DScale (CATransform3D t, CGFloat sx,
    CGFloat sy, CGFloat sz)
Copy the code

CATransform3DRotate returns the transformation matrix constructed by rotating the existing transformation, with Angle representing radians and X,y, and z representing radian multiples of rotation on each axis

CATransform3D CATransform3DRotate (CATransform3D t, CGFloat angle,
    CGFloat x, CGFloat y, CGFloat z)
Copy the code

CATransform3DInvert returns the inverted transformation matrix

CATransform3D CATransform3DInvert (CATransform3D t)
Copy the code

CATransform3DTranslate returns a transformation matrix that translates the corresponding distance on the X/Y /z axis

CATransform3D CATransform3DTranslate (CATransform3D t, CGFloat tx,
    CGFloat ty, CGFloat tz)
Copy the code

CATransform3DConcat returns a matrix that acts on both transformation matrices

CATransform3D CATransform3DConcat (CATransform3D a, CATransform3D b)
Copy the code

Several special transformation matrix of CATransform3DMakeScale CATransform3DMakeRotation/CATransform3DMakeTranslation is also applied to the transformation matrix of the original view

/* Returns a transform that translates by '(tx, ty, tz)': * t' = [1 0 0 0; 0 1 0 0; 0 0 1 0;  tx ty tz 1]. */ CATransform3D CATransform3DMakeTranslation (CGFloat tx, CGFloat ty, CGFloat tz) /* Returns a transform that scales by `(sx, sy, sz)': * t' = [sx 0 0 0; 0 sy 0 0; 0 0 sz 0;  0 0 0 1]. */ CATransform3D CATransform3DMakeScale (CGFloat sx, CGFloat sy, CGFloat sz) /* Returns a transform that rotates by 'angle' radians about the vector * '(x, y, z)'. If the vector has length zero the identity transform is * returned. */ CATransform3D CATransform3DMakeRotation (CGFloat angle, CGFloat x, CGFloat y, CGFloat z) CATransform3DIdentity[1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1], human and animal harmless matrix, usually used to restore the initial stateCopy the code

2. Create a rotating 3D cube

import UIKit class CATransform3DController: UIViewController { var animateCube = UIView.init() override func viewDidLoad() { super.viewDidLoad() self.title = "Transform3D" self.view.backgroundColor = .white self.edgesForExtendedLayout = [UIRectEdge.left, UIRectEdge.right] self.testTransform3D() } func testTransform3D() { let targetRect = CGRect.init(x: 0, y: 0, width: 200, height: 200) animateCube.frame = targetRect animateCube.center = self.view.center self.view.addSubview(animateCube) let frontView = UIView.init(frame: TargetRect) frontView. BackgroundColor = UIColor. Blue. WithAlphaComponent frontView. (0.25) layer. The transform = CATransform3DTranslate(frontView.layer.transform, 0, 0, 100) self.animateCube.addSubview(frontView) let backView = UIView.init(frame: TargetRect) backView. BackgroundColor = UIColor. Black. WithAlphaComponent backView. (0.5) layer. The transform = CATransform3DTranslate(backView.layer.transform, 0, 0, -100) self.animateCube.addSubview(backView) let leftView = UIView.init(frame: TargetRect) leftView. BackgroundColor = UIColor. Yellow. WithAlphaComponent leftView. (0.5) layer. The transform = CATransform3DTranslate(leftView.layer.transform, -100, 0, 0) leftView. Layer. The transform = CATransform3DRotate (leftView. Layer. The transform, CGFloat (Double. PI / 2.0), 0, 1, 0) self.animateCube.addSubview(leftView) let rightView = UIView.init(frame: TargetRect) rightView. BackgroundColor = UIColor. Purple. WithAlphaComponent rightView. (0.5) layer. The transform = CATransform3DTranslate(rightView.layer.transform, 100, 0, 0) rightView. Layer. The transform = CATransform3DRotate (rightView. Layer. The transform, CGFloat (Double. PI / 2.0), 0, 1, 0) self.animateCube.addSubview(rightView) let headView = UIView.init(frame: TargetRect) headView. BackgroundColor = UIColor. Orange. WithAlphaComponent headView. (0.5) layer. The transform = CATransform3DTranslate(headView.layer.transform, 0, 100, 0) headView. Layer. The transform = CATransform3DRotate (headView. Layer. The transform, CGFloat (Double. PI / 2.0), 1, 0, 0) self.animateCube.addSubview(headView) let footView = UIView.init(frame: TargetRect) footView. BackgroundColor = UIColor. Green. WithAlphaComponent footView. (0.5) layer. The transform = CATransform3DTranslate(footView.layer.transform, 0, -100, 0) footView. Layer. The transform = CATransform3DRotate (footView. Layer. The transform, CGFloat (Double. PI / 2.0), 1, 0, 0) self.animateCube.addSubview(footView) self.animateCube.layer.borderColor = UIColor.red.cgColor Self. AnimateCube. Layer. BorderWidth = 2.0 / / self animateCube. Transform = CGAffineTransform (scaleX: 1.2, y: 1.2) / / var the transform3D: CATransform3D = CATransform3DIdentity. / / the transform3D m34 = - 1.0/500.0 / / self. AnimateCube. Layer. SublayerTransform = Weak var weakSelf = self let Angle = CGFloat(double-.pi) / -360.0 var transform3D: CATransform3D = CATransform3DIdentity let timer = timer.init (timeInterval: 1.0/60.0, repeats: > > repeats. True) {(_) in transform3D = CATransform3DRotate(transform3D, Angle, 1, 1, 0.5) weakSelf! .animateCube.layer.sublayerTransform = transform3D } RunLoop.current.add(timer, forMode: RunLoop.Mode.common) } }Copy the code

Example:



Recommended articles:

Today we will talk about WebSocket (iOS/Golang) using Swift for Bessel curve drawing Swift 5.1 (11) – method Swift 5.1 (10) – Properties iOS App background keepalive Dance weekly