Track tapping coordinates on iPhone

I want to move UIImageView by tappig the location on the screen. How do I get X, Y coordinates of the tapping finger?


in the viewDidLoad.m, this could do the job :

/* 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];

And then in the handleTaps: method

- (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));
    }
}

Of course, you implement the handleTaps method regarding your needs.


You can use any of below methods

If you want it to be moved on touched begin Event, use this

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

If you want it to be moved on touches ended event, use this

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

and place the code pasted below

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

In Above code , the pt Contains the x and y co-ordinates of your touched location. Moreover you may also use animations for smoother moves.

链接地址: http://www.djcxy.com/p/30670.html

上一篇: 在模拟器上模拟双指单击

下一篇: 跟踪iPhone上的攻丝坐标