【问题标题】:Creating a view over a cell in a UICollectionView在 UICollectionView 中的单元格上创建视图
【发布时间】:2013-01-10 04:21:34
【问题描述】:

我在将新创建的视图放置在 UICollectionView 单元格上时遇到问题(并将其扩大以填满屏幕)。当集合视图滚动到顶部时效果很好,但是当我滚动时,我无法正确放置新视图,因为我收集的浮点值低于窗口高度。

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    UIView *picturesView = [[UIView alloc] init];
    picturesView.frame = CGRectMake(CGRectGetMidX([[collectionView cellForItemAtIndexPath:indexPath] bounds]), CGRectGetMidY([[collectionView cellForItemAtIndexPath:indexPath] bounds]), 100, 100);

    [self.view addSubview:picturesView];

    [UIView animateWithDuration:0.3 animations:^{
        picturesView.frame = CGRectMake(0, 0, windowWidth, windowHeight);
        picturesView.alpha = 1.0;
    }];
}

这里的问题是CGRectMake的x和y参数中的值在滚动后大于可用的窗口区域,并且视图出现在滚动剪辑的下方(新创建的视图似乎从底部向上滑动屏幕,这不是所需的动画)。

逻辑会让我通过 positionY = ((totalScrollHeight - topScrollAreaClipped) + screenPositionOfCell) 计算这些应该放在哪里。我似乎无法获得的唯一变量是被滚动剪辑的 UICollectionView 的高度。

除非有更好的方法来做到这一点。

【问题讨论】:

    标签: ios objective-c xcode uicollectionview


    【解决方案1】:

    您需要补偿UICollectionView 的滚动位置。检查scrollOffsett 属性,并从您的x/y 位置减去该值,为您的UIView 提供正确的框架。

    【讨论】:

    • scrollOffset 实际上不是答案。但它确实为我指明了正确的方向。我最终做了以下事情:
    • scrollOffset 实际上不是答案。但它确实为我指明了正确的方向。我最终做了以下事情:UICollectionViewLayoutAttributes *attributes = [collectionView layoutAttributesForItemAtIndexPath:indexPath]; float frameX = (attributes.frame.origin.x + CGRectGetMidX([[collectionView cellForItemAtIndexPath:indexPath] bounds])); float frameY = (attributes.frame.origin.y + CGRectGetMidY([[collectionView cellForItemAtIndexPath:indexPath] bounds])); if(frameY > Global.windowHeight) { frameY = (frameY - collectionView.contentOffset.y); } frameY 做到了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多