简体   繁体   English

iOS UIScrollView滚动检测

[英]iOS UIScrollView scrolling detection

I'm developing kind of UITextView copy with customizations. 我正在开发具有自定义功能的UITextView副本。 For now I need to implement selection cursors (see screenshot of original UITextView below) 现在,我需要实现选择光标(请参见下面原始UITextView屏幕截图)

UITextView

As I discovered from Debug View Hierarchy Apple developers draw these dots on separate Window to avoid clipping, and when UIScrollView starts dragging they move these dots inside UITextView , when it stops dragging they move it back to separate window. 正如我从Debug View Hierarchy发现的那样,Apple开发人员在单独的Window上绘制这些点以避免剪贴,并且当UIScrollView开始拖动时,他们将这些点移到UITextView ,而当它停止拖动时,他们将其移回单独的窗口。 The only problem with this approach is how can I detect when some of my TextView superview 's are UIScrollView and they start/end scrolling? 这种方法的唯一问题是,当某些TextView superviewUIScrollView且它们开始/结束滚动时,如何检测? Setting delegate for each of UIScrollView -type superviews looks bad and will bring a lot of headache, cause I will need to manage several delegates if needed (and even detect there change). 为每个UIScrollView类型的superviews设置委托看起来很糟糕,并且会带来很多麻烦,因为如果需要,我将需要管理多个委托(甚至检测到那里的变化)。 Any ideas? 有任何想法吗?

You can use the same scroll view delegate for all UIScrollViews. 您可以对所有UIScrollView使用相同的滚动视图委托。

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

Just implement the delegate methods and take different action if necessary for each scroll view. 只需实现委托方法,并在必要时对每个滚动视图采取不同的操作。 Either do this by referencing properties in the class or setting the tag. 通过引用类中的属性或设置标签来执行此操作。

func scrollViewDidScroll(scrollView: UIScrollView!) {
   if scrollView.tag == 0 {
     // Do stuff
   } else {
     // Do other stuff
   }
}
/*
 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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM