iphone - 如何让 super View 拦截按钮触摸事件?

标签 iphone objective-c cocoa-touch

假设我有这段代码:

#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/

相关文章:

ios - 如何删除弹出 UIViewController 中的 UIParallaxDimmingView?

iphone - UIDocumentInteractionController 的奇怪问题

ios - 当用户正在打电话或网络共享时,如何使 iPhone 应用程序看起来正确?

iphone - 将视频添加到应用程序

ios - 将 UITapGestureRecognizer 添加到 UILabel 特定部分的最佳方法是什么?

iphone - 任何想法如何从同一工作区下的其他项目加载 Nib - IOS?

iphone - 如何减少 map 上的注释数量?

ios - 集成解析时在 Xcode 中出错

ios - 路径应用程序时间轴实现

iphone - @property(nonatomic)ivar @property(nonatomic,assign)ivar 相同还是不同?