如何使一个superview截取按钮触摸事件? [英] How to make a superview intercept button touch events?
问题描述
说我有这个代码:
#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主屏幕)。
Is there any way to intercept the touch events getting sent to the button? What I'd like to eventually do is make a UIView subclass that will tell its view controller (or delegate, whichever) when it detects a swipe so it can "push" the next view controller onto the stack (similar to the iPhone home screen). I figured this was the first step, but I'm open to suggestions if I'm approaching this incorrectly.
推荐答案
感谢您的支持。建议和资料性答复。我最后只是使用此页面上显示的说明:(在Trick 1:模拟照片应用程序使用单个UIScrollView刷卡/缩放/滚动)。
Thanks for the suggestions and informative replies. I ended up just using the explanation shown on this page: (under "Trick 1: Emulating Photos app swiping/zooming/scrolling with a single UIScrollView").
这篇关于如何使一个superview截取按钮触摸事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!