BarButtonItem是segmentedControl的包装器。 UIBarbuttonitem和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");
}
}
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];
}
}
}
当我点击工具栏,而不是在导航栏中时,检测到了触摸开始和触摸自动。
我确实实现了这样的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 -
答案 0 :(得分:0)
你可以简单地向barbutton项添加一个动作,如
[self.mybarbutton setAction:@selector(barButtonTapped:)];
[self.mybarbutton setTarget:self];