旋转UIBarButtonItem Swift [英] Rotate UIBarButtonItem Swift

查看:103
本文介绍了旋转UIBarButtonItem Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swift中,我有一个汉堡棒按钮,所以当点击时我想要汉堡棒按钮旋转90度(这样线条是垂直的)然后当你再次点击它时我希望它回到它的原始状态(水平)

In Swift, I have a hamburger bar button, so when tapped I want that hamburger Bar button to rotate 90 degrees (so that the lines are vertical) and then when you click it again I would like it to go back to it's original state (horizontal)

注意:您能否确保这适用于UIBarButtonItem,因为对普通UIButton的某些解决方案不起作用。

NOTE: Can you make sure that this works for a UIBarButtonItem, because some solution to a normal UIButton does not work.

推荐答案

我在中使用 UIButton UIBarButtonItem 实现此目的,以及状态为垂直或不是的变量

I use a UIButton inside of UIBarButtonItem to achieve this, and a variable with state vertical or not

这是我的故事板设置

以下是简单视图控制器的代码

import UIKit

class ViewController: UIViewController {

    var isVertical : Bool = false

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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

    @IBAction func rotateAction(_ sender: Any) {
        if(!self.isVertical)
        {
            UIView.animate(withDuration: 0.2, animations: { 
                self.navigationItem.leftBarButtonItem?.customView?.transform =  CGAffineTransform(rotationAngle: 90 * .pi / 180)
            }, completion: { (finished) in
                self.isVertical = true
            })
        }else{
            UIView.animate(withDuration: 0.2, animations: {
                self.navigationItem.leftBarButtonItem?.customView?.transform =  CGAffineTransform.identity
            }, completion: { (finished) in
                self.isVertical = false
            })
        }

    }


}

结果

希望这会有所帮助

这篇关于旋转UIBarButtonItem Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆