【问题标题】:How to get the x position with respect to content size and device width?如何获得关于内容大小和设备宽度的 x 位置?
【发布时间】:2017-08-28 11:35:41
【问题描述】:

我有这些价值观:

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

当滚动到466的位置时,如何找到相对于UICollectionView宽度的值(origin.x)?

其他详细信息:我正在将自定义视图移动到 UICollectionView 中单元格的滚动位置。即使我们的内容大小大于UICollectionView 的实际宽度,我们也应该将自定义视图放置在相对于UICollectionView 的位置。

【问题讨论】:

  • @hemange 你能提供一些我看到的代码并且可以提供一些帮助

标签: ios swift uiscrollview uicollectionview


【解决方案1】:

试试这个代码 // 未测试

已编辑

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

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];

【讨论】:

  • 我已经更具体地更新了我的问题,你能检查一下吗?
  • @Hemang 请在 9 月 4 日检查编辑,如果您仍有问题,请告诉我
  • @Hemang 对我来说它正在工作请复制粘贴的代码scrollviewDidScroll,我已根据您的观点添加了完整代码,请提供您面临的问题
  • @Hemang 你可以查看收藏视图滚动指示器和红色视图
  • 感谢迈克,感谢您对我的问题的不懈努力。我已经找到解决方案了,我稍后会在这里发布。
猜你喜欢
  • 2019-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-30
  • 2013-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多