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

Gesture Recognizer は、タッチを再利用可能なクラスにカプセル化するので便利です。

于 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 に答える