动画(旋转)UIBarButtonItem 自定义按钮

Animate (Rotate) UIBarButtonItem custom buttons

我只想在单击时旋转 UIBarButtonItem。

我遵循了这个 post ,但它不起作用。

区别是:

1- 我在加载视图时在运行时设置图像:

     showHideBtn.image = showHeaderimage

2- 我的右侧栏按钮项中有两个按钮:

这是我的代码:

 @IBAction func rotateAction(_ sender: Any) {
    if(!self.isVertical)
    {
        UIView.animate(withDuration: 0.2, animations: { 

  self.navigationItem.rightBarButtonItem?.customView?.transform =  CGAffineTransform(rotationAngle: 90 * .pi / 180)
        }, completion: { (finished) in
            self.isVertical = true
        })
    }else{
        UIView.animate(withDuration: 0.2, animations: {
            self.navigationItem.rightBarButtonItem?.customView?.transform =  CGAffineTransform.identity
        }, completion: { (finished) in
            self.isVertical = false
        })
    }

}

我做错了什么?

更新代码:

   DispatchQueue.main.async {
     if(!self.isVertical) {

        UIView.animate(withDuration: 0.2, animations: {

            self.navigationItem.rightBarButtonItem?.customView?.transform =  CGAffineTransform(rotationAngle: 90 * .pi / 180)
        }, completion: { (finished) in
           self.isVertical = true
        })


    }else {
        UIView.animate(withDuration: 0.2, animations: {

            self.navigationItem.rightBarButtonItem?.customView?.transform =  CGAffineTransform(rotationAngle: 90 * .pi / 180)
        }, completion: { (finished) in
           self.isVertical = false
        })

    } }

显示按钮 属性:

这可能是您在栏项中设置自定义视图的方式。也许这个 self.navigationItem.rightBarButtonItem?.customView?. 返回 nil。无论如何,这是一个工作版本:

正在创建自定义 UIBarButtonItem:

let button1: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 60, height: 30))

let button2: UIButton = UIButton(frame: CGRect(x: 70, y: 0, width: 60, height: 30))

override func viewDidLoad(_ animated: Bool) {
    super.viewDidLoad(animated)
    button1.backgroundColor = .gray
    button1.setTitle("CustomButton", for: .normal)
    button1.addTarget(self, action: #selector(rotateAction(_:)), for: .touchUpInside)
    let barItem1: UIBarButtonItem = UIBarButtonItem(customView: button1)

    button2.backgroundColor = .gray
    button2.setTitle("CustomButton", for: .normal)
    button2.addTarget(self, action: #selector(rotateAction(_:)), for: .touchUpInside)
    let barItem2: UIBarButtonItem = UIBarButtonItem(customView: button1)

    navigationItem.setRightBarButtonItems([barItem1, barItem2], animated: true)
}

为点击的按钮设置动画:

@objc func rotateAction(_ sender: UIButton) {
    let customView = sender

    let transform: CGAffineTransform = isVertical ? .identity : CGAffineTransform(rotationAngle: 90 * .pi / 180)

    UIView.animate(withDuration: 0.2, animations: {
        customView.transform =  transform
    }, completion: { (finished) in
        self.isVertical = !self.isVertical
    })
}

为避免替换情节提要中设置的条形按钮 item/items,请获取这些按钮项并创建一个条形按钮项数组,其中包括您在代码中创建的那些:

let existingBarItems: [UIBarButtonItem] = navigationItem.rightBarButtonItems ?? []
let rightBarItems = existingBarItems = [yourCustomButtonItem]
navigationItem.setRightBarButtonItems(rightBarItems, animated: true)

并确保您的自定义栏按钮项的框架不与现有按钮相交。