3

我有一个简单的全屏 UIView 。当用户点击屏幕时,我需要写出 x,y

Console.WriteLine ("{0},{1}",x,y);

我需要为此使用什么 API?

4

2 回答 2

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

手势识别器很好,因为它们将触摸封装到可重用的类中。

于 2012-04-21T22:27:01.887 回答
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);
}
于 2012-04-21T18:48:09.917 回答