Rotate UIBarButtonItem Swift

ε祈祈猫儿з 提交于 2019-12-21 05:29:08

问题


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)

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


回答1:


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

this is my storyboard setup

Here is the code of simple view controller

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

    }


}

Result

Hope this helps




回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/44856773/rotate-uibarbuttonitem-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!