<commit_before>//
//  DetailViewController.swift
//  UIAnimateDemo
//
//  Created by Bear on 2017/6/29.
//  Copyright © 2017年 Bear. All rights reserved.
//

import UIKit
import Foundation

class DetailViewController: UIViewController {
    
    var animateView: UIView!
    fileprivate let duration = 2.0
    fileprivate let delay = 0.2
    fileprivate let scale = 1.2
    var barTitle = ""
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    init(animate:String) {
        self.barTitle = animate
        super.init(nibName: nil, bundle: nil)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = barTitle
        view.backgroundColor = UIColor.white
        setupRect()
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2.0) {
            self.doAnimate()
        }
    }
    
    fileprivate func setupRect()
    {
        if barTitle == "BezierCurve Position" {
            animateView = drawCircleView()
            
        } else if barTitle == "View Fade In" {
            animateView = UIImageView(image: UIImage(named: "whatsapp"))
            animateView.frame = generalFrame
            animateView.center = generalCenter
        } else {
            animateView = drawRectView(UIColor.red, frame: generalFrame, center: generalCenter)
        }
        view.addSubview(animateView)
    }
    
    func doAnimate()
    {
        switch barTitle {
        case "2-Color":
            changeColor(UIColor.green)
            
        case "Simple 2D Rotation":
            rotateView(Double.pi)
            
        case "Multicolor":
            multiColor(UIColor.green, UIColor.blue)
            
        case "Multi Point Position":
            multiPosition(CGPoint(x: animateView.frame.origin.x, y: 100), CGPoint(x: animateView.frame.origin.x, y: 350))
            
        case "BezierCurve Position":
            var controlPoint1 = self.animateView.center
            controlPoint1.y -= 125.0
            var controlPoint2 = controlPoint1
            controlPoint2.x += 40.0
            controlPoint2.y -= 125.0;
            var endPoint = self.animateView.center;
            endPoint.x += 75.0
            curvePath(endPoint, controlPoint1: controlPoint1, controlPoint2: controlPoint2)
            
            
        case "Color and Frame Change":
            let currentFrame = self.animateView.frame
            let firstFrame = currentFrame.insetBy(dx: -30, dy: -50)
            let secondFrame = firstFrame.insetBy(dx: 10, dy: 15)
            let thirdFrame = secondFrame.insetBy(dx: -15, dy: -20)
            colorFrameChange(firstFrame, secondFrame, thirdFrame, UIColor.orange, UIColor.yellow, UIColor.green)
            
        case "View Fade In":
            viewFadeIn()
            
        case "Pop":
            Pop()
            
        default:
            let alert = makeAlert("Alert", message: "The animation not implemented yet", actionTitle: "OK")
            self.present(alert, animated: true, completion: nil)
        }
    }
    
    // MARK: - Private Methods for Animations
    fileprivate func changeColor(_ color: UIColor) {
        UIView.animate(withDuration: self.duration, animations: {
            self.animateView.backgroundColor = color
        }, completion: nil)
    }
    
    fileprivate func multiColor(_ firstColor: UIColor, _ secondColor: UIColor) {
        UIView.animate(withDuration: duration, animations: {
            self.animateView.backgroundColor = firstColor
        }, completion: { finished in
            self.changeColor(secondColor)
        })
    }
    
    fileprivate func simplePosition(_ pos: CGPoint) {
        UIView.animate(withDuration: self.duration, animations: {
            self.animateView.frame.origin = pos
        }, completion: nil)
    }
    
    fileprivate func multiPosition(_ firstPos: CGPoint, _ secondPos: CGPoint) {
        UIView.animate(withDuration: self.duration, animations: {
            self.animateView.frame.origin = firstPos
        }, completion: { finished in
            self.simplePosition(secondPos)
        })
    }
    
    fileprivate func rotateView(_ angel: Double) {
        UIView.animate(withDuration: duration, delay: delay, options: [.repeat], animations: {
            self.animateView.transform = CGAffineTransform(rotationAngle: CGFloat(angel))
        }, completion: nil)
    }
    
    fileprivate func colorFrameChange(_ firstFrame: CGRect, _ secondFrame: CGRect, _ thirdFrame: CGRect,
                                      _ firstColor: UIColor, _ secondColor: UIColor, _ thirdColor: UIColor) {
        UIView.animate(withDuration: self.duration, animations: {
            self.animateView.backgroundColor = firstColor
            self.animateView.frame = firstFrame
        }, completion: { finished in
            UIView.animate(withDuration: self.duration, animations: {
                self.animateView.backgroundColor = secondColor
                self.animateView.frame = secondFrame
            }, completion: { finished in
                UIView.animate(withDuration: self.duration, animations: {
                    self.animateView.backgroundColor = thirdColor
                    self.animateView.frame = thirdFrame
                }, completion: nil)
            })
        })
    }
    
    fileprivate func curvePath(_ endPoint: CGPoint, controlPoint1: CGPoint, controlPoint2: CGPoint) {
        let path = UIBezierPath()<commit_msg>[xiongwei3][modify]add keyframe animate<commit_after>//
//  DetailViewController.swift
//  UIAnimateDemo
//
//  Created by Bear on 2017/6/29.
//  Copyright © 2017年 Bear. All rights reserved.
//

import UIKit
import Foundation

class DetailViewController: UIViewController {
    
    var animateView: UIView!
    fileprivate let duration = 2.0
    fileprivate let delay = 0.2
    fileprivate let scale = 1.2
    var barTitle = ""
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    init(animate:String) {
        self.barTitle = animate
        super.init(nibName: nil, bundle: nil)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = barTitle
        view.backgroundColor = UIColor.white
        setupRect()
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2.0) {
            self.doAnimate()
        }
    }
    
    fileprivate func setupRect()
    {
        if barTitle == "BezierCurve Position" {
            animateView = drawCircleView()
            
        } else if barTitle == "View Fade In" {
            animateView = UIImageView(image: UIImage(named: "whatsapp"))
            animateView.frame = generalFrame
            animateView.center = generalCenter
        } else {
            animateView = drawRectView(UIColor.red, frame: generalFrame, center: generalCenter)
        }
        view.addSubview(animateView)
    }
    
    func doAnimate()
    {
        switch barTitle {
        case "2-Color":
            changeColor(UIColor.green)
            
        case "Simple 2D Rotation":
            rotateView(Double.pi)
            
        case "Multicolor":
            multiColor(UIColor.green, UIColor.blue)
            
        case "Multi Point Position":
            multiPosition(CGPoint(x: animateView.frame.origin.x, y: 100), CGPoint(x: animateView.frame.origin.x, y: 350))
            
        case "BezierCurve Position":
            var controlPoint1 = self.animateView.center
            controlPoint1.y -= 125.0
            var controlPoint2 = controlPoint1
            controlPoint2.x += 40.0
            controlPoint2.y -= 125.0;
            var endPoint = self.animateView.center;
            endPoint.x += 75.0
            curvePath(endPoint, controlPoint1: controlPoint1, controlPoint2: controlPoint2)
            
            
        case "Color and Frame Change":
            let currentFrame = self.animateView.frame
            let firstFrame = currentFrame.insetBy(dx: -30, dy: -50)
            let secondFrame = firstFrame.insetBy(dx: 10, dy: 15)
            let thirdFrame = secondFrame.insetBy(dx: -15, dy: -20)
            colorFrameChange(firstFrame, secondFrame, thirdFrame, UIColor.orange, UIColor.yellow, UIColor.green)
            
        case "View Fade In":
            viewFadeIn()
            
        case "Pop":
            Pop()
            
        default:
            let alert = makeAlert("Alert", message: "The animation not implemented yet", actionTitle: "OK")
            self.present(alert, animated: true, completion: nil)
        }
    }
    
    // MARK: - Private Methods for Animations
    fileprivate func changeColor(_ color: UIColor) {
        UIView.animate(withDuration: self.duration, animations: {
            self.animateView.backgroundColor = color
        }, completion: nil)
    }
    
    fileprivate func multiColor(_ firstColor: UIColor, _ secondColor: UIColor) {
        UIView.animate(withDuration: duration, animations: {
            self.animateView.backgroundColor = firstColor
        }, completion: { finished in
            self.changeColor(secondColor)
        })
    }
    
    fileprivate func simplePosition(_ pos: CGPoint) {
        UIView.animate(withDuration: self.duration, animations: {
            self.animateView.frame.origin = pos
        }, completion: nil)
    }
    
    fileprivate func multiPosition(_ firstPos: CGPoint, _ secondPos: CGPoint) {
        UIView.animate(withDuration: self.duration, animations: {
            self.animateView.frame.origin = firstPos
        }, completion: { finished in
            self.simplePosition(secondPos)
        })
    }
    
    fileprivate func rotateView(_ angel: Double) {
        UIView.animate(withDuration: duration, delay: delay, options: [.repeat], animations: {
            self.animateView.transform = CGAffineTransform(rotationAngle: CGFloat(angel))
        }, completion: nil)
    }
    
    fileprivate func colorFrameChange(_ firstFrame: CGRect, _ secondFrame: CGRect, _ thirdFrame: CGRect,
                                      _ firstColor: UIColor, _ secondColor: UIColor, _ thirdColor: UIColor) {
        
        //两种方式实现这种连续动画
// 
//        UIView.animate(withDuration: self.duration, animations: {
//            self.animateView.backgroundColor = firstColor
//            self.animateView.frame = firstFrame
//        }, completion: { finished in
//            UIView.animate(withDuration: self.duration, animations: {
//                self.animateView.backgroundColor = secondColor
//                self.animateView.frame = secondFrame
//            }, completion: { finished in
//                UIView.animate(withDuration: self.duration, animations: {
//                    self.animateView.backgroundColor = thirdColor
//                    self.animateView.frame = thirdFrame
//                }, completion: nil)
//            })
//        })
//        
        UIView.animateKeyframes(withDuration: 1, delay: delay, options: .calculationModeLinear, animations: {
            
            UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.5, animations: { 
                self.animateView.backgroundColor = firstColor
                self.animateView.frame = firstFrame
            })
            
            UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5, animations: { 
                self.animateView.backgroundColor = secondColor
                self.animateView.frame = secondFrame
            })
            
            UIView.addKeyframe(withRelativeStartTime: 1.0, relativeDuration: 0.5, animations: { 
                self.animateView.backgroundColor = thirdColor
                self.animateView.frame = thirdFrame
            })
            
            
        }) { (finished) in
            
        }
        
        
    }
    
    fileprivate func curvePath(_ endPoint: CGPoint, controlPoint1: CGPoint, controlPoint2: CGPoint) {
        let path = UIBezierPath()
{'fraction_non_alphanumeric': 0.09109042553191489, 'fraction_numerical': 0.011884973404255319, 'mean_word_length': 1.9142649551949624, 'pattern_counts': {'":': 16, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6075881', 'n_tokens_mistral': 3440, 'n_tokens_neox': 3210, 'n_words': 718}