Monotouch:获取x.y或点击UIView
问题描述:
我有一个简单的全屏UIView。当用户在屏幕上点击时,我需要写出x,yMonotouch:获取x.y或点击UIView
Console.WriteLine ("{0},{1}",x,y);
什么API我需要使用它吗?
答
在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);
手势识别器很好,因为它们将触摸封装到可重用类中。
答
-(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);
}