Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active August 2, 2016 23:52
Show Gist options
  • Save KentarouKanno/407c11bfd4d2cc6e80c95ee79d711c29 to your computer and use it in GitHub Desktop.
Save KentarouKanno/407c11bfd4d2cc6e80c95ee79d711c29 to your computer and use it in GitHub Desktop.
UIView Animation

UIView Animation

★ TransitionFlipFromLeftを自前で実現

import UIKit

class ViewController: UIViewController {
    
    let viewHead = UIView()
    let viewTail = UIView()
    var transform = CATransform3DIdentity
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.whiteColor()
        
        //カメラとの距離
        transform.m34 = 1/1000 //逆数で指定する(要調整)
        
        //表側のview
        viewHead.frame = self.view.bounds
        viewHead.backgroundColor = UIColor.greenColor()
        viewHead.layer.doubleSided = false //裏面なし
        self.view.addSubview(viewHead)
        
        //裏側のview
        viewTail.frame = self.view.bounds
        viewTail.backgroundColor = UIColor.orangeColor()
        viewTail.layer.doubleSided = false //裏面無し
        viewTail.layer.transform = CATransform3DRotate(transform, CGFloat(-M_PI), 0, 1, 0) //最初は裏面
        self.view.addSubview(viewTail)
    }
    
    override func viewDidAppear(animated: Bool) {
        UIView.animateWithDuration(2.8, animations: {
            //裏返す
            self.viewHead.layer.transform = CATransform3DRotate(self.transform, CGFloat(M_PI), 0, 1, 0)
            //表向ける
            self.viewTail.layer.transform = CATransform3DRotate(self.transform, 0, 0, 1, 0)
        })
    }
}

★ タップしたところに波紋を出しアニメーションさせる

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    struct Option {
        var borderWidth = CGFloat(1.0)
        var radius = CGFloat(30.0)
        var duration = CFTimeInterval(0.4)
        var borderColor = UIColor ( red: 1.0, green: 0.0944, blue: 0.0804, alpha: 1.0 )
        var fillColor = UIColor.clearColor()
        var scale = CGFloat(2.0)
        var isRunSuperView = true
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        super.touchesBegan(touches, withEvent: event)
        
        if let touch = touches.first {
            let point = touch.locationInView(self.view)
            

            let option = Option()
            
            for i in 0...1 {
                
                UIGraphicsBeginImageContextWithOptions (
                    CGSizeMake((option.radius + option.borderWidth) * 2, (option.radius + option.borderWidth) * 2), false, 3.0)
                let path = UIBezierPath(
                    roundedRect: CGRectMake(option.borderWidth, option.borderWidth, option.radius * 2, option.radius * 2),
                    cornerRadius: option.radius)
                option.fillColor.setFill()
                path.fill()
                option.borderColor.setStroke()
                path.lineWidth = option.borderWidth
                path.stroke()
                let img = UIGraphicsGetImageFromCurrentImageContext() as UIImage
                UIGraphicsEndImageContext()
                
                
                let layer = CAShapeLayer()
                layer.contents = img.CGImage
                layer.frame = CGRectMake(point.x - option.radius, point.y - option.radius, option.radius * 2, option.radius * 2)
                
                self.view.layer.addSublayer(layer)
                
                CATransaction.begin()
                CATransaction.setAnimationDuration(5.0)
                CATransaction.setCompletionBlock({
                    layer.removeFromSuperlayer()
                })
                
                
                let animation : CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")
                animation.delegate = self
                animation.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseOut)
                
                animation.duration = 2
                animation.removedOnCompletion = false
                animation.fillMode = kCAFillModeForwards
                animation.toValue = NSNumber(double: 2.0)
                
                
                let animation2 : CABasicAnimation = CABasicAnimation(keyPath: "opacity")
                animation2.delegate = self
                animation2.duration = 2
                animation2.removedOnCompletion = false
                animation2.fillMode = kCAFillModeForwards
                animation2.fromValue = NSNumber(double: 1.0)
                animation2.toValue = NSNumber(double: 0.0)
                
                let group : CAAnimationGroup = CAAnimationGroup()
                group.beginTime = CACurrentMediaTime() + Double(i) * 0.25
                group.animations = [animation, animation2]
                group.removedOnCompletion = false
                group.fillMode = kCAFillModeBackwards
                
                layer.addAnimation(group, forKey: "scale")
                layer.opacity = 0.0
                CATransaction.commit()
            }
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment