UISwipeGestureRecogniser未被识别

UISwipeGestureRecogniser未被识别

问题描述:

我有一个UIScrollView,我在其中创建并添加包含简单LINQ查询结果的UILabels。UISwipeGestureRecogniser未被识别

我已成立了一个UISwipeGestureRecognizer产生标签内环路(iOS: issues with UIGestureRecognisers vs Subviews说我需要为每个控制的新识别器 - 我假设我可以用一个UILabel一样的UIImageView做添加识别器),然后将识别器添加到标签。

当包含UIScrollView的视图开始时,滚动视图按预期工作,但滑动不是。

警告:只有在模拟器上试过这个,我的iPhone正在动作。

private void CreateViewHistory() 
    { 
     float xtext = 4f; 
     float y = 4f; 
     int c = 0; 
     var tv = tank.Events.OrderByDescending(t => t.Date).ToList(); 
     tbiHistClearAll.Enabled = enableDelete; 
     //tgrRemove.AddTarget(this, new Selector("screenSwipe")); 
     //pgrRemove.DelaysTouchesBegan = true; 
     foreach (var e in tv) 
     { 

      var tgrRemove = new UISwipeGestureRecognizer() 
      { 
       NumberOfTouchesRequired = 1, 
       Direction = UISwipeGestureRecognizerDirection.Right, 
      }; 

      tgrRemove.AddTarget(this, new Selector("screenSwipe")); 

      svHistoryEvents.PanGestureRecognizer.RequireGestureRecognizerToFail(tgrRemove); 
      string info = string.Format("{0} - {1}{2} ({3})\n{4} - {5}\n{6} - {7}\n{8} - {9}", e.EventType, e.Volume, AppDelegate.Self.db.getShortVolumeUnits(), e.Date.ToShortDateString(), 
           StringUtils.GetString("Sowcrop.Implement"), e.Implement != null ? e.Implement.ImplementType : StringUtils.GetString("Common.NonRecorded"), 
           StringUtils.GetString("Common.User"), StringUtils.GetString("Common.NonRecorded"), 
           StringUtils.GetString("Common.Notes"), !string.IsNullOrEmpty(e.Notes) ? e.Notes : StringUtils.GetString("Common.NonRecorded")); 
      var lbl = new UILabel() 
      { 
       UserInteractionEnabled = true 
      }; 

      lbl = UICreation.MakeLabelWithTag(svHistoryEvents, new RectangleF(xtext, y, 320f, 90f), info, UITextAlignment.Left, UIColor.Black, false, 4, c); 
      lbl.AddGestureRecognizer(tgrRemove); 
      svHistoryEvents.AddSubview(lbl); 
      lblTemp.Add(lbl); 
      c++; 
      y += 94f; 
     } 
     UIUtils.ResizeScrollView(svHistoryEvents); 
    } 

    [Export("screenSwipe")] 
    public void SwipeRemove(UIGestureRecognizer s) 
    { 
     var swipe = s as UIGestureRecognizer; 
     var tv = tank.Events.OrderByDescending(t => t.Date).ToList(); 
     var txt = swipe.View as UILabel; 
     switch (swipe.State) 
     { 
      case UIGestureRecognizerState.Began: 
       Console.WriteLine("Swipe began"); 
       break; 
      case UIGestureRecognizerState.Changed: 
       Console.WriteLine("Swipe changed"); 
       tv.RemoveAt(txt.Tag); 
       CreateViewHistory(); 
       break; 
      case UIGestureRecognizerState.Ended: 
       Console.WriteLine("Swipe ended"); 
       break; 
      case UIGestureRecognizerState.Cancelled: 
       Console.WriteLine("Swipe cancelled"); 
       break; 
     } 
    } 

MakeLabelWithTag生成一个UILabel然后可以添加到滚动视图。

我在这里错过了什么,或者我需要做一些特别的事情,因为标签被保存在滚动视图中?

我也尝试过在UISwipeGestureRecogniser in a UIScrollView上建议的内容,但仍然没有成功。

发现问题,这可能是我长时间遇到的最愚蠢的事情!

要让滑动手势在滚动视图中工作,您必须先包含您想在UIView中添加的任何内容,然后将其添加到滚动视图中。

要因此获取滚动内刷卡上班,你需要做以下

private void CreateViewHistory() 
    { 
     foreach (var i in svHistoryEvents.Subviews) 
      if (i is UIView) 
       i.RemoveFromSuperview(); 
     float xtext = 4f; 
     float y = 4f; 
     int c = 0; 
     tbiHistClearAll.Enabled = enableDelete; 
     foreach (var e in tv) 
     { 

      var tgrRemove = new UISwipeGestureRecognizer() 
      { 
       NumberOfTouchesRequired = 1, 
       Direction = UISwipeGestureRecognizerDirection.Right, 
      }; 

      tgrRemove.AddTarget(this, new Selector("screenSwipe")); 
      var view = new UIView(new RectangleF(xtext, y, 320f, 90f)); 
      svHistoryEvents.PanGestureRecognizer.RequireGestureRecognizerToFail(tgrRemove); 
      string info = string.Format("{0} - {1}{2} ({3})\n{4} - {5}\n{6} - {7}\n{8} - {9}", e.EventType, e.Volume, AppDelegate.Self.db.getShortVolumeUnits(), e.Date.ToShortDateString(), 
           StringUtils.GetString("Sowcrop.Implement"), e.Implement != null ? e.Implement.ImplementType : StringUtils.GetString("Common.NonRecorded"), 
           StringUtils.GetString("Common.User"), StringUtils.GetString("Common.NonRecorded"), 
           StringUtils.GetString("Common.Notes"), !string.IsNullOrEmpty(e.Notes) ? e.Notes : StringUtils.GetString("Common.NonRecorded")); 
      var lbl = new UILabel() 
      { 
       UserInteractionEnabled = true 
      }; 

      lbl = UICreation.MakeLabelWithTag(svHistoryEvents, new RectangleF(0, 0, 320f, 90f), info, UITextAlignment.Left, UIColor.Black, false, 4, c); 
      view.AddGestureRecognizer(tgrRemove); 
      view.AddSubview(lbl); 
      svHistoryEvents.AddSubview(view); 
      lblTemp.Add(lbl); 
      c++; 
      y += 94f; 
     } 
     UIUtils.ResizeScrollView(svHistoryEvents); 
    } 

    [Export("screenSwipe")] 
    public void SwipeRemove(UIGestureRecognizer s) 
    { 
     var swipe = s as UIGestureRecognizer; 
     var txt = swipe.View.Subviews[0] as UILabel; 
     switch (swipe.State) 
     { 
      case UIGestureRecognizerState.Began: 
       Console.WriteLine("Swipe began"); 
       break; 
      case UIGestureRecognizerState.Changed: 
       Console.WriteLine("Swipe changed"); 
       break; 
      case UIGestureRecognizerState.Ended: 
       Console.WriteLine("Swipe ended"); 
       tv.RemoveAt(txt.Tag); 
       CreateViewHistory(); 
       break; 
      case UIGestureRecognizerState.Cancelled: 
       Console.WriteLine("Swipe cancelled"); 
       break; 
     } 
    } 

它不可能只添加一个UILabel和对的刷卡行为,它必须是对带有标签的UIView作为它的子视图。