zoukankan      html  css  js  c++  java
  • 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.
        }
        */
    }
  • 相关阅读:
    牛客IOI周赛17-提高组 卷积 生成函数 多项式求逆 数列通项公式
    6.3 省选模拟赛 Decompose 动态dp 树链剖分 set
    AtCoder Grand Contest 044 A Pay to Win 贪心
    5.29 省选模拟赛 树的染色 dp 最优性优化
    luogu P6097 子集卷积 FST FWT
    CF724C Ray Tracing 扩展欧几里得 平面展开
    5.30 省选模拟赛 方格操作 扫描线 特殊性质
    5.29 省选模拟赛 波波老师 SAM 线段树 单调队列 并查集
    Spring main方法中怎么调用Dao层和Service层的方法
    Bug -- WebService报错(两个类具有相同的 XML 类型名称 "{http://webService.com/}getPriceResponse"。请使用 @XmlType.name 和 @XmlType.namespace 为类分配不同的名称。)
  • 原文地址:https://www.cnblogs.com/tian-sun/p/5039470.html
Copyright © 2011-2022 走看看