UamacrollView内部的Xamarin IOS UILabel Tap手势无法正常工作

时间:2016-08-25 16:13:30

标签: ios uiscrollview xamarin.ios

我有一个UIScrollView,在里面我有一个UILabel。我需要检测UILabel的轻击手势事件。目前,它不起作用。我是xamarin IOS的​​新手,请帮忙。提前谢谢。

这是我的代码

UILabel lViewallLabel = new UILabel(new CGRect((View.Bounds.Width / 2) - 20, 270, View.Bounds.Width / 2, 16));
lViewallLabel.Text = "VIEW ALL >>";
lViewallLabel.TextAlignment = UITextAlignment.Right;
lViewallLabel.TextColor = UIColor.White;
lViewallLabel.Font = UIFont.SystemFontOfSize(12f);
lViewallLabel.AdjustsFontSizeToFitWidth = true;
lViewallLabel.UserInteractionEnabled = true;
UITapGestureRecognizer tgrLabel2 = new UITapGestureRecognizer(() =>
       {
             UIAlertView myAlert = new UIAlertView();
             myAlert.AddButton("OK");
             myAlert.Message = "Label was tapped.";
             myAlert.Title = "It worked!";
             myAlert.Show();
        });
lViewallLabel.AddGestureRecognizer(tgrLabel2);
innerView.AddSubview(lViewallLabel);

scrollView.AddSubview(innerView);

1 个答案:

答案 0 :(得分:3)

我使用下面的代码解决了这个问题。代码变化很小,我已经将UILabel直接添加到UIScrollView而不是innerview。

UITapGestureRecognizer singleTap = new UITapGestureRecognizer();
singleTap.CancelsTouchesInView = false;
scrollView.AddGestureRecognizer(singleTap);

scrollView.AddSubview(lViewallLabel);

post帮我解决了这个问题。