Ios 用CGPoint拖动坐标

Ios 用CGPoint拖动坐标,ios,drag,gesture,cgrect,cgpoint,Ios,Drag,Gesture,Cgrect,Cgpoint,我想在拖动时从用户的手指获取坐标。我试过这个代码,但它说坐标总是{0,0}, 怎么了 - (IBAction)Drag{ UIPanGestureRecognizer *Recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragged)]; [self.view addGestureRecognizer:Recognizer]; } -(void) dragged{ UITouch

我想在拖动时从用户的手指获取坐标。我试过这个代码,但它说坐标总是{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)并且得到了相同的结果。

谢谢

这很正常,您使用的是
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));
    }
}

工作,非常感谢你!