如何访问 UITouch 位置在窗口中的坐标

发布于 2025-01-04 10:08:18 字数 1022 浏览 6 评论 0原文

我正在使用 Cocos2D 开发一个迷你 iPhone 游戏。我想检测精灵的触摸。为此,我决定不对 CCSprite 类进行子类化,而是使用图层类中的触摸事件:

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CCLOG(@"touch began...");
    CCSprite *particularSprite = [self getChildByTag:artSprite];
    CCNode *nodeClass = (CCNode*) particularSprite;
    CGRect DesiredSprite = CGRectMake(nodeClass.positionInPixels.x,  nodeClass.positionInPixels.y,particularSprite.contentSize.width  , particularSprite.contentSize.height);

    for (UITouch *myTouch in touches) {
        CGPoint touchPosition = [myTouch locationInView: [myTouch view]];
        if(CGRectContainsPoint(DesiredSprite ,touchPosition ))
        {
            CCLOG(@"Sprite touched");
        }
    }
}

不幸的是,坐标是错误的。 locationInView 对其进行了不同的翻译。我正在使用左横向视图(kCCDeviceOrientationLandscapeLeft)。

在函数上添加断点并查看 myTouch 变量,然后我看到它有一个名为 locationInWindow 的成员变量,它反映了实际的触摸位置(这就是我想要的)。

我尝试访问 locationInWindow 但没有 getter 方法。我怎样才能这样做呢?

非常感谢和最诚挚的问候

I am using Cocos2D to develop a mini iPhone game.. I wanted to detect the touch of a sprite. To do so I decided not to subclass the CCSprite class but instead using the touch events in the layer class:

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CCLOG(@"touch began...");
    CCSprite *particularSprite = [self getChildByTag:artSprite];
    CCNode *nodeClass = (CCNode*) particularSprite;
    CGRect DesiredSprite = CGRectMake(nodeClass.positionInPixels.x,  nodeClass.positionInPixels.y,particularSprite.contentSize.width  , particularSprite.contentSize.height);

    for (UITouch *myTouch in touches) {
        CGPoint touchPosition = [myTouch locationInView: [myTouch view]];
        if(CGRectContainsPoint(DesiredSprite ,touchPosition ))
        {
            CCLOG(@"Sprite touched");
        }
    }
}

Unfortunately the coordinates are wrong. The locationInView translates it differently. I am using the landscapeleft view (kCCDeviceOrientationLandscapeLeft).

Adding a breakpoint on the function and looking at the myTouch variable, then I see that it has a member variable called locationInWindow which reflects the actual touch position (which is what I want).

I tried to access to the locationInWindow but there is no getter method for it. How can I do so?

Many thanks and Best Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

末が日狂欢 2025-01-11 10:08:18

Window 是一个UIWindow,它是UIView 子类。另外UITouch还有一个window属性。所以你可以尝试:

CGPoint touchPosition = [myTouch locationInView:myTouch.window];

视图的转换数字到计算中;因此,您可能还想尝试使用 self.superview 作为参数。

Window is a UIWindow which is a UIView subclass. Additionally UITouch has a window property. So you could try:

CGPoint touchPosition = [myTouch locationInView:myTouch.window];

The view's transform figures into the calculation; So you might also want to try self.superview for the parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文