【发布时间】:2014-11-06 03:19:56
【问题描述】:
有没有一种方法可以轻松检测到离点击位置最近的 UIButton?我目前已经对平移手势进行了子类化,但只是在弄清楚如何处理这个问题上遇到了麻烦。过去,我调整了每个按钮框架的大小,以便按钮之间没有空白空间,但在我目前的情况下这是不可能的。
目前,我已经成功循环浏览了我的子视图,并且可以检测到我何时/不在按钮之上。但是,当我不在按钮上时,如何检测最近的按钮?
谢谢!
这是我识别 UIButton 的代码:
- (UIView *)identifySubview:(NSSet *)touches {
CGPoint tapLocation = [[touches anyObject] locationInView:self.view];
for (UIView *view in [self.view viewWithTag:1000].subviews) {
for (UITableViewCell *cell in view.subviews) {
for (UIView *contentView in cell.subviews) {
for (UIButton *button in contentView.subviews) {
if ([button isKindOfClass:[UIButton class]]) {
CGRect trueBounds = [button convertRect:button.bounds toView:self.view];
if (CGRectContainsPoint(trueBounds, tapLocation)) {
return button;
} else {
//How would I detect the closest button?
}
}
}
}
}
}
return nil;
}
【问题讨论】:
-
按钮大小都一样吗?
标签: ios objective-c touch-event uipangesturerecognizer