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

标签 ios swift uibarbuttonitem

我想做的就是在单击时旋转 UIBarButtonItem。

我关注了这篇文章 Rotate UIBarButtonItem Swift , 但它不起作用。

区别在于:

enter image description here

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

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

    } }

显示按钮属性:

enter image description here

最佳答案

这可能是您在栏项中设置自定义 View 的方式。也许这个 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
    })
}

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

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

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

关于ios - 动画(旋转)UIBarButtonItem 自定义按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49307990/

相关文章:

ios - Xcode 8.1 发布 APP 崩溃 libobjc.A.dylib objc_msgSend

ios - ABPeoplePickerNavigationController 类不支持子类化

uitableview - self.tableView 不被识别为 UITableView,而是一个随机方法

ios - 在 .docx 的 UITextView 中显示文本

ios - 在 iPad 上从 UIBarButtonItem 中关闭 UIActionSheet

ios - 如何在自定义的 UIBarButtonItem 上放置角标(Badge)

ios - MKMapView如何知道旋转角度?

ios - 具有不同 Storyboard样式的多个 iOS 目标

滑动时快速黑屏

ios - 如何为条形按钮项目实现图像和文本