【问题标题】:Animate (Rotate) UIBarButtonItem custom buttons动画(旋转)UIBarButtonItem 自定义按钮
【发布时间】:2018-08-24 18:25:57
【问题描述】:

我想要做的就是在 UIBarButtonItem 被点击时旋转它。

我关注了这个帖子Rotate UIBarButtonItem Swift,但它不起作用。

区别在于:

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

    } }

显示按钮属性:

【问题讨论】:

  • 尝试在第一个 if 之前添加 DispatchQueue.main.async { 并在最后一个 } 之前关闭它。我知道 @IBAction 是在主线程上调用的,但我最近看到很多人在更新 @IBAction 内的 UI 时遇到问题,并且通常在 main 修复它时调度它。
  • @IonescuVlad 我试过了,但它不起作用
  • 什么不起作用?
  • @Koen UIBarButtonItem 没有旋转
  • 您是否单步执行了代码?

标签: ios swift uibarbuttonitem


【解决方案1】:

这可能是您在栏项目中设置自定义视图的方式。也许这个self.navigationItem.rightBarButtonItem?.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 existingBarItems: [UIBarButtonItem] = navigationItem.rightBarButtonItems ?? []
let rightBarItems = existingBarItems = [yourCustomButtonItem]
navigationItem.setRightBarButtonItems(rightBarItems, animated: true)

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

【讨论】:

  • @Glaphi 您的解决方案有效,但它取代了完成按钮而不是旁边,如何解决?谢谢。 (“完成”是通过情节提要而不是在运行时创建的)
  • 我编辑了我的答案,看看我是否遗漏了任何东西@Wael
  • @Glaphi 它工作的人谢谢你。但我一直在挖掘为什么它不能通过故事板工作,我拖放一个 uibutton 并且仍然不能工作。
  • 不客气!如果解决了您的问题,您可以将答案标记为正确:)
  • @Glaphi 我找到了原因,但我不知道如何解决它,看起来 UIbutton 必须是右侧的第一个按钮,如果不是这样,它将无法工作(自我.navigationItem.rightBarButtonItem?.customView 返回 nil)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-30
  • 1970-01-01
  • 2010-09-14
相关资源
最近更新 更多