說我有這樣的代碼:如何製作超級視圖攔截按鈕觸摸事件?
#import <UIKit/UIKit.h>
@interface MyView : UIView
@end
@implementation MyView
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// How can I get this to show up even when the button is touched?
NSLog(@"%@", [touches anyObject]);
}
@end
@interface TestViewAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
}
@end
@implementation TestViewAppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
MyView *view = [[MyView alloc] initWithFrame:[window frame]];
[view setBackgroundColor:[UIColor whiteColor]];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Hiya!" forState:UIControlStateNormal];
[button setFrame:CGRectMake(100.0, 100.0, 200.0, 200.0)];
[view addSubview:button];
[window addSubview:view];
[window makeKeyAndVisible];
}
- (void)dealloc
{
[window release];
[super dealloc];
}
@end
有沒有辦法攔截越來越發送到該按鈕的觸摸事件?我最終想要做的是製作一個UIView子類,當它檢測到滑動時會告訴它的視圖控制器(或委託,無論哪個),以便將下一個視圖控制器「推送」到堆棧上(類似於iPhone主屏幕)。我想這是第一步,但如果我不正確地處理這些問題,我會接受建議。
你不是指返回[superview hitTest:point withEvent:event]; – 2012-02-14 15:06:19
'super'是對的,因爲你想調用類的父類方法,'superview'會跳過這個。 – keegan3d 2012-10-04 01:16:50