3 回答

TA貢獻(xiàn)1869條經(jīng)驗 獲得超4個贊
在文檔中,查找“ 響應(yīng)者對象”和“響應(yīng)者鏈”
您可以通過轉(zhuǎn)發(fā)響應(yīng)者鏈上的修飾來“共享”對象之間的修飾。您的UIButton有一個接收UITouch事件的響應(yīng)器/控制器,我的猜測是,一旦它對返回的消息執(zhí)行了正確的解釋-觸摸已被處理和處置。
蘋果公司建議這樣的事情(當(dāng)然取決于觸摸的類型):
[self.nextResponder touchesBegan:touches withEvent:event];
而不是處理觸摸事件,而是將其傳遞出去。
子類UIButton:
MyButton.h
#import <Foundation/Foundation.h>
@interface MyButton : UIButton {
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event ;
@end
我的按鈕
#import "MyButton.h"
@implementation MyButton
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
printf("MyButton touch Began\n");
[self.nextResponder touchesBegan:touches withEvent:event];
}
@end

TA貢獻(xiàn)1735條經(jīng)驗 獲得超5個贊
我知道這個問題已經(jīng)有兩年了(并且已經(jīng)回答了),但是...
當(dāng)我自己嘗試時,觸摸被轉(zhuǎn)發(fā)了,但是按鈕不再像按鈕那樣表現(xiàn)。我也將修飾傳遞給“超級”,現(xiàn)在一切都很好。
因此,對于可能偶然發(fā)現(xiàn)此問題的初學(xué)者,代碼應(yīng)如下所示:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self.nextResponder touchesBegan:touches withEvent:event];
}

TA貢獻(xiàn)1982條經(jīng)驗 獲得超2個贊
也無需繼承!只需將其放在實現(xiàn)的頂層即可,然后再進(jìn)行其他操作:
#pragma mark PassTouch
@interface UIButton (PassTouch)
@end
@implementation UIButton (PassTouch)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[self.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
[self.nextResponder touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
[self.nextResponder touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
[self.nextResponder touchesCancelled:touches withEvent:event];
}
@end
- 3 回答
- 0 關(guān)注
- 750 瀏覽
添加回答
舉報