我有一个简单的全屏UIView。当用户点击屏幕时,我需要写出x,y
Console.WriteLine ("{0},{1}",x,y);
我需要使用哪种API?
答案 0 :(得分:10)
在MonoTouch中(因为你在C#中问过......虽然前一个答案是正确的:)但是:
public override void TouchesBegan (NSSet touches, UIEvent evt)
{
base.TouchesBegan (touches, evt);
var touch = touches.AnyObject as UITouch;
if (touch != null) {
PointF pt = touch.LocationInView (this.View);
// ...
}
你也可以使用UITapGestureRecognizer:
var tapRecognizer = new UITapGestureRecognizer ();
tapRecognizer.AddTarget(() => {
PointF pt = tapRecognizer.LocationInView (this.View);
// ...
});
tapRecognizer.NumberOfTapsRequired = 1;
tapRecognizer.NumberOfTouchesRequired = 1;
someView.AddGestureRecognizer(tapRecognizer);
手势识别器非常好,因为它们将触摸封装到可重用的类中。
答案 1 :(得分:3)
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
printf("Touch at %f , %f \n" , [touch locationInView:self.view].x, [touch locationInView:self.view].y);
}