每当触摸特定的UIViewController时,我想调用一个方法。
-touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
是一种能够完成我正在寻找的方法,不管它在状态栏,导航栏或工具栏上都没有检测到触摸。如何在每次触摸UIViewController时创建一个运行的方法?或者换句话说,整个屏幕?
答案 0 :(得分:4)
尝试添加手势识别。到你的window对象,因为它是uiview
的子类。
或者像@Bamsworld说的那样。 “对于全屏检测(包括状态栏),我认为您需要子类UIWindow
并使其成为becomeFirstResponder:
覆盖触摸并从窗口子类处理。”
答案 1 :(得分:1)
子类UIApplication说MyApplication并实现方法
- (void)sendEvent:(UIEvent *)event {
[super sendEvent:event];
// Do whatever you want
}
然后在main.m中将默认实现更改为
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, NSStringFromClass([MyApplication class]), NSStringFromClass([YourAppdelegate class]));
}
}
您将获得方法
中的每个操作- (void)sendEvent:(UIEvent *)event
答案 2 :(得分:0)
#import <QuartzCore/QuartzCore.h>
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setMultipleTouchEnabled:YES];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// Remove old red circles on screen
NSArray *subviews = [self.view subviews];
for (UIView *view in subviews) {
[view removeFromSuperview];
}
// Enumerate over all the touches and draw a red dot on the screen where the touches were
[touches enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
// Get a single touch and it's location
UITouch *touch = obj;
CGPoint touchPoint = [touch locationInView:self.view];
// Draw a red circle where the touch occurred
UIView *touchView = [[UIView alloc] init];
[touchView setBackgroundColor:[UIColor redColor]];
touchView.frame = CGRectMake(touchPoint.x, touchPoint.y, 30, 30);
touchView.layer.cornerRadius = 15;
[self.view addSubview:touchView];
[touchView release];
}];
}