1

我有一个已知位置(CGPoint),我想查询它下面或包含它的对象的视图(UIView),无论是视图本身,还是其中的按钮,或者标签或任何其他实例

然后我想抓住那个对象,找出它的类型,并调用用户点击它时发生的任何方法。

我尝试在视图上调用 touchesBegan,但似乎无法创建触摸事件或 uievents……如果我错了,请纠正我。

我在想可能有一种方法可以用 hitTest 来做到这一点,但我不确定。

4

1 回答 1

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");
        }
于 2012-08-21T19:39:36.840 回答