2014-02-17 4 views
0

간단한 LINQ 쿼리의 결과가 포함 된 UILabels을 만들고 추가하는 UIScrollView가 있습니다.UISwipeGestureRecogniser가 인식되지 않습니다.

레이블을 생성하는 루프 내에서 UISwipeGestureRecognizer를 설정했습니다 (iOS: issues with UIGestureRecognisers vs Subviews은 각 컨트롤마다 새로운 인식기가 필요하다고 말합니다. 인식기를 추가하기 위해 UIImageView와 동일한 UILabel을 사용할 수 있다고 가정하고 있습니다). 인식자를 레이블에 추가했습니다.

UIScrollView가 포함 된보기가 시작되면 scrollview가 예상대로 작동하지만 스 와이프가 작동하지 않습니다.

경고 : 시뮬레이터에서만 시도했지만 내 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에서 추가 할 내용을 모두 포함시킨 다음 스크롤보기에 추가해야합니다.

따라서 작업 할 수있는 ScrollView 내에서 슬쩍을 얻으려면, 당신은 단지 UILabel의를 추가하고 그에 슬쩍 행위를 할 수없는 다음과 같은

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; 
     } 
    } 

을 할 필요가, 그것은에 있어야한다 라벨이있는 하위 뷰인 UIView입니다.

관련 문제