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


フリップアニメーションで写真の切り替えを行う。
・・・本当はもっと滑らかに動かす予定だったんだけどここらで挫折。
せめて偶数と奇数で動きのタイミングで変えてみたけど、小手先かなあ。
ちなみに写真は平安神宮前での実物大パトレイバーが来たときのものです。

フリップアニメーションはここらで一旦止めて次回から別のものに挑戦

![iphonePlay3.gif](https://qiita-image-store.s3.amazonaws.com/0/40693/80713f09-695c-6463-1b0c-59ee8de4455f.gif "iphonePlay3.gif")


```ViewController.swift

import UIKit
import QuartzCore

class ParaparaLayer : CALayer {
    var panelNumber : Int = 0
}

class ViewController: UIViewController {
    var front : Bool = true
    var touch : Bool  = false
    var myView : UIView = UIView()
    var layerKaisou : CGFloat = 0
    let photos : [String] = ["IMG_0695.jpg", "IMG_0704.jpg", "IMG_0720.jpg", "IMG_0723.jpg"]
    var index : Int = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        myView = UIView(frame: self.view.frame)
        front = !front
        self.addImage(photos[index])
        front = !front
        self.addImage(photos[++index])
        self.view.addSubview(myView)
        
      }
    
    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)
        
        if(touch){
            return
        }
        
        touch = true
        
        self.front = !self.front
        var transform : CATransform3D = CATransform3DIdentity
        transform.m34 = 1.0 / -420.0
        
        transform = CATransform3DMakeRotation(CGFloat(M_PI),  0.0, 1.0, 0.0)
        
        var rl : CFRunLoopRef = CFRunLoopGetCurrent()
        let distantFuture = NSDate.distantFuture() as? NSDate
        var timer : CFRunLoopTimerRef = CFRunLoopTimerCreate(nil, CFDateGetAbsoluteTime(distantFuture),0,0,0, nil, nil)
        
        CFRunLoopAddTimer(rl, timer, kCFRunLoopDefaultMode)
        
        var name : String
        if(front){
            name = "front"
        } else {
            name = "back"
        }
        
        
        let ary = myView.layer.sublayers
        if(ary.count > 0){
        
            for (var v : CGFloat = 0; v < 4; v++) {
                for (var h : CGFloat = 0; h < 4; h++) {
                    for(var i : Int = 0; i < ary.count; i++) {
                        var tile : ParaparaLayer = ary[i] as ParaparaLayer
                        
                        if((v%2 == 0 && tile.panelNumber%2 == 0)
                            || (v%2 == 1 && tile.panelNumber%2 == 1)) {
                                if(tile.name != name){
                                    CATransaction.begin()
                                    CATransaction.setValue(kCFBooleanFalse, forKeyPath: kCATransactionDisableActions)
                                    CATransaction.setCompletionBlock{
                                        CFRunLoopStop(rl)
                                    }
                                    
                                    CATransaction.setAnimationDuration(0.7)
                                    tile.transform = transform
                                    
                                    CATransaction.commit()
                                    CFRunLoopRun()
                                    
                                }
                        }
                    }
                }
            }
        }

        ++index
        if(index >= photos.count){
            index  = 0
        }
        self.addImage(photos[index])
        self.touch = false        
 
    }
       
    func addImage(photo : String) {
        var r : CGRect = CGRectZero;
        let image = UIImage(named: photo)
        var divCount : CGFloat = 4.0
        
        r.size.width = myView.bounds.size.width/divCount
        r.size.height = myView.bounds.size.height/divCount

        var name : String
        if(!front){
            name = "front"
        } else {
            name = "back"
        }
        
        var kisu :Int = 1
        var gusu :Int = 0
        for (var v : CGFloat = 0; v < divCount; v++) {
            for (var h : CGFloat = 0; h < divCount; h++) {
                var tile = ParaparaLayer()
                if (v%2) == 0 {
                    gusu = gusu + 2
                    tile.panelNumber = gusu
                } else {
                    kisu = kisu + 2
                    tile.panelNumber = kisu
                }
                tile.contents = image.CGImage
                tile.frame = r;
                
                //描画領域指定
                tile.contentsRect = CGRectMake(h/divCount, v/divCount, (CGFloat)(1.0/divCount), (CGFloat)(1.0/divCount))
                tile.doubleSided = false
                tile.zPosition = layerKaisou
                tile.name = name
                myView.layer.addSublayer(tile)
                r = CGRectOffset(r, r.size.width, 0);   //  次の位置へ
            }
            //  矩形を左端に戻し、縦位置を一段ずらず
            r.origin.x = myView.bounds.origin.x;
            r = CGRectOffset(r, 0, r.size.height);
        }
        
        layerKaisou = layerKaisou - 1
    }
    
}

```

####今回も作るに当たって参考にさせて貰ったページ


[CFRunLoopによるCore Animationの逐次的アニメーション](http://qiita.com/icecocoa6/items/6d5c023ada5e30eb209c)