gpt4 book ai didi

所有 UIButtons 的全局 ios 触觉反馈

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:36:03 26 4
gpt4 key购买 nike

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

有人有什么建议吗?

最佳答案

在类别中覆盖提到@ChoungTran 的此类方法并不是一个好主意。如果您想自定义类别中的默认方法,最好在 swizzled method 中执行此操作.

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


来自 Apple documentation :

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:

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's class, there is no way to invoke the original implementation.

A category cannot reliably override methods declared in another category of the same class.

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.

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.

关于所有 UIButtons 的全局 ios 触觉反馈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52666393/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com