问题描述
我想从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];
}
固定码在这里 - AXIS
CGPoint touchLocation = [[CCDirector sharedDirector] convertToGL:[self convertToNodeSpace:[tapRecognizer locationInView:[[CCDirector sharedDirector] openGLView]]]];
推荐答案
您可以使用 locationInView :
在UIGestureRecognizer上的方法。如果您为视图传递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:
。
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位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!