我想从 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];
}
此处已修复代码 - 这也修复了倒 Y 轴
CGPoint touchLocation = [[CCDirector sharedDirector] convertToGL:[self convertToNodeSpace:[tapRecognizer locationInView:[[CCDirector sharedDirector] openGLView]]]];
最佳答案
您可以在 UIGestureRecognizer 上使用 locationInView:
方法。如果为 View 传递 nil,此方法将返回窗口中触摸的位置。
- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer
{
CGPoint touchPoint = [tapRecognizer locationInView: _tileMap]
}
还有一个有用的委托(delegate)方法gestureRecognizer:shouldReceiveTouch:
。只需确保实现并将您的点击手势的委托(delegate)设置为 self。
保留对手势识别器的引用。
@property UITapGestureRecognizer *theTapRecognizer;
初始化手势识别器
_theTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(someMethod:)];
_theTapRecognizer.delegate = self;
[someView addGestureRecognizer: _theTapRecognizer];
监听委托(delegate)方法。
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
CGPoint touchLocation = [_tileMap convertTouchToNodeSpace: touch];
// use your CGPoint
return YES;
}
关于ios - 如何从 UIGestureRecognizer 获取 UITouch 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16618109/