繁体   English   中英

Swift 无法为导航栏按钮项设置动画

[英]Swift unable to animate navigation bar button item

我需要一些帮助来翻译UINavigationBar中的rightBarButtonItem 我已经关注了这一点,还发现了类似的问题,这些问题导致我将按钮添加为customViewUIBarButtonItem ,但是代码仍然无法正常工作。

我创建了一个UIBarButtonItem并将一个UIButton设置为其自定义视图。

let button: UIButton = {
    let b = UIButton(type: .system)
    b.setTitle("Test", for: .normal)
    return b
}()

lazy var barButton = UIBarButtonItem(customView: button)

这个方法是从viewDidLoad调用的

func configNavBar() {
    navigationItem.rightBarButtonItem = barButton
}

在发生某些操作时为栏按钮设置动画:

func animateRightBarButton() {
    UIView.animate(withDuration: 0.25,
                   delay: 0,
                   usingSpringWithDamping: 0.8,
                   initialSpringVelocity: 1.5,
                   options: .curveEaseInOut,
                   animations: {
                    guard let customView = self.barButton.customView else {return}
                    var x: CGFloat = customView.transform.tx == 0 ? 100 : 0
                    customView.transform = CGAffineTransform(scaleX: x, y: 0)
    }, completion: nil)
} 

加载视图后,条形按钮按预期显示。 我想让按钮滑出屏幕,但我注意到了几个问题。

当调用此 function 时,该按钮将从视图中消失,没有 animation。 如果我print(x) (翻译 x 值)它保持在100.0并且不会使用三元运算符变回0 感谢任何帮助以使这项工作正常进行,谢谢。

使用此代码:

let bell = UIImage(named: "gift")!
    let button = UIButton(type: .custom)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setBackgroundImage(bell, for: .normal)
    button.addTarget(self, action: #selector(self.showAdd), for: .touchUpInside)
    let bellButton = UIBarButtonItem(customView: button)
    self.navigationItem.leftBarButtonItem = bellButton
    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
        if let bell = self.navigationItem.leftBarButtonItem?.customView {
            let animation = CAKeyframeAnimation(keyPath: "transform.translation.x")
            animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)
            animation.duration = 0.6
            animation.values = [-8.0, 8.0, -8.0, 8.0, -4.0, 4.0, -4.0, 4.0, 0.0 ]
            bell.layer.add(animation, forKey: "shake")
        }
    }

感谢@KMI

我通过在单独的帮助程序中执行 @KMI 方法的功能来提高可用性:

Swift 5

UIBarButtonItem+Helper.swift

extension UIBarButtonItem {
    typealias AnimationClosure = (_ customView: UIView)->()
    
    private var uniqueCustomViewAnimationKey: String {
        "customViewAnimation"
    }
    
    func animate(with animation: CAAnimation, delay: TimeInterval = 0.5) {
        DispatchQueue.main.asyncAfter(deadline: .now() + delay) { [weak self] in
            guard let `self` = self else { return }
            self.customView?.layer.add(animation, forKey: self.uniqueCustomViewAnimationKey)
        }
    }
}

以及在一些 class 中的用法:

func bellItem() -> UIBarButtonItem {
    let button = UIButton(type: .custom)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setBackgroundImage(UIImage(named: "bell"), for: .normal)
    button.addTarget(self, action: #selector(bellItemPressed), for: .touchUpInside)
        
    let item = UIBarButtonItem(customView: button)
        
    // create custom animation
    let animation = CAKeyframeAnimation(keyPath: "transform.translation.x")
    animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)
    animation.duration = 0.6
    animation.values = [-8.0, 7.0, -6.0, 5.0, -4.0, 3.0, -2.0, 1.0, 0.0 ]

    // applying the animation
    item.animate(with: animation)
        
    return item
}

@objc private func bellItemPressed() {
    // do some stuff when item pressed
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM