Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionView指示器后面的UIView_Ios_Objective C_Uiscrollview_Uicollectionview_Indicator - Fatal编程技术网

Ios UICollectionView指示器后面的UIView

Ios UICollectionView指示器后面的UIView,ios,objective-c,uiscrollview,uicollectionview,indicator,Ios,Objective C,Uiscrollview,Uicollectionview,Indicator,我想创建一个自定义UIView,其中包含一个UIImageView和一个指向UICollectionView滚动指示器的UILabel,并使用它滚动 我正在使用以下代码: - (void)scrollViewDidScroll:(UIScrollView *)scrollView { //get refrence of vertical indicator UIImageView *verticalIndicator = ((UIImageView *)[scrollView.

我想创建一个自定义UIView,其中包含一个UIImageView和一个指向UICollectionView滚动指示器的UILabel,并使用它滚动

我正在使用以下代码:

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

但是指示灯视图并不完全遵循指示灯

这对我有用:请看附件中的gif。我添加了自定义uiview,蓝色与scrool指示器平行

我尝试使用table视图,但这也适用于collection视图

另外,请确保在视图中添加了uiview并加载了方法

//在
viewDidLoad
中:

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

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

indicatorView.backgroundColor=[UIColor blueColor];

[self.tableView addSubview:indicatorView];
对我来说,这很有效:

....
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; 
}

你的意思是想在滚动条上附加uiview?是的,我刚刚上传了一张图片,解释了我需要什么。绿色视图应始终遵循真正的uicollection视图的滚动指示符号?请检查我的答案@Rifinioyou可以更改p点x坐标。我已经给出了指标-10。对我来说效果很好,谢谢。您需要编辑答案并使用计算出的p点:CGRect indicatorFrame=CGRectMake(p.x,p.y,10,10);
....
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; 
}