Вы можете добавить распознаватель жестов в представление.
Например,
// in viewDidLoad:
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTapped:)];
tapRecognizer.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tapRecognizer];
- (void)backgroundTapped:(UITapGestureRecognizer*)recognizer {
// display alert
}
Вы также можете попробовать иметь полный размер UIView
за кнопкой, которая имеет распознаватель жестов.на нем:
// in viewDidLoad:
UIView *backgroundView = [[UIView alloc] initWithFrame:self.view.bounds];
backgroundView.backgroundColor = [UIColor clearColor];
backgroundView.opaque = NO;
[self.view addSubview:backgroundView];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTapped:)];
tapRecognizer.numberOfTapsRequired = 1;
[backgroundView addGestureRecognizer:tapRecognizer];
- (void)backgroundTapped:(UITapGestureRecognizer*)recognizer {
// display alert
}
Это может работать лучше, чем добавление распознавателя жестов в self.view
.