【问题标题】:iOS UIScrollView scrolling detectioniOS UIScrollView 滚动检测
【发布时间】:2015-06-04 13:23:38
【问题描述】:

我正在开发一种带有自定义功能的 UITextView 副本。现在我需要实现选择光标(参见下面原始UITextView 的屏幕截图)

正如我从Debug View Hierarchy 发现的那样,Apple 开发人员在单独的窗口上绘制这些点以避免剪辑,当UIScrollView 开始拖动时,他们将这些点移动到UITextView 内,当停止拖动时,他们将其移回单独的窗口。这种方法的唯一问题是我如何检测我的一些TextView superviewUIScrollView 并且它们开始/结束滚动?为每个UIScrollView-type superviews 设置委托看起来很糟糕,并且会带来很多麻烦,因为如果需要,我将需要管理多个委托(甚至检测那里的变化)。有什么想法吗?

【问题讨论】:

    标签: ios objective-c iphone uiscrollview


    【解决方案1】:
    /*
     In your viewDidLoad or where ever you create the UITextView call this :[self checkParentViewOfTextView:textField];
    */
    
    -(void)checkParentViewOfTextView:(UITextView*)txv {
        if ([txv.superview isKindOfClass:[UIScrollView class]]) { // Check if the superview if UIScrollView
            UIScrollView *superScroll =(UIScrollView*) txv.superview;
            superScroll.delegate = self;// In order to call the delegate methods below
            superScroll.tag = 5; // Set a tag to access the current scrollView at these delegate methods
        }
    }
    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
        //Any scrollView did begin scrolling
        if (scrollView.tag == 5) {
            //Actions for your scrollView
        }
    }
    -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
        //Any scrollView did end scrolling
        if (scrollView.tag == 5) {
            //Actions for your scrollView
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以对所有 UIScrollView 使用相同的滚动视图委托。

      scrollView1.delegate = self
      scrollView2.delegate = self
      etc...
      

      只需实现委托方法并在必要时为每个滚动视图采取不同的操作。通过引用类中的属性或设置标签来做到这一点。

      func scrollViewDidScroll(scrollView: UIScrollView!) {
         if scrollView.tag == 0 {
           // Do stuff
         } else {
           // Do other stuff
         }
      }
      

      【讨论】:

      • 问题是:如果我有滚动视图,里面有很多东西(包括我的 textview),而且我需要(或其他任何人)使用一些与 textview 无关的自定义委托?
      • 您尝试使用的自定义委托是什么?只需将您的委托设置为您想要的任何类。
      猜你喜欢
      • 2011-04-17
      • 1970-01-01
      • 2012-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-23
      • 2012-07-02
      • 2012-12-12
      相关资源
      最近更新 更多