(iphone)[super touchesBegan / Moved / Ended]做什么? [英] (iphone) what does [super touchesBegan/Moved/Ended] do?

查看:130
本文介绍了(iphone)[super touchesBegan / Moved / Ended]做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数重载方法需要 [super theMethod] 调用。

Most overloaded methods require [super theMethod] call.

(例如, [super viewDidLoad]; [super viewWillAppear]; [super dealloc];

我没有想过我是否需要 [super touchesBegan:withEvent:] 是否打电话,但它似乎以某种方式发挥作用。

I didn't think twice whether I need [super touchesBegan:withEvent:] call or not, but it seems to play a role somehow.

我什么时候需要它?我需要它吗?

When do I need it and when don't I need it?

我正在尝试以编程方式取消触摸事件,这似乎与我提出的问题有关。

I'm trying to programmatically cancel touch events when I need to, and it seems to be related to the question I asked.

推荐答案

虽然这一切都取决于对象的预期行为,但我们要指出基本的:你在一个中调用超类实现如果你想要超类实现的默认行为,那么重写方法(你的意思是覆盖,而不是重载)。

While it all depends on the expected behaviors of the object, let's point out the basic: You call the superclass implementation in an overridden method (you mean overriding, not overloading) if you want default behaviors implemented by the superclass.

所以你应该问的问题是:

So the questions you should ask are:

1)超类实现的作用是什么?

1) What does the superclass implementation do?

2)我的对象是否需要它?

2) Do I need it for my object?

那么你的超类的 touchesBegan:withEvent:等是什么呢?这取决于您的类层次结构。类层次结构中的自定义类可能已经覆盖了这些方法,您可能想要这些行为。我假设没有自定义类已经覆盖了这些方法,所以你必须只检查Cocoa实现。

So what does touchesBegan:withEvent: etc. of your superclass do? It depends on your class hierarchy. The custom classes in your class hierarchy may already override those methods and you may want those behaviors, or not. I assume that no custom class has yet overridden those methods, so you have to check Cocoa implementations only.

touchesBegan:withEvent:首先在UIResponder类中定义。我们来看看 UIResponder文档 ,表示

touchesBegan:withEvent: is first defined in UIResponder class. Let's look at the UIResponder documentation, which says


此方法的默认实现不执行任何操作。然而UIR应答器的UIKit子类,特别是UIView,会立即将消息转发到响应者链。

The default implementation of this method does nothing. However immediate UIKit subclasses of UIResponder, particularly UIView, forward the message up the responder chain.

所以你在这里得到一个理论。如果你的类直接继承自UIResponder,你根本不需要调用超类实现,因为它无论如何都不会执行任何操作。如果它继承自其他UIKit子类(如UIView),则调用超类实现将启用在响应器链上转发消息。但是,覆盖 touchesBegan:withEvent:可能意味着您的子类本身想要处理触摸事件,因此您不希望将事件转发到响应者链。所以,我想你不想在这里调用超类实现。

So you get a theory here. If your class directly inherits from UIResponder, you don't need to call the superclass implementation at all as it does nothing anyway. If it inherits from other UIKit subclasses such as UIView, calling the superclass implementation will enable forwarding messages up the responder chain. However, overriding touchesBegan:withEvent: perhaps means that your subclass wants to handle touch events itself, so you don't want to forward the events up the responder chain. So, I guess you don't want to call the superclass implementation here.

这实际上取决于具体情况。比如说,你是UIScrollView的子类,并实现了一个 touches ... 方法。如果您仍然希望视图在用户拖动时滚动,那么您希望调用超类实现,因为它将为您处理滚动。

It really depends on the situation. Say, that you subclass a UIScrollView, and implements one of the touches... method. If you still want the view to scroll as the user drags, then you want to call the superclass implementation because it will handle scrolling for you.

总结一下,你有能够回答上面的两个问题来决定。要了解超类实现的功能,请在 Apple文档中阅读相关内容。在许多情况下,如果超类实现的作用对于常见的预期行为至关重要,那么指南文档会给出调用超类实现的明确建议。

To sum up, you have to be able to answer the two questions above to decide. To find out what a superclass implementation does, read about it in the Apple Documentation. In many cases, if what a superclass implementation does is essential for common expected behaviors, the guide documents give an explicit advice to call the superclass implementation.

这篇关于(iphone)[super touchesBegan / Moved / Ended]做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆