跟踪iPhone上的攻丝坐标

时间:2012-03-03 09:28:58

标签: iphone objective-c ios

我想通过点击屏幕上的位置来移动 UIImageView 。如何获得敲击手指的X,Y坐标?

2 个答案:

答案 0 :(得分:0)

在viewDidLoad.m中,这可以完成这项工作:

/* Create the Tap Gesture Recognizer */
self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTaps:)];

/* The number of fingers that must be on the screen */
self.tapGestureRecognizer.numberOfTouchesRequired = 1;
/* The total number of taps to be performed before the gesture is recognized */
self.tapGestureRecognizer.numberOfTapsRequired = 1;

/* Add this gesture recognizer to our view */
[self.view addGestureRecognizer:self.tapGestureRecognizer];

然后在handleTaps:方法

- (void) handleTaps:(UITapGestureRecognizer*)paramSender{
    NSUInteger touchCounter = 0;
    for (touchCounter = 0; touchCounter < paramSender.numberOfTouchesRequired; touchCounter++)
    {
        CGPoint touchPoint = [paramSender locationOfTouch:touchCounter inView:paramSender.view];
        NSLog(@"Touch #%lu: %@", (unsigned long)touchCounter+1, NSStringFromCGPoint(touchPoint));
    }
}

当然,您可以根据需要实施handleTaps方法。

答案 1 :(得分:0)

您可以使用以下任何一种方法

如果您希望在触摸的开始事件中移动它,请使用此

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{}

如果您希望在触摸结束事件中移动它,请使用此

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{}

并将代码粘贴在

下面
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
        CGPoint pt = [[touches anyObject] locationInView:self.view];
        imageView.center = CGPointMake(pt.x,pt.y);
}

在上面的代码中,pt包含触摸位置的x和y坐标。 此外,您还可以使用动画来实现更平滑的动作。