我想从UIGestureRecognizer获取我的点击的UITouch位置,但我无法通过查看文档和其他SO问题来确定该如何操作.你们谁能给我指路吗?

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

FIXED CODE HERE - THIS ALSO FIXES INVERTED Y AXIS

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

推荐答案

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

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

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

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

@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;
}

Objective-c相关问答推荐

UIAlertController:supportedInterfaceOrientations 被递归调用

Objective-C 对象的列表 Select 器

如何使用格式化占位符本地化字符串?

UILabel:你如何设置字体大小?

iOS performSelectorOnMainThread 有多个参数

如何在 iPhone 应用程序的 UINavigationBar 中添加图像

UIRefreshControl iOS 6 xcode

如何切换回已经加载的 UIViewController?

ios 11 UITabBar UITabBarItem 定位问题

如何删除应用程序可访问的所有 keys 串项目?

在字符串Objective-c中连接字符串

以编程方式访问应用标识符前缀

iOS Buttons - 添加边框

添加/删除带有动画的 UITableViewCell?

Objective-C构建中的重复符号错误?

如何本地化 iOS 故事板

在 UITableView 的 iOS 文档目录中列出保存的文件?

使 UILabel 的文本加粗

Xcode 的不完整实现警告

哪个是正确的,nil 或 NULL,来标记没有 Objective-C 块?