1

我想在区域 CGRectMake(130, 0, 60, 480)中触摸时隐藏和显示工具栏

不使用UIGestureRecognizer因为它影响我的其他视图

如果在该区域中触摸一次,工具栏应该隐藏,如果工具栏被隐藏,应该显示工具栏

我试过这个

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  {
    if (mainToolbar.hidden == YES) {
        mainToolbar.hidden=NO;
    }
  else if(mainToolbar.hidden == NO){
           [mainToolbar setHidden:YES];
    }
  }

但仅在触摸工具栏时隐藏工具栏

谢谢......

4

2 回答 2

1

在添加工具栏的视图中编写代码。您将使用以下代码获得触摸位置:

UITouch *touch = [[event allTouches] anyObject];

CGPoint location = [touch locationInView: touch.view];

CGRectContainsPoint:然后使用函数检查触摸是否在您的矩形内。

另一种方法是在所需区域简单地放置一个按钮。希望这可以帮助

于 2013-08-02T08:42:54.757 回答
0

该方法为您提供了屏幕上所有触摸的 NSSet。您可以使用它来自定义您的触摸行为。例如:

//This will change the state of whether mainToolbar is hidden or not.  In the case of multiple touches, it will change the property if any touch is in the CGRect area.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    CGRect testRect = CGRectMake(130, 0, 60, 480);
    for (UITouch *touch in touches) {
        if (CGRectContainsPoint(testRect, [touch locationInView:self.view])) {
            mainToolbar.hidden = !mainToolbar.hidden;
        }
    }
}

或者,如果您只希望在进行一次触摸时发生该操作,而在用户用多个手指触摸时忽略它,您将检查:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    CGRect testRect = CGRectMake(130, 0, 60, 480);
    if ([touches count] == 1) {
        //If there is only one touch, we check for that.  Otherwise, we ignore it.
        UITouch *touch = [touches anyObject];
        if (CGRectContainsPoint(testRect, [touch locationInView:self.view])) {
            mainToolbar.hidden = !mainToolbar.hidden;
        }
    }
}

你可以用这四个函数touchesBegan: touchesMoved: touchesEnded:touchesCanceled:.

于 2013-08-02T08:57:39.593 回答