假设我有这段代码:
#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 子类,当它检测到滑动时,它将告诉它的 View Controller (或委托(delegate),无论哪个),以便它可以将下一个 View Controller “推”到堆栈上(类似于 iPhone 主屏幕).我认为这是第一步,但如果我处理不当,我愿意接受建议。
最佳答案
我有兴趣查看其他解决方案,但我知道最简单的方法是重写这两个 UIView 方法中的任何一个:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;
调用这些方法以确定触摸是否在 View 或其任何 subview 的边界内,因此这是拦截触摸然后将其传递的好时机。随心所欲,然后
return [super hitTest:point withEvent:event];
或
return [super pointInside:point withEvent:event];
关于iphone - 如何让 super View 拦截按钮触摸事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1281211/