Ios 设置UIBarButtonim自定义按钮的动画(旋转)

Ios 设置UIBarButtonim自定义按钮的动画(旋转),ios,swift,uibarbuttonitem,Ios,Swift,Uibarbuttonitem,我只想在点击时旋转uibarbuttonite 我关注了这个帖子,但它不起作用 区别在于: 1-加载视图时,我在运行时设置图像: showHideBtn.image = showHeaderimage 2-我的右栏按钮项中有两个按钮: 这是我的密码: @IBAction func rotateAction(_ sender: Any) { if(!self.isVertical) { UIView.animate(withDuration: 0.2

我只想在点击时旋转uibarbuttonite

我关注了这个帖子,但它不起作用

区别在于:

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.RightBarButtonim?.customView?
返回的是零。无论如何,这里有一个工作版本:

创建自定义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
    })
}
为点击的按钮设置动画:

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
    })
}
为了避免替换序列图像板中的条形按钮项/项集,请获取这些按钮项并创建一个条形按钮项数组,其中包括您在代码中创建的条形按钮项:

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

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

尝试在第一个if之前添加
DispatchQueue.main.async{
,并在最后一个}之前关闭它。我知道
@IBAction
是在主线程上调用的,但我最近看到很多人在更新
@IBAction
中的UI时遇到问题,通常在主线程上调度它来修复它。@IonescuVlad我试过,它不起作用什么不起作用?@Koen the uibarbuttonite没有旋转你有没有仔细检查代码?@Glaphi你的解决方案起作用了,但它取代了旁边的“完成”按钮,如何解决?非常感谢。(完成是通过故事板而不是运行时创建的)我编辑了我的答案,看一看,如果我遗漏了什么,请告诉我@Wael@Glaphi这很好,谢谢你。但我一直在通过故事板挖掘为什么它不起作用,我拖动了一个uibutton,但仍然不起作用。不客气!,如果它解决了您的问题,您可以将答案标记为正确:)@Glaphi我找到了原因,但我不知道如何解决它,看起来UIbutton必须是右侧的第一个按钮,如果不是这样,它将无法工作(self.navigationItem.RightBarButtonim?.customView返回nil)