<tfoot id='3Lizn'></tfoot>

    <bdo id='3Lizn'></bdo><ul id='3Lizn'></ul>

    <small id='3Lizn'></small><noframes id='3Lizn'>

    1. <legend id='3Lizn'><style id='3Lizn'><dir id='3Lizn'><q id='3Lizn'></q></dir></style></legend>
    2. <i id='3Lizn'><tr id='3Lizn'><dt id='3Lizn'><q id='3Lizn'><span id='3Lizn'><b id='3Lizn'><form id='3Lizn'><ins id='3Lizn'></ins><ul id='3Lizn'></ul><sub id='3Lizn'></sub></form><legend id='3Lizn'></legend><bdo id='3Lizn'><pre id='3Lizn'><center id='3Lizn'></center></pre></bdo></b><th id='3Lizn'></th></span></q></dt></tr></i><div id='3Lizn'><tfoot id='3Lizn'></tfoot><dl id='3Lizn'><fieldset id='3Lizn'></fieldset></dl></div>

        用于按钮触摸事件的自定义 UIButton 类

        时间:2023-07-06

        1. <tfoot id='GWePK'></tfoot>

          <small id='GWePK'></small><noframes id='GWePK'>

              <bdo id='GWePK'></bdo><ul id='GWePK'></ul>
                <tbody id='GWePK'></tbody>

              • <i id='GWePK'><tr id='GWePK'><dt id='GWePK'><q id='GWePK'><span id='GWePK'><b id='GWePK'><form id='GWePK'><ins id='GWePK'></ins><ul id='GWePK'></ul><sub id='GWePK'></sub></form><legend id='GWePK'></legend><bdo id='GWePK'><pre id='GWePK'><center id='GWePK'></center></pre></bdo></b><th id='GWePK'></th></span></q></dt></tr></i><div id='GWePK'><tfoot id='GWePK'></tfoot><dl id='GWePK'><fieldset id='GWePK'></fieldset></dl></div>

                1. <legend id='GWePK'><style id='GWePK'><dir id='GWePK'><q id='GWePK'></q></dir></style></legend>

                  本文介绍了用于按钮触摸事件的自定义 UIButton 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  是否可以创建一个自定义 UIButton 类,其中包含每次用户触摸按钮时都会自动调用的触摸事件动画?

                  Is it possible to create a custom UIButton class with an touch event animation which gets automatically invoked everytime the user touches the button?

                   import UIKit
                  
                   class AnimatedButton: UIButton {
                  
                       @objc private func buttonClicked(sender: UIButton) {
                  
                                UIView.animate(withDuration: 0.5,
                                               delay: 1.0,
                                               usingSpringWithDamping: 1.0,
                                               initialSpringVelocity: 0.2,
                                               options:  .curveEaseOut,
                                               animations: {
                                                   //do animation
                                                   sender.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)
                                               },
                                               completion: nil)
                  
                       }
                    }
                  

                  所以我不想在 ViewController 中创建必须调用 button.buttonTouched() 的操作.每次我在所有 UIButton 中使用此类时,都会自动发生这种情况.

                  So I don't want to create an action in a ViewController in which button.buttonTouched() must be invoked. This should happen automatically everytime I use this class in all UIButton.

                  有没有可能做这样的事情?

                  Is there any possibility to do something like that?

                  推荐答案

                  像这样创建按钮的自定义类

                  Create the button's custom class like this

                  class CustomButton:UIButton {
                  
                      override init(frame: CGRect) {
                          super.init(frame: frame)
                          configeBtn()
                      }
                  
                      required init?(coder aDecoder: NSCoder) {
                         super.init(coder: aDecoder)
                          configeBtn()
                      }
                  
                      func configeBtn() {
                  
                          self.addTarget(self, action: #selector(btnClicked(_:)), for: .touchUpInside)
                  
                      }
                  
                      @objc func btnClicked (_ sender:UIButton) {
                  
                          // animate here 
                  
                      }
                  }
                  

                  这篇关于用于按钮触摸事件的自定义 UIButton 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:用于实例化 ViewController 的按钮在隐藏后不起作用 下一篇:以编程方式添加按钮以执行 segue

                  相关文章

                  <tfoot id='WDLXv'></tfoot>

                  1. <legend id='WDLXv'><style id='WDLXv'><dir id='WDLXv'><q id='WDLXv'></q></dir></style></legend>
                      <bdo id='WDLXv'></bdo><ul id='WDLXv'></ul>

                    <small id='WDLXv'></small><noframes id='WDLXv'>

                      <i id='WDLXv'><tr id='WDLXv'><dt id='WDLXv'><q id='WDLXv'><span id='WDLXv'><b id='WDLXv'><form id='WDLXv'><ins id='WDLXv'></ins><ul id='WDLXv'></ul><sub id='WDLXv'></sub></form><legend id='WDLXv'></legend><bdo id='WDLXv'><pre id='WDLXv'><center id='WDLXv'></center></pre></bdo></b><th id='WDLXv'></th></span></q></dt></tr></i><div id='WDLXv'><tfoot id='WDLXv'></tfoot><dl id='WDLXv'><fieldset id='WDLXv'></fieldset></dl></div>