• <tfoot id='FSwtp'></tfoot>

      <bdo id='FSwtp'></bdo><ul id='FSwtp'></ul>

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

  • <i id='FSwtp'><tr id='FSwtp'><dt id='FSwtp'><q id='FSwtp'><span id='FSwtp'><b id='FSwtp'><form id='FSwtp'><ins id='FSwtp'></ins><ul id='FSwtp'></ul><sub id='FSwtp'></sub></form><legend id='FSwtp'></legend><bdo id='FSwtp'><pre id='FSwtp'><center id='FSwtp'></center></pre></bdo></b><th id='FSwtp'></th></span></q></dt></tr></i><div id='FSwtp'><tfoot id='FSwtp'></tfoot><dl id='FSwtp'><fieldset id='FSwtp'></fieldset></dl></div>
    1. <legend id='FSwtp'><style id='FSwtp'><dir id='FSwtp'><q id='FSwtp'></q></dir></style></legend>
      1. Swift - UIButton 覆盖 setSelected

        时间:2023-07-08
        <i id='bno86'><tr id='bno86'><dt id='bno86'><q id='bno86'><span id='bno86'><b id='bno86'><form id='bno86'><ins id='bno86'></ins><ul id='bno86'></ul><sub id='bno86'></sub></form><legend id='bno86'></legend><bdo id='bno86'><pre id='bno86'><center id='bno86'></center></pre></bdo></b><th id='bno86'></th></span></q></dt></tr></i><div id='bno86'><tfoot id='bno86'></tfoot><dl id='bno86'><fieldset id='bno86'></fieldset></dl></div>
          <bdo id='bno86'></bdo><ul id='bno86'></ul>

          • <small id='bno86'></small><noframes id='bno86'>

            • <legend id='bno86'><style id='bno86'><dir id='bno86'><q id='bno86'></q></dir></style></legend>
              <tfoot id='bno86'></tfoot>

                    <tbody id='bno86'></tbody>
                  本文介绍了Swift - UIButton 覆盖 setSelected的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在 Swift 中创建一个 UIButton 子类,以在选择时执行自定义绘图和动画

                  I'm making a UIButton subclass in Swift to perform custom drawing and animation on selection

                  在 Swift 中,在 ObjC 中覆盖 - (void)setSelected:(BOOL)selected 的等价物是什么?

                  What would be the equivalent in Swift of overriding - (void)setSelected:(BOOL)selected in ObjC?

                  我试过了

                  覆盖 var selected: Bool

                  所以我可以实现一个观察者,但我得到了

                  so I could implement an observer but I get

                  不能用存储的属性'selected'覆盖

                  推荐答案

                  和其他人提到的一样,您可以使用 willSet 来检测更改.但是,在覆盖中,您不需要将值分配给 super,您只是在观察现有的更改.

                  Like others mentioned you can use willSet to detect changes. In an override, however, you do not need assign the value to super, you are just observing the existing change.

                  您可以从以下游乐场观察到几件事:

                  A couple things you can observe from the following playground:

                  1. 覆盖 willSet/didSet 的属性仍会为 get/set 调用 super.您可以判断,因为状态从 .normal 变为 .selected.
                  2. willSet 和 didSet 即使值没有改变也会被调用,因此您可能希望将 selected 的值与 willSet 中的 newValue 进行比较oldValue 中的 didSet 来确定是否进行动画处理.
                  1. Overriding a property for willSet/didSet still calls super for get/set. You can tell because the state changes from .normal to .selected.
                  2. willSet and didSet are called even when the value is not changing, so you will probably want do the compare the value of selected to either newValue in willSet or oldValue in didSet to determine whether or not to animate.

                  import UIKit
                  
                  class MyButton : UIButton {
                  
                      override var isSelected: Bool {
                          willSet {
                              print("changing from (isSelected) to (newValue)")
                          }
                  
                          didSet {
                              print("changed from (oldValue) to (isSelected)")
                          }
                      }
                  }
                  
                  let button = MyButton()
                  
                  button.state == .normal
                  button.isSelected = true // Both events fire on change.
                  button.state == .selected
                  button.isSelected = true // Both events still fire.
                  

                  这篇关于Swift - UIButton 覆盖 setSelected的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:子类化 UIButton 但无法访问我的属性 下一篇:如何获取自定义 UIButton 上的点击坐标?

                  相关文章

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

                  1. <small id='yslmy'></small><noframes id='yslmy'>

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