如何从 UIGestureRecognizer 获取 UITouch 位置 [英] How to get UITouch location from UIGestureRecognizer

查看:28
本文介绍了如何从 UIGestureRecognizer 获取 UITouch 位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 UIGestureRecognizer 获取我的水龙头的 UITouch 位置,但我无法通过查看文档和其他 SO 问题来弄清楚如何.你们中的一个可以指导我吗?

I want to get the UITouch location of my tap from UIGestureRecognizer, but I can not figure out how to from looking at both the documentation and other SO questions. Can one of you guide me?

- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer
{
    CCLOG(@"Single tap");
    UITouch *locationOfTap = tapRecognizer; //This doesn't work

    CGPoint touchLocation = [_tileMap convertTouchToNodeSpace:locationOfTap];
    //convertTouchToNodeSpace requires UITouch

    [_cat moveToward:touchLocation];
}

此处已修复代码 - 这也修复了倒 Y 轴

CGPoint touchLocation = [[CCDirector sharedDirector] convertToGL:[self convertToNodeSpace:[tapRecognizer locationInView:[[CCDirector sharedDirector] openGLView]]]];

推荐答案

您可以在 UIGestureRecognizer 上使用 locationInView: 方法.如果为视图传递 nil,此方法将返回窗口中触摸的位置.

You can use the locationInView: method on UIGestureRecognizer. If you pass nil for the view, this method will return the location of the touch in the window.

- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer
{
    CGPoint touchPoint = [tapRecognizer locationInView: _tileMap]
}

还有一个有用的委托方法gestureRecognizer:shouldReceiveTouch:.只需确保实现并将您的点击手势的委托设置为 self.

There is also a helpful delegate method gestureRecognizer:shouldReceiveTouch:. Just make sure to implement and set your tap gesture's delegate to self.

保留对手势识别器的引用.

Keep a reference to the gesture recognizer.

@property UITapGestureRecognizer *theTapRecognizer;

初始化手势识别器

_theTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(someMethod:)];
_theTapRecognizer.delegate = self;
[someView addGestureRecognizer: _theTapRecognizer];

监听委托方法.

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    CGPoint touchLocation = [_tileMap convertTouchToNodeSpace: touch];
    // use your CGPoint
    return YES;
}

这篇关于如何从 UIGestureRecognizer 获取 UITouch 位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆