且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

跟随 UICollectionView 指示器的 UIView

更新时间:2023-12-05 17:20:16

这对我有用:请查看随附的 gif.我添加了与滚动指示器平行的蓝色自定义 uiview.

This worked for me: Please look at the attached gif. I have added the custom uiview with blue color parallel to the scrool indicator.

我尝试了表格视图,但这也适用于集合视图.

I tried for the table view, but this also works for the collection view too.

-(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;
       });

  }

还要确保您在视图中添加了 uiview 确实加载了方法.

Also maske sure you added the uiview in you view did load method.

//在viewDidLoad中:

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

Note that I have used the sample values for the indicatorView postion.You can replace these as per your need.

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

indicatorView.backgroundColor=[UIColor blueColor];

[self.tableView addSubview:indicatorView];