旋转UIBarButtonItem Swift

时间:2017-07-01 02:11:51

标签: ios swift uibarbuttonitem

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

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

3 个答案:

答案 0 :(得分:7)

我在UIButton内使用UIBarButtonItem来实现这一目标,并使用状态为垂直的变量

这是我的故事板设置 enter image description here

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

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
            })
        }

    }


}

<强>结果

enter image description here

希望这有帮助

答案 1 :(得分:1)

@IBAction func rotateAction1(_ 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

        })

        revealViewController().revealToggle(true)
    } else {
        UIView.animate(withDuration: 0.2, animations: {
            self.navigationItem.leftBarButtonItem?.customView?.transform = CGAffineTransform.identity
        }, completion: {
            (finished) in
            self.isVertical = false
        })
        revealViewController().revealToggle(false)
    }
}

答案 2 :(得分:0)

斯威夫特4:

func rotateBarButton() {

        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40)) // Create new button & set its frame
        button.setImage(#imageLiteral(resourceName: "settings"), for: UIControlState()) // Assign an image
        let lef = UIBarButtonItem(customView: button)
        self.navigationItem.leftBarButtonItem = lef// Set as barButton's customView

        // Gets you half way there //
        UIView.animate(withDuration: 0.8, delay: 0.1, options: UIViewAnimationOptions.curveEaseIn, animations: {
            self.navigationItem.leftBarButtonItem?.customView?.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI))
        }, completion: nil)
        // Rotates all the way around //
        UIView.animate(withDuration: 0.5, delay: 0.5, options: UIViewAnimationOptions.curveEaseIn, animations: {
            self.navigationItem.leftBarButtonItem?.customView?.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI * 2))
        }, completion: nil)
    }