10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swiftでフリップアニメーション

Posted at

練習がてらに。

iphonePlay.gif

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
    }

}

参考

フリップアニメーションでビューを切り替える

CALayerでFlip Animation

10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?