菜鸟教程小白 发表于 2022-12-13 16:04:33

所有 UIButtons 的全局 ios 触觉反馈


                                            <p><p>如何在我的应用程序中为每个 UIButton 的每个 touchupinside 事件应用触觉反馈,而无需为每个单独的按钮编写代码?我已经尝试创建一个 UIButton 类别并覆盖 <code>- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event</code> 但这会弄乱我的一些 UIButton 操作,(我可能实现得很糟糕)</p>

<p>有人有什么建议吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在类别中覆盖提到@ChoungTran 的此类方法并不是一个好主意。如果您想自定义类别中的默认方法,最好在 <a href="https://nshipster.com/method-swizzling/" rel="noreferrer noopener nofollow">swizzled method</a> 中进行。 .</p>

<p>但是,我更喜欢创建一个从 UIButton 派生的自定义类,并在那里实现该逻辑,并在需要触觉的任何地方使用该按钮。</p>

<hr/>

<p>来自 <a href="http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocCategories.html#//apple_ref/doc/uid/TP30001163-CH20-SW1" rel="noreferrer noopener nofollow">Apple documentation</a> :</p>

<blockquote>
<p>Although the Objective-C language currently allows you to use a
category to override methods the class inherits, or even methods
declared in the class interface, you are strongly discouraged from
doing so. A category is not a substitute for a subclass. There are
several significant shortcomings to using a category to override
methods:</p>

<p>When a category overrides an inherited method, the method in the
category can, as usual, invoke the inherited implementation via a
message to super. However, if a category overrides a method that
exists in the category&#39;s class, there is no way to invoke the original
implementation.</p>

<p>A category cannot reliably override methods declared in another
category of the same class.</p>

<p>This issue is of particular significance because many of the Cocoa
classes are implemented using categories. A framework-defined method
you try to override may itself have been implemented in a category,
and so which implementation takes precedence is not defined.</p>

<p>The very presence of some category methods may cause behavior changes
across all frameworks. For example, if you override the
windowWillClose: delegate method in a category on NSObject, all window
delegates in your program then respond using the category method; the
behavior of all your instances of NSWindow may change. Categories you
add on a framework class may cause mysterious changes in behavior and
lead to crashes.</p>
</blockquote></p>
                                   
                                                <p style="font-size: 20px;">关于所有 UIButtons 的全局 ios 触觉反馈,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/52666393/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/52666393/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: 所有 UIButtons 的全局 ios 触觉反馈