3D是在原来的基础上修改z轴,实现投影、拉伸等3D喜爱效果,iOS提供了一个3D变幻矩阵(CATtransform3D),通过这个矩阵可实现各种透视、拉伸、移动和缩放效果。
一、相关概念
1、锚点,其指定了bounds相对于position的值,同时也是变化时的中心点,默认为(0.5,0.5),
2、矩阵变化的原理,CATransform3D实质上是定义了一个三维变换,源码如下
public init(m11: CGFloat(x缩放), m12: CGFloat(y切变), m13: CGFloat, m14: CGFloat, m21: CGFloat(x切变), m22: CGFloat(y缩放), m23: CGFloat, m24: CGFloat, m31: CGFloat, m32: CGFloat, m33: CGFloat, m34: CGFloat(透视), m41: CGFloat(x平移), m42: CGFloat(y平移), m43: CGFloat(z平移), m44: CGFloat)
二、3D动画效果
3D旋转效果:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(3)
//旋转
imgV!.layer.transform = CATransform3DMakeRotation(CGFloat(Double.pi/2), 1, 1, 1)
//缩放
// imgV!.layer.transform = CATransform3DMakeScale(0.1, 0.1, 0.1)
//平移
// imgV!.layer.transform = CATransform3DMakeTranslation(100, 100, 100)
UIView.commitAnimations()
}
三、Cover Flow 3D效果
import UIKit
class MyCoverFlowController: UIViewController {
var imageViewArr:NSMutableArray?
let imageV1:UIImageView = UIImageView(frame: CGRect(x: 100, y: 100, width: 200, height: 250))
let imageV2:UIImageView = UIImageView(frame: CGRect(x: 100, y: 100, width: 200, height: 250))
let imageV3:UIImageView = UIImageView(frame: CGRect(x: 100, y: 100, width: 200, height: 250))
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
imageViewArr = [imageV1,imageV2,imageV3];
for i in 0...(imageViewArr?.count)!-1 {
let imageV = imageViewArr?.object(at: i) as! UIImageView
let path:String = "\((i+1)*1000)"
imageV.image = UIImage(named: path)
imageV.layer.anchorPoint.y = 0.0
view.addSubview(imageV)
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
for i in 0...(imageViewArr?.count)!-1 {
var imageTransform:CATransform3D = CATransform3DIdentity
imageTransform.m34 = -0.005;
imageTransform = CATransform3DTranslate(imageTransform, 0.0, 150.0, 0.0)
imageTransform = CATransform3DScale(imageTransform, 0.95, 0.6, 1.0)
if i==0 {
imageTransform = CATransform3DRotate(imageTransform, CGFloat(Double.pi/8), 0.0, 1.0, 0.0)
}else if i==1 {
imageTransform = CATransform3DRotate(imageTransform, CGFloat(-Double.pi/8), 0.0, 1.0, 0.0)
}
let imageView:UIImageView = imageViewArr?.object(at: i) as! UIImageView
let animation = CABasicAnimation(keyPath: "transform")
animation.fromValue = NSValue(caTransform3D: imageView.layer.transform)
animation.toValue = NSValue(caTransform3D: imageTransform)
animation.duration = 3
/**************************/
let ani:CABasicAnimation = CABasicAnimation()
ani.keyPath = "position"
ani.duration = 3
if i==0{
ani.toValue = NSValue(cgPoint: CGPoint(x: 100, y: 10))
} else if i==1 {
ani.toValue = NSValue(cgPoint: CGPoint(x: 300, y: 10))
} else {
ani.toValue = NSValue(cgPoint: CGPoint(x: 200, y: 20))
}
let anG:CAAnimationGroup = CAAnimationGroup()
anG.duration = 3
anG.repeatCount = 1
anG.isRemovedOnCompletion = false
anG.fillMode = CAMediaTimingFillMode.forwards
anG.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
anG.animations = [animation,ani]
imageView.layer.add(anG, forKey: "\(i)")
}
}
}