在另一个子视图上检测到touchesBegan和touchesEnded
我真的被这个案子困了一周了。 我在 UINavigationItem 中有一个 UIBarButtonItem,层次结构如下
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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以简单地向栏按钮项目添加一个操作,例如
you can simply add an action to the barbutton item like