我有一个 UIButton
,它只会在我点击其中一个非常小的子区域时对我的点击手势使用react。我是 assuming当我点击按钮内的其他地方时,可能另一个 View 正在吞下我的手势。
问题是.. 有没有办法知道哪个 UIView
正在接受点击?我知道我可以强行将所有手势处理程序附加到我的所有 View ..但看起来工作量太大。有什么想法吗?
更新: 艾哈迈德在下面的回答非常有效。我还添加了 UIView 的 recursiveDescription快速找到有问题的 View 。
也就是说,如果我在 NSLog 行上放置一个断点:
- (void)sendEvent:(UIEvent *)event
{
[super sendEvent:event];
UIView *touchReceipientView =((UITouch *)[event.allTouches anyObject]).view;
NSLog(@"View = %@ ",touchReceipientView);
}
我得到这个输出(作为例子):
(lldb) po [touchReceipientView recursiveDescription]
<UIView: 0x1fd16470; frame = (0 430; 320 40); layer = <CALayer: 0x1fd164d0>>
| <UIButton: 0x1fd13030; frame = (297 0; 18 19); opaque = NO; tag = 11; layer = <CALayer: 0x1fd130f0>>
| | <UIImageView: 0x1fd13190; frame = (-41 -40.5; 100 100); alpha = 0; opaque = NO; userInteractionEnabled = NO; tag = 1886548836; layer = <CALayer: 0x1fd131f0>>
| | <UIImageView: 0x1fd14d00; frame = (1 2; 16 15); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x1fdc8e50>>
这让我立即识别出有问题的 UIView!
最佳答案
您可以子类化 UIApplication 并查看谁在接收事件。
在 main.m 中,
@interface MyApplication : UIApplication
@end
@implementation MyApplication
- (void)sendEvent:(UIEvent *)event
{
[super sendEvent:event];
NSLog(@"View = %@", ((UITouch *)[event.allTouches anyObject]).view);
}
@end
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, NSStringFromClass([MyApplication class]), NSStringFromClass([MyAppDelegate class]));
}
}
关于iphone - 如何知道正在接收当前手势的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17695076/