我想在拖动时从用户的手指获取坐标。我试过这段代码,但是它说坐标总是{0,0}, 怎么了?
- (IBAction)Drag{
UIPanGestureRecognizer *Recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragged)];
[self.view addGestureRecognizer:Recognizer];
}
-(void) dragged{
UITouch *touch ;
CGPoint location = [touch locationInView:touch.view];
NSLog(@"%@", NSStringFromCGPoint (location));
}
我也试过了NSLog(@"%.2f %.2f" location.x, location.y);
并得到了同样的结果。
感谢
答案 0 :(得分:0)
这是很正常的,你使用的是touch
而没有给它赋值。
手势识别器的动作采用一个参数,即识别器本身,而后者又有一个locationInView:
方法,所以你应该使用它。此外,您需要检查识别器的状态。最后,您可能不希望在需要时添加手势识别器,只需从头开始添加。
// probably in your viewDidLoad
UIPanGestureRecognizer *Recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(panGestureRecognizerAction:)];
[self.view addGestureRecognizer:Recognizer];
- (void)panGestureRecognizerAction:(UIPanGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateBegan ||
recognizer.state == UIGestureRecognizerStateChanged)
{
CGPoint location = [recognizer.state locationInView:touch.view];
NSLog(@"%@", NSStringFromCGPoint (location));
}
}