//
//  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, width: 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, width: 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, width: 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, width: 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.
    }
    */

}

swift po 实现动态按钮2的更多相关文章

  1. Atitit 动态按钮图片背景颜色与文字组合解决方案

    Atitit 动态按钮图片背景颜色与文字组合解决方案 转换背景颜色,setFont("cywe_img", fontScale, 50, 5) 设置文字大小与坐标 文字分拆,使用字 ...

  2. android Button 切换背景,实现动态按钮和按钮颜色渐变

        android Button 切换背景,实现动态按钮和按钮颜色渐变 一.添加android 背景筛选器selector实现按钮背景改变     1.右键单击项目->new->Oth ...

  3. VS2010,MFC动态按钮和窗体背景图片,以及是静态文字控件透明,并避免静态文字刷新出现的重叠问题

    1.动态按钮的四种动作 1)正常 2)按下 3)滑过 4)失效 在MFC中,4个动作对应着四种位图bmp, 首先,将代表四种状态的位图加载入资源中,将对应的按钮设置为BitmapButton 第二,在 ...

  4. MFC动态按钮的创建及其消息响应(自定义消息)

    动态按钮(多个)的创建: 1.在类中声明并定义按钮控件的ID #define IDC_D_BTN 10000 2.在类的OnInitDialog()函数中动态创建按钮(建立按钮对象时最好建立对象的指针 ...

  5. MFC动态按钮的创建及其消息响应 和 自定义消息

    原文链接: http://www.cnblogs.com/gaohongchen01/p/4046525.html 动态按钮(多个)的创建: 1.在类中声明并定义按钮控件的ID #define IDC ...

  6. swift计算label动态宽度和高度

    swift计算label动态宽度和高度 func getLabHeigh(labelStr:String,font:UIFont,width:CGFloat) -> CGFloat { let ...

  7. vue之小小动态按钮

    Vue是前台框架,可以独立完成前后端分离式web项目渐进式的javascript框架 ,今天我们来设计一个简单的动态按钮   具体效果图如下: 点击后会变成这样: 首先我们需要下载vue.js:htt ...

  8. Swift - 获取、改变按钮的标题文本(UIButton点击切换title)

    在开发中,我们常常需要动态地改变按钮标签文字,使用 setTitle() 函数就可以了.有时我们需要在几个标题间切换,比如下面样例所示,按钮点击后按钮文字会在"播放""暂 ...

  9. swift class的动态派发

    一.测试代码 class BaseCallClass{ func NormalCall(){} @objc func OcCall(){} @objc dynamic func OcDynamicCa ...

随机推荐

  1. 【python】Beautiful Soup的使用

    1. Beautiful Soup的简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.pyt ...

  2. TS流解析 三

    应该说真正了解TS,还是看了朋友推荐的<数字电视业务信息及其编码>一书之后,MPEG2 TS和数字电视是紧密不可分割的,值得总结一下其中的一些关系. ISO/IEC-13818-1:系统部 ...

  3. php 的两个扩展 memcache 和 memcachd

    今天绕了很大弯子, 因为自己写的php的memcache类. 于是出现了下面问题 在本地测试好好的, 线上就出了问题 原因是线上使用的是memcache, 我本地使用的是memcached 区别参考网 ...

  4. 517. Super Washing Machines

    ▶ 超级洗碗机.给定一个有 n 元素的整数数组,我们把 “将指定位置上元素的值减 1,同时其左侧或者右侧相邻元素的值加 1” 称为一次操作,每个回合内,可以选定任意 1 至 n 个位置进行独立的操作, ...

  5. Snackbar学习笔记

    官方推荐使用Snackbar代替Toast   Paste_Image.png Snackbar使用其实就是一句话,跟toast很相似: Snackbar.make(view,"消息已发出& ...

  6. IOS AudioServicesPlaySystemSound 后台锁屏播放

    AudioServicesPlaySystemSound 想在锁屏后台播放报警提示音. 添加了UIBackgroundModes,audio,官方审核不通过! IOS的闹钟是怎么实现的,锁屏不能播放声 ...

  7. 使用SQL Server 2008的事务日志传送功能备份数据库(logshiping)

    使用SQL Server 2008的事务日志传送功能备份数据库(logshiping) 使用SQL Server 2008的事务日志传送功能备份数据库(logshiping)

  8. JAXB--@XmlElementWrapper注解(二)

    在JAXB标准中,@XmlElementWrapper注解表示生成一个包装 XML 表示形式的包装器元素. 此元素主要用于生成一个包装集合的包装器 XML 元素.因此,该注释支持以下两种形式的序列化. ...

  9. pdb调试

    [pdb调试] 前置技能: os.getcwd():获取当前工作目录. os.chdir():切换工作目录. 运行 python -m pdb myscript.py (Pdb) 会自动停在第一行,等 ...

  10. Cocoa Touch(四): 多线程GCD, NSObject, NSThread, NSOperationQueue

    多线程的重要性不必多言,现代操作系统不可能离开进程线程的抽象.具体到ios应用,我们只能在一个进程中管理线程,主线程不应该去执行非常耗时间的后台操作导致出现卡机现象,后台的事情交给后台线程来完成. G ...