Monotouch:获取x.y或点击UIView

时间:2012-04-21 18:28:21

标签: iphone ios uiview xamarin.ios monodevelop

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

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

我需要使用哪种API?

2 个答案:

答案 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);
}