練習がてらに。
X軸でフリップさせたい場合はreflcetionImageのCGContextScaleCTMとCGContextTranslateCTMをコメントアウトして、CATransform3DMakeRotationを変更すれば良い。
ViewController.swift
import UIKit
import QuartzCore
class ViewController: UIViewController {
var frontLayer = CALayer()
var backLayer = CALayer()
var front : Bool = true
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
frontLayer.frame = CGRectMake(100, 100, 100, 100)
frontLayer.contents = UIImage(named: "cover1.png").CGImage
frontLayer.doubleSided = false
frontLayer.zPosition = 1
var backImage = UIImage(named: "cover2.png")
backImage = reflectionImage(backImage)
backLayer.frame = CGRectMake(100, 100, 100, 100)
backLayer.contents = backImage.CGImage
backLayer.doubleSided = true
self.view.layer.addSublayer(frontLayer)
self.view.layer.addSublayer(backLayer)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
CATransaction.begin()
CATransaction.setValue(kCFBooleanFalse, forKeyPath: kCATransactionDisableActions)
CATransaction.setAnimationDuration(1.0)
self.front = !self.front
var transform : CATransform3D = CATransform3DIdentity
transform.m34 = 1.0 / -420.0
if(!self.front){
transform = CATransform3DMakeRotation(CGFloat(M_PI), 0.0, 1.0, 0.0)
} else {
transform = CATransform3DMakeRotation(0, 0.0, 1.0, 0.0)
}
self.frontLayer.transform = transform
self.backLayer.transform = transform
CATransaction.commit()
}
func reflectionImage (image: UIImage) -> UIImage {
UIGraphicsBeginImageContext(image.size)
var context = UIGraphicsGetCurrentContext()
CGContextScaleCTM(context, -1, -1);
CGContextTranslateCTM(context, -image.size.width, -image.size.height)
CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage)
var backImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return backImage
}
}