touchesBegan和touchesEnded在另一个子视图上检测到
问题描述:
我真的被困在一个星期左右这种情况。 我里面有一个UINavigationItem的UIBarButtonItem,层次结构是这样的touchesBegan和touchesEnded在另一个子视图上检测到
的BarButtonItem是segmentedControl的包装。 UIBarbutitem和UIsegmentedControl是以编程方式编写的,但其他编程则是在IB中编写的。
在这种情况下,我想在按下或触摸barbuttonitem后显示一个视图。在这个论坛中读到的几个线程中,我知道UIBarbuttonItem没有继承UIResponder,所以我选择NavigationBar来获得触摸,并且为它定义一个框架。
这是我提出的代码:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
navBar = self.navigationController.navigationBar;
int index = _docSegmentedControl.selectedSegmentIndex;
NSLog(@"index di touches began : %d", index);
CGFloat x;
if (index == 0) {
x = 0.0;
}else if (index == 1) {
x = widthSegment + 1;
}else if (index == 2) {
x = 2*widthSegment + 1;
}else if (index == 3) {
x = 3*widthSegment+ 1;
}else if (in dex == 4) {
x = 4*widthSegment + 1;
}
CGRect frame = CGRectMake(x, 0.00, widthSegment, 46.00);
UITouch *touch = [touches anyObject];
CGPoint gestureStartPoint = [touch locationInView:navBar];
NSLog(@"gesturestart : %f, %f", gestureStartPoint.x, gestureStartPoint.y);
if (CGRectContainsPoint(frame, gestureStartPoint)) {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(segmentItemTapped:) object:[self navBar]];
NSLog(@"cancel popover");
}
}
导航栏是myViewController.h声明,我将其设置为一个IBOutlet。
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
int index = _docSegmentedControl.selectedSegmentIndex;
NSLog(@"index di touches ended : %d", index);
navBar = self.navigationController.navigationBar;
CGFloat x;
if (index == 0) {
x = 0.0;
}else if (index == 1) {
x = widthSegment + 1;
}else if (in dex == 2) {
x = 2*widthSegment + 1;
}else if (index == 3) {
x = 3*widthSegment+ 1;
}else if (index == 4) {
x = 4*widthSegment + 1;
}
CGRect frame = CGRectMake(x, 0.00, widthSegment, 46.00);
UITouch *touch = [touches anyObject];
CGPoint gestureLastPoint = [touch locationInView:navBar];
NSLog(@"lastPOint : %d", gestureLastPoint);
if (CGRectContainsPoint(frame, gestureLastPoint)) {
if (touch.tapCount <= 2) {
[self performSelector:@selector(segmentItemTapped:) withObject:nil afterDelay:0.0];
}
}
}
touchesBegan和touchesEnded在我点击工具栏时检测到,而不在导航栏中。
我没有实现的则hitTest方法是这样的:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *touchedView = [super hitTest:point withEvent:event];
NSSet* touches = [event allTouches];
// handle touches if you need
return touchedView;
}
,但它仍然没有得到更好的。
有人可以解释为什么会发生这种情况? 问候
-Risma-
答
你可以一个动作简单地添加到barbutton项目像
[self.mybarbutton setAction:@selector(barButtonTapped:)];
[self.mybarbutton setTarget:self];
THX的帮助AJI,但它解决不了我的问题,segmentItemTap动作一个动作显示popover,当我尝试你的代码,popover没有来,我想起了未被发现的touchesBegan和touchesEnded在我的导航栏中,你知道为什么? – 2010-12-16 13:00:10