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

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

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

      1. iPhone:覆盖 UIButton buttonWithType 以返回子类

        时间:2023-07-06

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

                  <small id='7fFjs'></small><noframes id='7fFjs'>

                  本文介绍了iPhone:覆盖 UIButton buttonWithType 以返回子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我希望能够创建一个具有超大响应区域的 UIButton.我知道这样做的一种方法是覆盖子类中的 hitTest 方法,但我如何首先实例化我的自定义按钮对象?

                  [OversizedButton buttonWithType: UIButtonTypeDetailDisclosure];

                  无法开箱即用,因为 buttonWithType 返回的是 UIButton,而不是 OversizedButton.

                  看来我也需要重写 buttonWithType 方法.有谁知道怎么做?

                  @implementation OversizedButton+ (id)buttonWithType:(UIButtonType)buttonType{//构造并返回一个 OversizedButton 而不是 UIButton//需要处理 UIButtonTypeDetailDisclosure 等特殊类型//我需要知道如何做这部分}- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{//如果触摸事件在超大区域,则返回结果//我已经知道如何做这部分}@结尾

                  或者,也许我可以使用 alloc/initWithFrame 创建按钮.但是 buttonType 属性是只读的,那么如何创建自定义按钮类型呢?

                  注意:我知道还有其他方法可以做到这一点,例如在可见按钮后面设置一个不可见按钮.我不喜欢这种方法,宁愿避免它.对上述方法的任何帮助都会非常有帮助.谢谢

                  解决方案

                  UIButton buttonWithType: 返回 UIButtonUIRoundedRectButton,具体取决于type 参数的值.

                  由于 UIButton 没有提供 initWithType: 方法,我认为尝试重写 buttonWithType: 会很危险.p>

                  我建议您改为子类 UIControl.然后,您可以将按钮作为子视图添加到您的控件,并拦截 hitTest:withEvent:.

                  I want to be able to create a UIButton with an oversized responsive area. I know that one way to do that is to override the hitTest method in a subclass, but how do I instantiate my custom button object in the first place?

                  [OversizedButton buttonWithType: UIButtonTypeDetailDisclosure];
                  

                  doesn't work out of the box because buttonWithType returns a UIButton, not an OversizedButton.

                  So it seems like I need to override the buttonWithType method as well. Does anyone know how to do this?

                  @implementation OversizedButton
                  
                  + (id)buttonWithType:(UIButtonType)buttonType
                  {
                     // Construct and return an OversizedButton rather than a UIButton
                     // Needs to handle special types such as UIButtonTypeDetailDisclosure
                     // I NEED TO KNOW HOW TO DO THIS PART
                  }
                  
                  - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 
                  {
                     // Return results if touch event was in oversized region
                     // I ALREADY KNOW HOW TO DO THIS PART
                  }
                  
                  @end
                  

                  Alternatively, maybe I could create the button using alloc/initWithFrame. But the buttonType property is readonly, so how do you create the custom button types?

                  Note: I know there are other ways to do this, such as having an invisible button behind the visible one. I don't care for that approach and would prefer to avoid it. Any help on the approach described above would be very helpful. Thanks

                  解决方案

                  UIButton buttonWithType: returns a UIButton or a UIRoundedRectButton, depending on the value of the type parameter.

                  As UIButton doesn't provide an initWithType: method, I believe it would be dangerous to try and override buttonWithType:.

                  I suggest you subclass UIControl instead. You can then add a button as a subview to your control, and intercept hitTest:withEvent:.

                  这篇关于iPhone:覆盖 UIButton buttonWithType 以返回子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Iphone UIButton 在嵌套的 UIViews 中不起作用 下一篇:动画按钮 Allowuserinteraction 不起作用

                  相关文章

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

                      <bdo id='4HQzx'></bdo><ul id='4HQzx'></ul>

                    <small id='4HQzx'></small><noframes id='4HQzx'>

                    <legend id='4HQzx'><style id='4HQzx'><dir id='4HQzx'><q id='4HQzx'></q></dir></style></legend>