码迷,mamicode.com
首页 > 其他好文 > 详细

HamburgerButton

时间:2017-07-10 10:25:51      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:size   otto   log   double   -128   alpha   select   duration   旋转   

Swift:Pop汉堡包变红叉叉

定义:

  //状态

    var hamburger = true

    var hamburgerButton : UIButton?

    var top:UIView?

    var  middle:UIView?

    var  bottom:UIView?

 

 //实例化按钮

        self.hamburgerButton = UIButton()

        self.hamburgerButton?.backgroundColor = UIColor.black

        self.hamburgerButton?.frame = CGRect(x: 100, y: 200, width: 150, height: 150)

        

        self.hamburgerButton?.layer.cornerRadius = 75

        self.view.addSubview(hamburgerButton!)

        self.hamburgerButton?.addTarget(self, action:#selector(ViewController.didTapHamburgerButton as (ViewController) -> () -> ()), for: UIControlEvents.touchUpInside)

        //三道杠

        let sectionWidth:CGFloat = 80.0

        let sectionHeight:CGFloat = 11.0

        //top

        top = UIView(frame: CGRect(x: (self.hamburgerButton?.bounds.size.width)! / 2 - sectionWidth / 2, y: 40, width: sectionWidth, height: sectionHeight))

        top?.backgroundColor = UIColor.white

        top?.isUserInteractionEnabled = false

        top?.layer.cornerRadius = sectionHeight / 2

        self.hamburgerButton?.addSubview(top!)

        //middle

        middle = UIView(frame: CGRect(x: (self.hamburgerButton?.bounds.size.width)! / 2 - sectionWidth / 2, y: 69, width: sectionWidth, height: sectionHeight))

        middle?.backgroundColor = UIColor.white

        middle?.isUserInteractionEnabled = false

        middle?.layer.cornerRadius = sectionHeight / 2

        self.hamburgerButton?.addSubview(middle!)

        //bottom

        bottom = UIView(frame: CGRect(x: (self.hamburgerButton?.bounds.size.width)! / 2 - sectionWidth / 2, y: 99, width: sectionWidth, height: sectionHeight))

        bottom?.backgroundColor = UIColor.white

        bottom?.isUserInteractionEnabled = false

        bottom?.layer.cornerRadius = sectionHeight / 2

        self.hamburgerButton?.addSubview(bottom!)

        //所有pop对象的复用实例

        //颜色动画

        var topColor = self.top?.pop_animation(forKey: "topColor") as! POPSpringAnimation?

        var bottomColor = self.top?.pop_animation(forKey: "bottomColor") as! POPSpringAnimation?

        

        //旋转动画

        var topRotate = self.top?.layer.pop_animation(forKey: "topRotate") as! POPSpringAnimation?

        var bottomRotate = self.top?.layer.pop_animation(forKey: "bottomRotate") as! POPSpringAnimation?

        

        //位置:Y坐标动画

        var topPosition = self.top?.layer.pop_animation(forKey: "topPosition")as!POPSpringAnimation?

        var bottomPosition = self.top?.layer.pop_animation(forKey: "bottomPosition")as!POPSpringAnimation?

 //三道杠变红叉叉

            hamburger = false

            UIView.animate(withDuration: 0.2, animations: {

                self.middle?.alpha = 0

            })

            //变色

            if topColor != nil {

                topColor?.toValue = UIColor.red

            }else{

                topColor = POPSpringAnimation(propertyNamed: kPOPViewBackgroundColor)

                topColor?.toValue = UIColor.red

                topColor?.springBounciness = 0

                topColor?.springSpeed = 18

                top?.pop_add(topColor, forKey: "topColor")

            }

            if bottomColor != nil {

                bottomColor?.toValue = UIColor.red

            }else{

                bottomColor = POPSpringAnimation(propertyNamed: kPOPViewBackgroundColor)

                bottomColor?.toValue = UIColor.red

                bottomColor?.springBounciness = 0

                bottomColor?.springSpeed = 18

                bottom?.pop_add(topColor, forKey: "bottomColor")

            }

            //旋转

            if topRotate != nil {

                topRotate?.toValue = -Double.pi / 4

            }else{

                topRotate = POPSpringAnimation(propertyNamed: kPOPLayerRotation)

                topRotate?.toValue = -Double.pi / 4

                topRotate?.springBounciness = 11

                topRotate?.springSpeed = 18

                top?.layer.pop_add(topRotate, forKey: "topRotate")

            }

            if bottomRotate != nil {

                bottomRotate?.toValue = Double.pi / 4

            }else{

                bottomRotate = POPSpringAnimation(propertyNamed: kPOPLayerRotation)

                bottomRotate?.toValue = Double.pi / 4

                bottomRotate?.springBounciness = 11

                bottomRotate?.springSpeed = 18

                bottom?.layer.pop_add(bottomRotate, forKey: "bottomRotate")

            }

            //移动

            if topPosition != nil {

                topPosition?.toValue = 29

            }else{

                topPosition = POPSpringAnimation(propertyNamed: kPOPLayerTranslationY)

                topPosition?.toValue = 29

                topPosition?.springBounciness = 11

                topPosition?.springSpeed = 18

                top?.layer.pop_add(topPosition, forKey: "topPosition")

            }

            if bottomPosition != nil {

                bottomPosition?.toValue = -29

            }else{

                bottomPosition = POPSpringAnimation(propertyNamed: kPOPLayerTranslationY)

                bottomPosition?.toValue = -29

                bottomPosition?.springBounciness = 11

                bottomPosition?.springSpeed = 18

                bottom?.layer.pop_add(bottomPosition, forKey: "bottomPosition")

            }

     //红叉叉变回三道杠把值设反就可以了

技术分享技术分享     

            

        

 

HamburgerButton

标签:size   otto   log   double   -128   alpha   select   duration   旋转   

原文地址:http://www.cnblogs.com/Princekin-Ios/p/7144225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!