swift po 实现动态按钮2

//
//  ButtonViewController.swift
//  PopInstall
//
//  Created by su on 15/12/11.
//  Copyright © 2015年 tian. All rights reserved.
//

import UIKit

class ButtonViewController: UIViewController {
    //状态
    var hamburgerOpen = true
    var hambureButton : MyButton?
    var top:UIView?
    var middle:UIView?
    var bottom:UIView?
   
    override func viewDidLoad() {
        super.viewDidLoad()
       
        self.view.backgroundColor = UIColor.whiteColor()
        //实例化汉堡包按钮
        self.hambureButton = MyButton()
        self.hambureButton?.backgroundColor = UIColor.blackColor()
        self.hambureButton?.frame = CGRect(x: 100, y: 100, 150, height: 150)
        self.hambureButton?.layer.cornerRadius = 75
        self.view.addSubview(hambureButton!)
       
        self.hambureButton?.addTarget(self, action: "didTapHanburgerButton:", forControlEvents: UIControlEvents.TouchUpInside)
        //三道杠的高度
        let sectionWidth:CGFloat = 80.0
        let sectionHeight:CGFloat = 11.0
        //绘制三道杠
        top = UIView(frame: CGRect(x: self.hambureButton!.bounds.size.width / 2 - sectionWidth / 2, y: 40, sectionWidth, height: sectionHeight))
        top!.backgroundColor = UIColor.whiteColor()
        top!.userInteractionEnabled = false
        top!.layer.cornerRadius = sectionHeight / 2
        self.hambureButton?.addSubview(top!)
       
        middle = UIView(frame: CGRect(x: self.hambureButton!.bounds.size.width / 2 - sectionWidth / 2, y: 69, sectionWidth, height: sectionHeight))
        middle!.backgroundColor = UIColor.whiteColor()
        middle!.userInteractionEnabled = false
        middle!.layer.cornerRadius = sectionHeight / 2
        self.hambureButton?.addSubview(middle!)
       
        bottom = UIView(frame: CGRect(x: self.hambureButton!.bounds.size.width / 2 - sectionWidth / 2, y: 99, sectionWidth, height: sectionHeight))
        bottom!.backgroundColor = UIColor.whiteColor()
        bottom!.userInteractionEnabled = false
        bottom!.layer.cornerRadius = sectionHeight / 2
        self.hambureButton?.addSubview(bottom!)
    }
    func didTapHanburgerButton(sender:UIButton) {
        //所有pop对象的复用实例
        //颜色动画
        var topColor = self.top?.pop_animationForKey("topColor") as! POPSpringAnimation?
        var bottomColor = self.bottom?.pop_animationForKey("bottomColor") as! POPSpringAnimation?
       
        //角度旋转动画
        var topRotate = self.top?.layer.pop_animationForKey("topRatate") as! POPSpringAnimation?
        var bottomRotate = self.bottom?.layer.pop_animationForKey("bottomRotate") as! POPSpringAnimation?
//
//        //位置:Y坐标 动画
        var topPosition = self.top?.layer.pop_animationForKey("topPosition") as! POPSpringAnimation?
        var bottonPosition = self.bottom?.layer.pop_animationForKey("bottonPosition") as! POPSpringAnimation?
       
        if hamburgerOpen {
            //三道杠变红叉叉
           hamburgerOpen = false
            UIView.animateWithDuration(0.2, animations: { () -> Void in
                self.middle?.alpha = 0
            })
//            改变上下两道杠的颜色
            if topColor != nil {
                topColor?.toValue = UIColor.redColor()
            } else {
                topColor = POPSpringAnimation(propertyNamed: kPOPViewBackgroundColor)
                topColor?.toValue = UIColor.redColor()
                topColor?.springBounciness = 0
                topColor?.springSpeed = 18
                top?.pop_addAnimation(topColor, forKey: "topColor")
            }
            if bottomColor != nil {
                bottomColor?.toValue = UIColor.redColor()
            } else {
                bottomColor = POPSpringAnimation(propertyNamed: kPOPViewBackgroundColor)
                bottomColor?.toValue = UIColor.redColor()
                bottomColor?.springBounciness = 0
                bottomColor?.springSpeed = 18
                bottom?.pop_addAnimation(bottomColor, forKey: "bottomColor")
            }
           
            //旋转上下两道杠的角度
            if topRotate != nil {
                topRotate?.toValue = -M_PI / 4
            } else {
                topRotate = POPSpringAnimation(propertyNamed: kPOPLayerRotation)
                topRotate?.toValue = -M_PI / 4
                topRotate?.springBounciness = 11
                topRotate?.springSpeed = 18
                top?.layer.pop_addAnimation(topRotate, forKey: "topRatate")
            }
           
            if bottomRotate != nil {
                bottomRotate?.toValue = M_PI / 4
            } else {
                bottomRotate = POPSpringAnimation(propertyNamed: kPOPLayerRotation)
                bottomRotate?.toValue = M_PI / 4
                bottomRotate?.springBounciness = 11
                bottomRotate?.springSpeed = 18
                bottom?.layer.pop_addAnimation(bottomRotate, forKey: "bottomRotate")
            }
            //改变上下两道杠的y坐标形成红色的叉叉
            if topPosition != nil {
                topPosition?.toValue = 29
            } else {
                topPosition = POPSpringAnimation(propertyNamed: kPOPLayerTranslationY)
                topPosition?.toValue = 29
                topPosition?.springBounciness = 11
                topPosition?.springSpeed = 18
                top?.layer.pop_addAnimation(topPosition, forKey: "topPosition")
            }
           
            if bottonPosition != nil {
                bottonPosition?.toValue = -29
            } else {
                bottonPosition = POPSpringAnimation(propertyNamed: kPOPLayerTranslationY)
                bottonPosition?.toValue = -29
                bottonPosition?.springBounciness = 11
                bottonPosition?.springSpeed = 18
                bottom?.layer.pop_addAnimation(bottonPosition, forKey: "bottonPosition")
            }
           
        } else {
            //红叉叉变三道杠
            hamburgerOpen = true
            UIView.animateWithDuration(0.2, animations: { () -> Void in
                self.middle?.alpha = 1
            })
            //            改变上下两道杠的颜色
            if topColor != nil {
                topColor?.toValue = UIColor.whiteColor()
            } else {
                topColor = POPSpringAnimation(propertyNamed: kPOPViewBackgroundColor)
                topColor?.toValue = UIColor.whiteColor()
                topColor?.springBounciness = 0
                topColor?.springSpeed = 18
                top?.pop_addAnimation(topColor, forKey: "topColor")
            }
            if bottomColor != nil {
                bottomColor?.toValue = UIColor.whiteColor()
            } else {
                bottomColor = POPSpringAnimation(propertyNamed: kPOPViewBackgroundColor)
                bottomColor?.toValue = UIColor.whiteColor()
                bottomColor?.springBounciness = 0
                bottomColor?.springSpeed = 18
                bottom?.pop_addAnimation(bottomColor, forKey: "bottomColor")
            }
           
            //旋转上下两道杠的角度
            if topRotate != nil {
                topRotate?.toValue = 0
            } else {
                topRotate = POPSpringAnimation(propertyNamed: kPOPLayerRotation)
                topRotate?.toValue = 0
                topRotate?.springBounciness = 11
                topRotate?.springSpeed = 18
                top?.layer.pop_addAnimation(topRotate, forKey: "topRatate")
            }
           
            if bottomRotate != nil {
                bottomRotate?.toValue = 0
            } else {
                bottomRotate = POPSpringAnimation(propertyNamed: kPOPLayerRotation)
                bottomRotate?.toValue = 0
                bottomRotate?.springBounciness = 11
                bottomRotate?.springSpeed = 18
                bottom?.layer.pop_addAnimation(bottomRotate, forKey: "bottomRotate")
            }
            //改变上下两道杠的y坐标形成红色的叉叉
            if topPosition != nil {
                topPosition?.toValue = 0
            } else {
                topPosition = POPSpringAnimation(propertyNamed: kPOPLayerTranslationY)
                topPosition?.toValue = 0
                topPosition?.springBounciness = 11
                topPosition?.springSpeed = 18
                top?.layer.pop_addAnimation(topPosition, forKey: "topPosition")
            }
           
            if bottonPosition != nil {
                bottonPosition?.toValue = 0
            } else {
                bottonPosition = POPSpringAnimation(propertyNamed: kPOPLayerTranslationY)
                bottonPosition?.toValue = 0
                bottonPosition?.springBounciness = 11
                bottonPosition?.springSpeed = 18
                bottom?.layer.pop_addAnimation(bottonPosition, forKey: "bottonPosition")
            }

        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
   

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
}
原文地址:https://www.cnblogs.com/tian-sun/p/5039470.html