跟随 UICollectionView 指示器的 UIView

3
我希望创建一个自定义的UIView,其中包含一个UIImageView和一个UILabel,指向UICollectionView滚动指示器,并随其一起滚动。

enter image description here

我正在使用这段代码:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //get refrence of vertical indicator
    UIImageView *verticalIndicator = ((UIImageView *)[scrollView.subviews objectAtIndex:(scrollView.subviews.count-1)]);
    // get the relative position of the indicator in the main window
    CGPoint p = [verticalIndicator convertPoint:verticalIndicator.bounds.origin toView:[[UIApplication sharedApplication] keyWindow]];
    // set the custom view new position
    CGRect indicatorFrame = self.indicatorView.frame;
    indicatorFrame.origin.y = p.y;
    self.indicatorView.frame = indicatorFrame;
}

但是指示器视图并不完全跟随指示器!!

你的意思是想要将一个UIview附加到滚动条上吗? - Teja Nandamuri
是的,我刚刚上传了一张解释我需要什么的图片。绿色视图应始终跟随滚动指示器。 - Rifinio
那真的是一个UICollectionView吗? - Teja Nandamuri
1
请检查我的答案 @Rifinio - Teja Nandamuri
2个回答

2
这对我很有用:请查看附加的gif。 我已经添加了自定义uiview,与滚动指示器平行,颜色为蓝色。
我尝试了表视图,但这也适用于集合视图。 enter image description here
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    dispatch_async(dispatch_get_main_queue(), ^{
        //get refrence of vertical indicator
        UIImageView *verticalIndicator = ((UIImageView *)[scrollView.subviews objectAtIndex:(scrollView.subviews.count-1)]);
        // get the relative position of the indicator in the main window
        //    CGPoint p = [verticalIndicator convertPoint:verticalIndicator.bounds.origin toView:[[UIApplication sharedApplication] keyWindow]];

        CGPoint p = CGPointMake(verticalIndicator.frame.origin.x-10,
                                verticalIndicator.frame.origin.y - scrollView.contentOffset.y);
        // set the custom view new position
        CGRect indicatorFrame = CGRectMake(verticalIndicator.frame.origin.x, verticalIndicator.frame.origin.y, 10, 10);
         self.indicatorView.frame = indicatorFrame;
       });

  }

请确保你在你的viewDidLoad方法中添加了。

//在viewDidLoad中:

请注意,我使用了指示视图位置的示例值。您可以根据需要替换这些值。

indicatorView=[[UIView alloc]initWithFrame:CGRectMake( p.x, p.y , 10, 10)];

indicatorView.backgroundColor=[UIColor blueColor];

[self.tableView addSubview:indicatorView];

你可以更改p点的x坐标。我已经给出了指示器-10。 - Teja Nandamuri
对我来说它很好用,谢谢。你需要编辑答案并使用计算出的p点:CGRect indicatorFrame = CGRectMake(p.x, p.y, 10, 10); - Rifinio

0

对我来说,这个可以工作:

....
scrollIndicatorView = [self scrollIndicator];
[self.collectionView addSubview:scrollIndicatorView];
....

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  // percentage of the content reached * height
  CGFloat yPos = (self.collectionView.contentOffset.y / (self.collectionView.contentSize.height - self.collectionView.bounds.size.height)) * self.collectionView.bounds.size.height;

  yPos += self.collectionView.contentOffset.y; // add content offset becasue view is inside colelctionview which content moves

  CGRect indicatorFrame = CGRectMake(self.collectionView.bounds.size.width - 10,
                                   yPos,
                                   10,
                                   30);
  scrollIndicatorView.frame = indicatorFrame; 
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接