说我有这样的代码:如何制作超级视图拦截按钮触摸事件?
#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