如何获得关于内容大小和设备宽度的x位置?

问题描述:

我有这些值:如何获得关于内容大小和设备宽度的x位置?

  • iPhone宽度:414
  • UICollectionView宽度:414
  • UICollectionView高度:25
  • UICollectionView ContentSize宽度:578
  • 滚动到现在的位置: 466
  • Cu自定查看宽度:112
  • 自定义视图身高:25

如何找到给UICollectionView宽度相对于一个值(origin.x)当滚动到位置是?

其他详细信息:我将自定义视图移动到UICollectionView的单元格的滚动位置。即使我们的内容尺寸大于UICollectionView的实际宽度,我们也应该根据UICollectionView的位置使用自定义视图。

+1

@hemange可以为您提供一些代码,我看到并能有所帮助 –

试试这个代码//未测试

EDITED

let percentage = (self.collectionview.contentOffset.x * 100)/(self.collectionview.contentSize.width - self.collectionview.frame.size.width) 
let rightCorner = (self.collectionview.frame.size.width * percentage)/100 

yourview.frame.origin.x = rightCorner 
yourview.frame.origin.x -= yourview.frame.size.width 

EDIT 9月4日

添加以下代码- (void)scrollViewDidScroll:(UIScrollView *)scrollView

if ([self.view viewWithTag:500000]) { 
    [[self.view viewWithTag:500000] removeFromSuperview]; 
} 

CGFloat xPos = ((scrollView.contentOffset.x)/(scrollView.contentSize.width - scrollView.frame.size.width)) * (scrollView.frame.size.width - 112) ; 

UIView *view = [UIView new]; 
view.tag = 500000; 
[view setFrame:CGRectMake(xPos ,self.collectionViewFrames.frame.origin.y + self.collectionViewFrames.frame.size.height, 112, 25)]; 
[view setBackgroundColor:[UIColor redColor]]; 
[self.view addSubview:view]; 
[self.view bringSubviewToFront:view]; 

enter image description here

enter image description here

+0

我有更具体的更新我的问题,你可以请检查? – Hemang

+0

@Hemang请检查编辑4九月,让我知道如果你仍然问 –

+0

@Hemang对我来说,它工作请复制粘贴代码在'scrollviewDidScroll',我已经添加完整的代码根据您的看法,请提供一个问题,你是面对 –