2017年5月18日 | Leave a comment 解决: 实现效果 import UIKit class ViewController: UIViewController { let width : Float = 100 var width_2 : Float { get{ return width / 2.0 } } override func viewDidLoad() { super.viewDidLoad() view.translatesAutoresizingMaskIntoConstraints = false // Do any additional setup after loading the view, typically from a nib. var t = CATransform3DIdentity.rotate(-Float.pi/4.0, 1, 0, 0).rotate(-Float.pi/4.0, 0, 1, 0) //.translate(200, 500, 0) //.rotate(Float.pi/4, 1, 0, 0) t.m34 = -1.0 / 500.0 view.layer.sublayerTransform = t let b0 = btn(i:0 , transform: CATransform3DIdentity.translate(0, 0, width_2), bgColor: UIColor.green) view.addSubview(b0) let b1 = btn(i:1 , transform: CATransform3DIdentity.translate(width_2, 0, 0).rotate(Float.pi/2, 0, 1, 0), bgColor: UIColor.blue) view.addSubview(b1) let b2 = btn(i:2 , transform: CATransform3DIdentity.translate(0, -width_2, 0).rotate(Float.pi/2, 1, 0, 0), bgColor: UIColor.orange) view.addSubview(b2) // let b3 = btn(i:3 , transform: CATransform3DIdentity.translate(0, width, 0).rotate(Float.pi/2, 1, 0, 0)) // view.addSubview(b3) // let b4 = btn(i:4 , transform: CATransform3DIdentity.translate(0, width, 0).rotate(Float.pi/2, 1, 0, 0)) // view.addSubview(b4) // let b5 = btn(i:5 , transform: CATransform3DIdentity.translate(0, width, 0).rotate(Float.pi/2, 1, 0, 0)) // view.addSubview(b5) } private func btn(i : Int , transform : CATransform3D, bgColor: UIColor) -> UIButton { let ret = UIButton(type: .custom) ret.setTitle("\(i)", for: .normal) ret.backgroundColor = bgColor ret.frame = CGRect(x: 0, y: 0, width: Int(width), height: Int(width)) ret.center = view.bounds.center ret.layer.transform = transform return ret } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } extension CATransform3D { func translate(_ tx:Float,_ ty:Float,_ tz:Float) -> CATransform3D{ let ret = CATransform3DTranslate(self, CGFloat(tx), CGFloat(ty), CGFloat(tz)) return ret } func rotate(_ angle:Float , _ x:Float,_ y:Float,_ z:Float) -> CATransform3D{ let ret = CATransform3DRotate(self, CGFloat(angle), CGFloat(x), CGFloat(y), CGFloat(z)) return ret } } extension CGRect { var center : CGPoint { get{ let x = origin.x + (size.width / 2.0) let y = origin.y + (size.height / 2.0) return CGPoint(x: x , y:y) } } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 import UIKit class ViewController: UIViewController { let width : Float = 100 var width_2 : Float { get{ return width / 2.0 } } override func viewDidLoad() { super.viewDidLoad() view.translatesAutoresizingMaskIntoConstraints = false // Do any additional setup after loading the view, typically from a nib. var t = CATransform3DIdentity.rotate(-Float.pi/4.0, 1, 0, 0).rotate(-Float.pi/4.0, 0, 1, 0) //.translate(200, 500, 0) //.rotate(Float.pi/4, 1, 0, 0) t.m34 = -1.0 / 500.0 view.layer.sublayerTransform = t let b0 = btn(i:0 , transform: CATransform3DIdentity.translate(0, 0, width_2), bgColor: UIColor.green) view.addSubview(b0) let b1 = btn(i:1 , transform: CATransform3DIdentity.translate(width_2, 0, 0).rotate(Float.pi/2, 0, 1, 0), bgColor: UIColor.blue) view.addSubview(b1) let b2 = btn(i:2 , transform: CATransform3DIdentity.translate(0, -width_2, 0).rotate(Float.pi/2, 1, 0, 0), bgColor: UIColor.orange) view.addSubview(b2)// let b3 = btn(i:3 , transform: CATransform3DIdentity.translate(0, width, 0).rotate(Float.pi/2, 1, 0, 0))// view.addSubview(b3)// let b4 = btn(i:4 , transform: CATransform3DIdentity.translate(0, width, 0).rotate(Float.pi/2, 1, 0, 0))// view.addSubview(b4)// let b5 = btn(i:5 , transform: CATransform3DIdentity.translate(0, width, 0).rotate(Float.pi/2, 1, 0, 0))// view.addSubview(b5) } private func btn(i : Int , transform : CATransform3D, bgColor: UIColor) -> UIButton { let ret = UIButton(type: .custom) ret.setTitle("\(i)", for: .normal) ret.backgroundColor = bgColor ret.frame = CGRect(x: 0, y: 0, width: Int(width), height: Int(width)) ret.center = view.bounds.center ret.layer.transform = transform return ret } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } extension CATransform3D { func translate(_ tx:Float,_ ty:Float,_ tz:Float) -> CATransform3D{ let ret = CATransform3DTranslate(self, CGFloat(tx), CGFloat(ty), CGFloat(tz)) return ret } func rotate(_ angle:Float , _ x:Float,_ y:Float,_ z:Float) -> CATransform3D{ let ret = CATransform3DRotate(self, CGFloat(angle), CGFloat(x), CGFloat(y), CGFloat(z)) return ret }} extension CGRect { var center : CGPoint { get{ let x = origin.x + (size.width / 2.0) let y = origin.y + (size.height / 2.0) return CGPoint(x: x , y:y) } }} width_2 是因为retina ,没太搞懂 参考: 《ios 核心动画高级技巧》