我有一个已知位置(CGPoint),我想查询视图(UIView)下面的对象或包含它的对象,无论是视图本身,还是其中的按钮,标签或任何其他实例
然后我想抓住那个对象,找出它的类型,并调用用户点击时发生的任何方法。
我尝试在视图上调用touchesBegan,但是没有办法创建触摸事件或者看起来似乎...如果我错了就纠正我。
我在想用hitTest可能有办法做到这一点,但我不确定。
答案 0 :(得分:0)
命中测试会做到这一点。
如果您不想返回某些内容,请关闭userInteractionEnabled
//Send touch down event
//Now, this is a bit hacky. Theres no public contrustors for UITouch or UIEvent, thus calling touches*: on the view is not possible. Instead, I search the view underneath it (with Hit Test) and call actions on that.
//Note. You need to allow for each type of object below, for the scope of the demo, I've allowed for only UIView and UIButton.
UIView *v = [[self view] hitTest:pointer.frame.origin withEvent:nil]; //origin is top left, point of pointer.
NSLog(@"%@", [v class] );
if([v isKindOfClass:[UIButton class]]){
selectedButton = (UIButton *)v;
}
else if ([v isKindOfClass:[UIView class]] && [[v backgroundColor] isEqual:[UIColor redColor]]){
selectedView = v;
dragLabel.text = @"You are dragging me!";
dragPoint = pointer.frame.origin; //record this point for later.
}
else {
NSLog (@"touched but no button underneath it");
}