gpt4 book ai didi

iphone - iOS - 拖放碰撞检测如何检测您选择的项目何时拖过另一个 subview ?

转载 作者:可可西里 更新时间:2023-11-01 03:06:06 27 4
gpt4 key购买 nike

我们正在将拖放功能添加到一个为玩家提供位置的运动场中。

位置是使用 Interface Builder 映射出来的,每个位置都是一个单独的 UIImageView。

我们希望能够将球员图像从屏幕一侧的板凳位置拖到场上的位置上。

我们如何才能最好地检测正在移动的选定玩家何时与现有 gamePosition imageView 发生碰撞?

我们正在寻找一种方法来检测当前位置下是否有 View 或ImageView。

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
tile1.center = location;

if gamePositionExistsAtCurrentLocation(location) { //want something like this
[tile1 setBackgroundColor:[UIColor blueColor]];
} else {
[tile1 setBackgroundColor:[UIColor yellowColor]];
}
}

最佳答案

检查你正在移动的项目的框架是否与你的 subview 的框架相交

for (UIView *anotherView in self.subviews) {
if (movingView == anotherView)
continue;
if (CGRectIntersectsRect(movingView.frame, anotherView.frame)) {
// Do something
}
}

如果我是你,我会将所有与游戏相关的项目添加到一个 NSArray 中,并通过它进行循环。因此,您不会检测到与您的游戏无关的 subview (如标签等)的碰撞。

关于iphone - iOS - 拖放碰撞检测如何检测您选择的项目何时拖过另一个 subview ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3947219/

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