跟踪iPhone上的攻丝坐标
我想通过在屏幕上显示位置来移动UIImageView 。 我如何获得攻牙手指的X,Y坐标?
在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方法。
您可以使用以下任何方法
如果你想让它在被触及的开始事件上移动,请使用它
- (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坐标。 此外,你也可以使用动画来平滑移动。
链接地址: http://www.djcxy.com/p/30669.html