gpt4 book ai didi

ios - 获取当 2 个或多个 View 重叠时动画时触摸的可见 UIButton?

转载 作者:行者123 更新时间:2023-11-29 04:40:27 25 4
gpt4 key购买 nike

我以编程方式生成几个 UIButton,然后使用 block 动画对它们进行动画处理。我可以通过实现 this answer 中的代码来确定触摸了哪个按钮。 (如下所示)。

我现在的问题是图像可以重叠,因此当给定触摸位置有超过 1 个 View 时,touchesBegan 中的代码会拉出错误的按钮(即,获取我所在的可见按钮下方的图像)接触)。

我想使用[触摸 View ]来与屏幕上的 UIButton 进行比较:

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;
}
*/
}
}

所以我的问题是如何确定哪一个重叠图像位于其他图像之上?

最佳答案

我在我的代码中做了一些假设,但本质上您需要获取所有已触摸按钮的列表,然后找到“顶部”的按钮。顶部的按钮应该具有 subview 数组中按钮的最高索引。

-(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];
}

此外,我在编辑窗口中输入了此内容,因此请原谅拼写错误。

关于ios - 获取当 2 个或多个 View 重叠时动画时触摸的可见 UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10474671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com