【问题标题】:Get the object at top of uiview hierarchy获取 uiview 层次结构顶部的对象
【发布时间】:2012-07-23 08:25:00
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: objective-c ipad views


    【解决方案1】:

    命中测试会做到这一点。

    如果您不希望返回某些内容,请关闭 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");
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-28
      • 1970-01-01
      • 2019-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多