3 回答

TA貢獻1869條經驗 獲得超4個贊
在文檔中,查找“ 響應者對象”和“響應者鏈”
您可以通過轉發響應者鏈上的修飾來“共享”對象之間的修飾。您的UIButton有一個接收UITouch事件的響應器/控制器,我的猜測是,一旦它對返回的消息執行了正確的解釋-觸摸已被處理和處置。
蘋果公司建議這樣的事情(當然取決于觸摸的類型):
[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貢獻1735條經驗 獲得超5個贊
我知道這個問題已經有兩年了(并且已經回答了),但是...
當我自己嘗試時,觸摸被轉發了,但是按鈕不再像按鈕那樣表現。我也將修飾傳遞給“超級”,現在一切都很好。
因此,對于可能偶然發現此問題的初學者,代碼應如下所示:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self.nextResponder touchesBegan:touches withEvent:event];
}

TA貢獻1982條經驗 獲得超2個贊
也無需繼承!只需將其放在實現的頂層即可,然后再進行其他操作:
#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 關注
- 760 瀏覽
添加回答
舉報