UISwipeGestureRecogniser无法识别

时间:2014-02-17 17:39:56

标签: ios6 ios7 uiscrollview xamarin.ios uigesturerecognizer

我有一个UIScrollView,我正在创建并添加包含简单LINQ查询结果的UILabel。

我在循环中设置了一个生成标签的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的建议,但仍然没有成功。

1 个答案:

答案 0 :(得分: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上,并将标签作为子视图。