2012-03-03 55 views
0

我想移動UIImageView通過tappig屏幕上的位置。我如何獲得攻牙手指的X,Y座標?在iPhone上跟蹤攻絲座標

在viewDidLoad.m
+2

[相同](http://www.google.fr/search?aq=f&sourceid=chrome&ie=UTF-8&q=uiimageview+tap) – 2012-03-03 09:30:34

回答

0

,這可能做的工作:

/* 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方法。

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座標。 此外,您也可以使用動畫來平滑移動。

+0

它很簡單,爲什麼你不搜索谷歌或stackoverflow? – HarshIT 2012-03-03 09:43:21