标签:
首先说明:这是一系列文章,参考本专题下其他的文章有助于你对本文的理解。
今天周六,希望大家都有一个愉快的周末。
今天来讲解一下CATransformLayer:CATransformLayer是一个专门用来创建三维视图的一个layer,也可以说是多个layer的集合。他没有多余的API,可以这么说,他只是承载了子layer。
下面就看一个例子,通过例子来讲解。
国际惯例先上图:
图就是这样的纯手工打造。
先创建一个CATransformLayer对象:
var transformLayer = CATransformLayer()
transformLayer.frame = someView.bounds
someView.layer.addSublayer(transformLayer)
这个没有任何技术含量。同样someView为我用storyboard创建的一个view
然后创建他的子layer,将layer添加到transformLayer上
var layer = sideLayerWithColor(redColor)
transformLayer.addSublayer(layer)
layer = sideLayerWithColor(orangeColor)
var transform = CATransform3DMakeTranslation(sideLength / 2, 0.0, sideLength / -2)
transform = CATransform3DRotate(transform, degreesToRadians(90.0), 0.0, 1.0, 0.0)
layer.transform = transform
transformLayer.addSublayer(layer)
layer = sideLayerWithColor(yellowColor)
layer.transform = CATransform3DMakeTranslation(0.0, 0.0, -sideLength)
transformLayer.addSublayer(layer)
layer = sideLayerWithColor(greenColor)
transform = CATransform3DMakeTranslation(sideLength / -2, 0.0, sideLength / -2)
transform = CATransform3DRotate(transform, degreesToRadians(90.0), 0.0, 1.0, 0.0)
layer.transform = transform
transformLayer.addSublayer(layer)
layer = sideLayerWithColor(blueColor)
transform = CATransform3DTranslate(transform, 0.0, sideLength / -2, sideLength / 2)
transform = CATransform3DRotate(transform, degreesToRadians(90.0), 1.0, 0.0, 0.0)
layer.transform = transform
transformLayer.addSublayer(layer)
layer = sideLayerWithColor(purpleColor)
transform = CATransform3DMakeTranslation(0.0, sideLength / 2.0, sideLength / -2.0)
transform = CATransform3DRotate(transform, degreesToRadians(90.0), 1.0, 0.0, 0.0)
layer.transform = transform
transformLayer.addSublayer(layer)
transformLayer.anchorPointZ = sideLength / -2.0
func sideLayerWithColor(color: UIColor) -> CALayer {
let layer = CALayer()
layer.frame = CGRect(origin: CGPointZero, size: CGSize(width: sideLength, height: sideLength))
layer.backgroundColor = color.CGColor
return layer
}
还有一个:degreesToRadians(),这个是转换角度,看上去更方便也更顺眼。
func degreesToRadians(degrees: Double) -> CGFloat {
return CGFloat(degrees * M_PI / 180.0)
}
括号内的参数分别是我定义的颜色来区分每个面的颜色不同
var redColor = UIColor.redColor()
var orangeColor = UIColor.orangeColor()
var yellowColor = UIColor.yellowColor()
var greenColor = UIColor.grayColor()
var purpleColor = UIColor.purpleColor()
var blueColor = UIColor.blueColor()
最后一行设置锚点,将锚点设置为立方体的中心
现在一个立方体已经做好了,但是运行之后看到的仍然是平面,因为这个立方体是正对着屏幕,其他面都被遮挡了。
下面就写个方法让他旋转角度
func applyRoationForXOffset(xOffset: Double, yOffset: Double){
let totalOffset = sqrt(xOffset * xOffset + yOffset * yOffset)
let totalRotation = CGFloat(totalOffset * M_PI / 180.0)
let xRotationalFactor = CGFloat(xOffset) / totalRotation
let yRotationalFactor = CGFloat(yOffset) / totalRotation
let currentTransform = CATransform3DTranslate(transformLayer.sublayerTransform, 0.0, 0.0, 0.0)
let rotationTransform = CATransform3DRotate(transformLayer.sublayerTransform, totalRotation, xRotationalFactor * currentTransform.m12 - yRotationalFactor * currentTransform.m11, xRotationalFactor * currentTransform.m22 - yRotationalFactor * currentTransform.m21, xRotationalFactor * currentTransform.m32 - yRotationalFactor * currentTransform.m31)
print(currentTransform.m12, currentTransform.m11)
transformLayer.sublayerTransform = rotationTransform
}
给参数设定值之后就可以看到一个有立体效果的立方体了,
下面就开始让这个立方体动起来,
手势的操作,当然少不了touchBegan和touchMoved了。
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch: UITouch = touches.first!
startPoint = touch.locationInView(someView)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch: UITouch = touches.first!
let currentPoint = touch.locationInView(someView)
let deltaX = currentPoint.x - startPoint.x
let deltaY = currentPoint.y - startPoint.y
applyRoationForXOffset(Double(deltaX), yOffset: Double(deltaY))
startPoint = touch.locationInView(someView)
}
iOS 动画Animation-4-5: CALayer子类:CATransformLayer
标签:
原文地址:http://blog.csdn.net/fish_yan_/article/details/51168027