2011-05-24 26 views

回答

30

有两种方法可以完成此操作。如果你已经得到了你使用的UIView的子类,你可以重写-touchesEnded:withEvent:方法上的子类,像这样:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *aTouch = [touches anyObject]; 
    CGPoint point = [aTouch locationInView:self]; 
    // point.x and point.y have the coordinates of the touch 
} 

如果你还没有子类的UIView,虽然和视图是由视图控制器拥有或什么的,那么你可以使用一个UITapGestureRecognizer,像这样:

// when the view's initially set up (in viewDidLoad, for example) 
UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)]; 
[someView addGestureRecognizer:rec]; 
[rec release]; 

// elsewhere 
- (void)tapRecognized:(UITapGestureRecognizer *)recognizer 
{ 
    if(recognizer.state == UIGestureRecognizerStateRecognized) 
    { 
     CGPoint point = [recognizer locationInView:recognizer.view]; 
     // again, point.x and point.y have the coordinates 
    } 
} 
+1

touchesEnded:withEvent:也可以在UIViewController中使用,因为这也是从UIResponder派生的。 – taskinoor 2011-05-24 16:54:35

+0

谢谢你们......这给了一个好的开始! – jdl 2011-05-24 20:36:13

2

我假设你的意思的手势识别(和触摸)。开始寻找这样一个广泛问题的最佳地点是Apple的示例代码Touches。它遍历了大量的信息。

+0

谢谢你的帮助。 – jdl 2011-05-24 20:39:45

2
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint touchPoint = [touch locationInView:myView]; 
    NSLog("%lf %lf", touchPoint.x, touchPoint.y); 
} 

你需要做这样的事情。 touchesBegan:withEvent:UIResponder的一种方法,其中UIViewUIViewController都是从中导出的。如果你谷歌这种方法,那么你会发现几个教程。 MoveMe来自苹果的样品是一个很好的例子。

+0

谢谢你的帮助。 – jdl 2011-05-24 20:38:35

2
func handleFrontTap(gestureRecognizer: UITapGestureRecognizer) { 
    print("tap working") 
    if gestureRecognizer.state == UIGestureRecognizerState.Recognized 
    { 
     `print(gestureRecognizer.locationInView(gestureRecognizer.view))` 
    } 
} 
0

斯威夫特3回答

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapAction(_:))) 
yourView.addGestureRecognizer(tapGesture) 


func tapAction(_ sender: UITapGestureRecognizer) { 

     let point = sender.location(in: yourView) 


}