如何使superview拦截按钮触摸事件?

Mic*_*ael 6 iphone cocoa-touch objective-c

说我有这个代码:

#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
Run Code Online (Sandbox Code Playgroud)

有没有办法拦截发送到按钮的触摸事件?我最终要做的是创建一个UIView子类,它会在检测到滑动时告诉它的视图控制器(或委托,无论哪个),这样它就可以将下一个视图控制器"推"到堆栈上(类似于iPhone主屏幕) ).我认为这是第一步,但如果我接近这个错误,我愿意接受建议.

Fel*_*xyz 16

我有兴趣看到其他解决方案,但我知道的最简单的方法是覆盖这两个UIView方法中的任何一个:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;
Run Code Online (Sandbox Code Playgroud)

调用这些方法来确定触摸是否在视图或其任何子视图的范围内,因此这是拦截触摸然后传递它的好点.随便做你想做的事

return [super hitTest:point withEvent:event];
Run Code Online (Sandbox Code Playgroud)

要么

return [super pointInside:point withEvent:event];
Run Code Online (Sandbox Code Playgroud)


Mic*_*ael 1

感谢您的建议和内容丰富的回复。我最终只使用了页面上显示的解释:(在“技巧 1:使用单个 UIScrollView 模拟照片应用程序滑动/缩放/滚动”下)。