在另一个子视图上检测到touchesBegan和touchesEnded

发布于 2024-10-08 12:29:51 字数 2698 浏览 0 评论 0原文

我真的被这个案子困了一周了。 我在 UINavigationItem 中有一个 UIBarButtonItem,层次结构如下

alt text

BarButtonItem 是分段控件的包装。 UIBarbuttonitem 和 UIsegmentedControl 是通过编程方式制作的,但其他的是在 IB 中制作的。

在这种情况下,我想在按下或触摸栏按钮项后显示视图。在我在这个论坛上读到的几个线程中,我知道 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");
}
}

navBar 在 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;
}

但它仍然没有变得更好。

有人可以描述为什么会发生这种情况吗? 问候
-里斯玛-

i really got stuck in a week about this case.
i have a UIBarButtonItem inside UINavigationItem, the hierarchy is like this

alt text

The BarButtonItem is a wrapper of segmentedControl. The UIBarbuttonitem and UIsegmentedControl are made programmatically, but the others are made in IB.

in this case, i want to show a view after pressing or touching the barbuttonitem. In several thread i read in this forum, i knew that UIBarbuttonItem didn't inherit UIResponder, so i choose the NavigationBar to get the touch, and i define a frame for it.

this is the code i made :

-(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");
}
}

the navBar was declare in myViewController.h and i set it as an 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 and touchesEnded was detected when i tap at the toolbar, NOT in the navbar.

i did implemented the hitTest method like this :

- (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;
}

but it still nothing get better.

Somebody can decribe why this is happen?
Regards
-Risma-

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌路终见情 2024-10-15 12:29:51

您可以简单地向栏按钮项目添加一个操作,例如

[self.mybarbutton setAction:@selector(barButtonTapped:)];  

[self.mybarbutton setTarget:self];

you can simply add an action to the barbutton item like

[self.mybarbutton setAction:@selector(barButtonTapped:)];  

[self.mybarbutton setTarget:self];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文