我真的被困在一個星期左右這種情況。 我裏面有一個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-
THX的幫助AJI,但它解決不了我的問題,segmentItemTap動作一個動作顯示popover,當我嘗試你的代碼,popover沒有來,我想起了未被發現的touchesBegan和touchesEnded在我的導航欄中,你知道爲什麼? – 2010-12-16 13:00:10