Ian Vink
Ian Vink

Reputation: 68770

Monotouch: Get x.y or tap on UIView

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

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

What API Do i need to use for that?

Upvotes: 3

Views: 2075

Answers (2)

Mike Bluestein
Mike Bluestein

Reputation: 706

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 could also use a UITapGestureRecognizer:

var tapRecognizer = new UITapGestureRecognizer ();

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

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

someView.AddGestureRecognizer(tapRecognizer);

Gesture recognizers are nice as they encapsulate touches into reusable classes.

Upvotes: 10

AMayes
AMayes

Reputation: 1767

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

Upvotes: 3

Related Questions