0

所以我在键盘和对象之间有这个空间。我想在用户触摸这个特定区域时触发一个方法。我该怎么做呢?我尝试使用带有 UITapGesture 的 UIView,但似乎无法正常工作。

4

4 回答 4

0

您引用的“对象”是什么?

如果它已经为用户交互启用,那么你所需要的就是......

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // do something
}

把它放在视图控制器中。键盘将消耗触摸,因此不会触发。如果“对象”启用了用户交互,它也会消耗触摸。这仅在按下不消耗触摸的区域时触发。

如果它不消耗触摸那么...

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // get the touch
    UITouch *touch = [touches anyObject];

    // get the location of the touch in the main view of the view controller
    CGPoint touchPoint = [touch locationInView:self.view];

    // if the touch is inside the object then ignore it
    if (CGRectContainsPoint(object.frame, touchPoint)) {
        return;
    }

    // this is where you do something.
}
于 2013-07-18T15:58:58.413 回答
0

您可能会使用不可见的按钮:

UIButton *theButton = [UIButton buttonWithType:UIButtonTypeCustom];
[theButton addTarget:self action:@selector(functionYouWantToCall:) forControlEvents:UIControlEventTouchUpInside];
theButton.frame = CGRectMake(x, y, width, height);
[self.view addSubview:theButton];

根据您的需要设置框架。完成按钮后,使用[theButton removeFromSuperview];

于 2013-07-18T16:00:10.857 回答
0

将 UITapGesture 添加到 self.view 但确保将 cancelsTouchInView 的属性设置为 NO。这将防止您覆盖您已经放置在屏幕上的任何触摸方法。

UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(someAction)];
tapGesture.cancelsTouchInView = NO;
[self.view addGestureRecognizer:tapGesture];
于 2013-07-18T16:24:42.517 回答
0

如果您尝试在键盘上添加其他键,例如 Chrome 如何使用“.com”的快捷方式等,您可能想尝试:

[self.yourTextView setInputAccessoryView:toolBar];
于 2013-07-18T16:45:44.827 回答