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)