我创建了一个UIView
子视图,我在故事板中添加到我的UIViewController
,我想回应子视图上的点击并调用superview的以下方法的实现。调用正常,但它不会将正确的touch.tapcount
作为参数传递给superview的实现。
以下是子视图中的方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
CGRect rect = [[UIScreen mainScreen] bounds];
UITouch *touch = [[event touchesForView:self]anyObject];
CGPoint location = [touch locationInView:self];
if(touch.tapCount == 2){
[self.superview touchesBegan:touches withEvent:event];
}
}
以下是superview中的方法,if块未执行:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
CGRect rect = [[UIScreen mainScreen] bounds];
UITouch *touch = [[event touchesForView:self]anyObject];
CGPoint location = [touch locationInView:self];
if(touch.tapCount == 2){
//Code is not executed here
}
}
解决方案
在superview中更改此语句:
UITouch *touch = [[event touchesForView:self]anyObject];
到此:
UITouch *touch = [[event touchesForView:[self.view.subviews objectAtIndex:1]] anyObject];