【问题标题】:Is it possible to extend UIButton so they all have a default behaviour without using @IBActions for each?是否可以扩展 UIButton 以便它们都具有默认行为而不使用 @IBActions ?
【发布时间】:2020-04-18 12:28:38
【问题描述】:

这可能是一个愚蠢的问题,但我正在努力思考是否有更好的方法来做到这一点。如果我有 10 个 ViewController,每个都有一个不同的按钮(假设这些按钮在 Storyboard 中创建了 Segue),但我希望它们在点击时都有一个简单的效果,我会这样写:

首先,一个 UIButton 的扩展,所以它有一个处理动画的方法

extension UIButton {
    func tap(){
    UIButton.animate(withDuration: 0.05, 
                     animations: { self.alpha -= 0.1 },
                     completion: { finish in
                        UIButton.animate(withDuration: 0.1, animations: {
                            self.alpha += 0.1
                     })
    })
}

然后为每个 ViewController 设置一个 IBAction。

class FirstViewController: UIViewController {

    @IBOutlet weak var button: UIButton!

    @IBAction func buttonTouchUpInside(_ sender: Any) {
        button.tap()
    }
}

...

class TenthViewController: UIViewController {
    @IBOutlet weak var button: UIButton!

    @IBAction func buttonTouchUpInside(_ sender: Any) {
        button.tap()
    }    
}

我想知道是否有更好的方法。以所有 UIButton 调用 tap() 的方式扩展 UIButton 的某种方式。我需要为所有这些添加一个目标吗?如果我确实使用了@IBAction,那么它会被稍后覆盖吗?

提前致谢,如果这是一个愚蠢的问题,我们深表歉意。

【问题讨论】:

    标签: swift uibutton ibaction


    【解决方案1】:

    我建议创建子类而不是扩展。例如,如果我想有几个按钮,在触摸时改变 alpha 或比例,我会使用这样的东西:

    class CustomButton: UIControl {
    
       override open var isHighlighted: Bool {
        didSet {
            UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseInOut, animations: {
                self.titleLabel.alpha = self.isHighlighted ? 0.3 : 1
                self.transform = self.isHighlighted ? .init(scaleX: 0.98, y: 0.98) : .identity
            }, completion: nil)
        }
      }
    }
    

    您可以在isHighlighted 中指定该效果。您可以创建 UIButton 的子类或使用 UIControl - 这样您就可以添加自定义标题标签、imageView 等。这取决于您的用例:)

    【讨论】:

    • 这太棒了,它用更少的代码实现了我所追求的。非常感谢@Ondřej。但是有一个问题,您是否使用 UIControl 而不是 UIButton 以便其他类型也可以继承它?
    • 太好了,很高兴听到这个消息!好吧,就我而言,我使用了 UIControl jsut,因为我想用子视图制作更复杂的视图。 (两个 ImageView,标签..)
    【解决方案2】:

    您可以继承 UIButton 并添加自定义行为:

    //  MARK: Selection Animated Button
    /**
    A button which animates when tapped.
    */
    open class AnimatedButton: UIButton {
        override public var isHighlighted: Bool {
            get { super.isHighlighted }
    
            set {
                if newValue && !isHighlighted {
                    UIView.animate(withDuration: 0.05,
                                   animations: { self.alpha = 0.5 },
                                     completion: { finish in
                                        UIButton.animate(withDuration: 0.1, animations: {
                                            self.alpha = 1.0
                                        })
                    })
                }
                super.isHighlighted = newValue
            }
        }
    }
    

    【讨论】:

    • 感谢詹姆斯的回答。这个解决方案的好处是只播放一次,这在其他情况下很有用,但是在这种情况下,只要用户按下它,我就需要按钮来保持它的风格,所以我接受了另一个答案。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    • 1970-01-01
    • 2011-06-09
    • 2011-03-21
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多