Ios 获取当两个或多个视图重叠时设置动画时触摸的可见按钮?

Ios 获取当两个或多个视图重叠时设置动画时触摸的可见按钮?,ios,uibutton,touch,Ios,Uibutton,Touch,我正在以编程方式生成几个UIButton,然后使用块动画为它们设置动画。我能够通过在中实现代码(如下所示)来确定触动了哪个按钮 我现在的问题是,图像可能重叠,因此当给定触摸位置有多个视图时,ToucheSBegint中的代码会拉出错误的按钮(即,在我正在触摸的可见按钮下方获取图像) 我想使用[touch view]与屏幕上的UIButtons进行比较: if (myButton==[touch view]) { ... 但这种比较总是失败的 我的接触开始了: -(void)touchesBeg

我正在以编程方式生成几个UIButton,然后使用块动画为它们设置动画。我能够通过在中实现代码(如下所示)来确定触动了哪个按钮

我现在的问题是,图像可能重叠,因此当给定触摸位置有多个视图时,ToucheSBegint中的代码会拉出错误的按钮(即,在我正在触摸的可见按钮下方获取图像)

我想使用[touch view]与屏幕上的UIButtons进行比较:

if (myButton==[touch view]) { ...
但这种比较总是失败的

我的接触开始了:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    for (UIButton *brain in activeBrains) {
        //Works, but only when buttons do not overlap
        if ([brain.layer.presentationLayer hitTest:touchLocation]) {
            [self brainExplodes:brain];
            break;
        }

        /* Comparison always fails
        if (brain == [touch view]) {
            [self brainExplodes:brain];
            break;
        }
        */
    }
}

因此,我的问题是如何确定重叠图像中的哪一个在其他图像之上?

我在这里的代码中做了一些假设,但本质上,您需要获得所有已触摸按钮的列表,然后找到“在顶部”的按钮。在子视图数组中,位于顶部的按钮的索引应该是最高的

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
    NSMutableArray *brainsTouched = [[NSMutableArray alloc] init];
    for (UIButton *brain in activeBrains) {
        //Works, but only when buttons do not overlap
        if ([brain.layer.presentationLayer hitTest:touchLocation]) {
            [brainsTouched addObject:brain];
        }
    }
    NSUInteger currentIndex;
    NSInteger viewDepth = -1;
    UIButton *brainOnTop;
    for (UIButton *brain in brainsTouched){
        currentIndex = [self.view.subviews indexOfObject:brain];
        if (viewDepth < currentIndex){ 
            brainOnTop = brain;
            viewDepth = currentIndex;
        }
    }
    [self brainExplodes:brainOnTop];
}
-(void)touchesbeated:(NSSet*)toucheevent:(UIEvent*)event{
UITouch*touch=[触摸任何对象];
CGPoint-touchLocation=[touch-locationInView:self.view];
NSMutableArray*头脑风暴=[[NSMutableArray alloc]init];
用于(UIButton*活动大脑中的大脑){
//工作,但只有当按钮不重叠时
if([brain.layer.presentationLayer hitTest:touchLocation]){
[脑力触碰对象:大脑];
}
}
整数当前索引;
NSInteger viewDepth=-1;
UIButton*脑顶;
用于(UIButton*头脑风暴中的大脑){
currentIndex=[self.view.subviews indexOfObject:brain];
如果(viewDepth

另外,我在编辑窗口中输入了这个,请原谅我的输入错误

UIView类包含一个标记属性,可以使用该属性用整数值标记各个视图对象。您可以使用标记来唯一标识视图层次结构中的视图,并在运行时对这些视图执行搜索。(基于标记的搜索比自己迭代视图层次更快。)标记属性的默认值为0


要搜索带标记的视图,请使用UIView的viewWithTag:method。此方法对接收器及其子视图执行深度优先搜索。它不会搜索超级视图或视图层次结构的其他部分。因此,从层次结构的根视图调用此方法会搜索层次结构中的所有视图,但从特定子视图调用此方法只会搜索视图的子集。

感谢@Aaron提供了一个好的解决方案。我确实根据我的情况重构了你的答案,以获得一个不可察觉的性能增益(wee),但更重要的是,我认为,如果我将来必须重构,阅读的内容会更少

我想,回想起来这很明显,但当然activeBrains数组反映了子视图的顺序(因为每个新的大脑都是在添加到超级视图之后添加到数组中的)。因此,只要简单地在阵列中向后循环,正确的大脑就会爆炸

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    for(int i=activeBrains.count-1; i>=0; i--) {
        UIButton *brain = [activeBrains objectAtIndex:i];

        if ([brain.layer.presentationLayer hitTest:touchLocation]) {
            [self explodeBrain:brain];
            break;
        }
    }
}

您所有的按钮用户交互和触摸都已启用吗?是的,触摸已启用。正如我所说,我已经让触摸事件工作,我只是在按钮重叠的情况下,正确按钮上的触摸事件有问题。我考虑过使用标签,但我不知道在这种情况下它们如何工作。这个解决方案产生了同样的问题:当两个或多个视图重叠时,如何在设置动画时触动可视UIButton的标记?没错。奇怪的是,通常情况下,视图可以很好地处理触摸,并确定触摸必须转发到哪个视图。我再想想