OGeek|极客世界-中国程序员成长平台

标题: 所有 UIButtons 的全局 ios 触觉反馈 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 16:04
标题: 所有 UIButtons 的全局 ios 触觉反馈

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

有人有什么建议吗?



Best Answer-推荐答案


在类别中覆盖提到@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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4