gpt4 book ai didi

ios - 检测圆形 SKShapeNode 中的触摸?

转载 作者:行者123 更新时间:2023-12-01 16:40:43 25 4
gpt4 key购买 nike

我需要检测圆形的触摸,问题是虽然形状看起来像圆形,但可触摸区域仍然是矩形。仅当用户触摸我的圈子时,我如何检测触摸?

这就是我现在正在做的事情:

 // Here I add the Shape
_circle = [SKShapeNode node];
_circle.name = @"circle";
_circle.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[_circle setPath:CGPathCreateWithRoundedRect(CGRectMake(-70, -70, 140, 140), 70, 70, nil)];
_circle.strokeColor = _circle.fillColor = [UIColor colorWithRed:255.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:1.0];
[self addChild:_circle];
//...

// Listening to touches
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
if (CGRectContainsPoint(_circle.frame, location)) {
NSLog(@"Circle is touched");
}
}

最佳答案

如果您正在处理一个实际的圆,您可以使用数学来获得您想要的结果。您可以只计算从圆心到接触点的距离。如果该距离小于圆的半径,则您的触摸有效。

- (bool)pressed:(UITouch*) touch
{
CGPoint location = [touch locationInNode:self];
float hyp = sqrtf(powf(_button.position.x - location.x,2) + powf(_button.position.y - location.y,2));
if(hyp < _button.frame.size.width/2)
{
return true;
} else {
return false;
}
}

但是,对于圆角矩形,此解决方案将不起作用。

但是,您可以通过使用 SpritKit 的物理特性来采取不同的方法。你要做的是创建一个带有物理体的透明节点,并让它移动到触摸位置。这应该会给您的节点和按钮之间的冲突。

当然你的按钮也需要一个合适的物理体。

我认为这不是您希望的理想解决方案,但目前对于 SpriteKit 来说还没有一个简单且性能友好的解决方案。

更新

想再增加一个选项,这同样不理想,但可以完全消除形状之外的触摸。然而,它有一种相反的问题,因为一些边缘不会是触摸 react 的。

您可以做的是创建一个辅助触摸对象,该对象是一个普通矩形,尽可能大而不超出您的圆角矩形,并使用它来进行触摸检测。

就像我说的那样,你会遇到相反的问题,因为你的触摸矩形没有覆盖的外部区域不会是触摸 react 的。根据具体情况,此功能可能需要也可能不需要。只是想我会把它扔在那里,让你决定。

关于ios - 检测圆形 SKShapeNode 中的触摸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24714292/

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