2014-02-17 198 views
0

我有一個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上建議的內容,但仍然沒有成功。

回答

0

發現問題,這可能是我長時間遇到的最愚蠢的事情!

要讓滑動手勢在滾動視圖中工作,您必須先包含您想在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作爲它的子視圖。