How to get UITouch location from UIGestureRecognizer(如何从 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 位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 UIGestureRecognizer 获取 UITouch 位置
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01