2017-08-28 39 views
0

我有這些值:如何獲得關於內容大小和設備寬度的x位置?

  • iPhone寬度:414
  • UICollectionView寬度:414
  • UICollectionView高度:25
  • UICollectionView ContentSize寬度:578
  • 滾動到現在的位置: 466
  • Cu自定查看寬度:112
  • 自定義視圖身高:25

如何找到給UICollectionView寬度相對於一個值(origin.x)當滾動到位置是?

其他詳細信息:我將自定義視圖移動到UICollectionView的單元格的滾動位置。即使我們的內容尺寸大於UICollectionView的實際寬度,我們也應該根據UICollectionView的位置使用自定義視圖。

+1

@hemange可以爲您提供一些代碼,我看到並能有所幫助 –

回答

0

試試這個代碼//未測試

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',我已經添加完整的代碼根據您的看法,請提供一個問題,你是面對 –

相關問題