Monotouch: get xy or tap UIView

I have a simple full screen UIView. When the user clicks on the screen, I need to write x, y

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

What API do I need to use for this?

+3
source share
2 answers

In MonoTouch (since you asked in C # ... although the previous answer is correct :) that would be:

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);
        // ...
}

You can also use UITapGestureRecognizer:

var tapRecognizer = new UITapGestureRecognizer ();

tapRecognizer.AddTarget(() => { 
    PointF pt = tapRecognizer.LocationInView (this.View);
    // ... 
});

tapRecognizer.NumberOfTapsRequired = 1;
tapRecognizer.NumberOfTouchesRequired = 1;

someView.AddGestureRecognizer(tapRecognizer);

Signs of gesture recognition are good because they encapsulate strokes in reusable classes.

+10
source
-(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);
}
+3
source

All Articles