iOS UIScrollView滚动检测

时间:2015-06-04 13:23:39

标签: ios objective-c iphone uiscrollview

我正在开发带有自定义的UITextView副本。现在我需要实现选择游标(见下面原始UITextView的截图)

UITextView

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

2 个答案:

答案 0 :(得分:0)

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

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

只需实现委托方法,并在必要时为每个滚动视图采取不同的操作。通过引用类中的属性或设置标记来执行此操作。

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

答案 1 :(得分:0)

/*
 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
    }
}