简体   繁体   English

如何创建像 AIRBNB 这样的水平集合视图(在幻灯片上自动选择索引路径)

[英]How to create a Horizontal collection view like AIRBNB (Auto select index path on slide)

Sample video of AIRBNB APP AIRBNB APP示例视频

I implemented logic in func scrollViewDidScroll(_ scrollView: UIScrollView) but it is not working properly.我在func scrollViewDidScroll(_ scrollView: UIScrollView)中实现了逻辑,但它无法正常工作。

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    
    let principal = scrollView.contentSize.width - scrollView.frame.width
    let progress = scrollView.contentOffset.x / principal
    var progresss = max(0, min(1, progress))
    progressBar.setProgress(Float(progresss), animated: true)
    
    new = Int((CGFloat(arrayTagBasedFilter.count)) * progresss)
    new = Int(max(0, min(arrayTagBasedFilter.count-1, new)))
    let newIndex = IndexPath(item: new, section: 0)
    
    if let selectedIndexForFilter = selectedIndexForFilter
    {
        if let cell = collectionViewFilter.cellForItem(at: selectedIndexForFilter) as? BoostFilterCVCell
        {
            cell.lblTitle.textColor = .black.withAlphaComponent(0.5)
        }
    }
    
    if let cell = collectionViewFilter.cellForItem(at: newIndex) as? BoostFilterCVCell
    {
        selectedIndexForFilter = newIndex
        cell.lblTitle.textColor = .black
        progressView.frame = CGRect(origin: CGPoint(x: cell.frame.origin.x, y: collectionViewFilter.frame.origin.y - 50),
                                    size: CGSize(width: cell.frame.width, height: 1))
    }
}

I was able to achieve this using the visibleCells method of UICollectionView .我能够使用UICollectionViewvisibleCells方法实现这一点。 Here is an example using colored cells, where the "highlighted" cell has its alpha transparency reduced to 25%.这是一个使用彩色单元格的示例,其中“突出显示”单元格的 alpha 透明度降低到 25%。

示例图像

This example is in Objective-C, but you should be able to convert it to Swift fairly easily.这个例子是在 Objective-C 中,但你应该能够很容易地将它转换成 Swift。

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    void (^animateCellAlpha)(__kindof UICollectionViewCell *cell, CGFloat alpha) = ^(__kindof UICollectionViewCell *cell, CGFloat alpha) {
        [UIView animateWithDuration:0.25 animations:^{
            [cell setAlpha:alpha];
        }];
    };
    void (^highlightCell)(__kindof UICollectionViewCell *cell) = ^(__kindof UICollectionViewCell *cell) {
        animateCellAlpha(cell, 0.25);
    };
    void (^unhighlightCell)(__kindof UICollectionViewCell *cell) = ^(__kindof UICollectionViewCell *cell) {
        animateCellAlpha(cell, 1.0);
    };
    
    // fetch your collection view
    
    UICollectionView * const collectionView = (UICollectionView *)scrollView;
    
    // get the visible cells sorted by X value
    
    NSArray <__kindof UICollectionViewCell *> * const visibleCells = [[collectionView visibleCells] sortedArrayUsingComparator:^NSComparisonResult(__kindof UICollectionViewCell * _Nonnull obj1, __kindof UICollectionViewCell * _Nonnull obj2) {
        return [@(CGRectGetMinX([obj1 frame])) compare:@(CGRectGetMinX([obj2 frame]))];
    }];
    
    // determine which cell should or should not be highlighted
    
    BOOL __block didHighlightCell = NO;
    [visibleCells enumerateObjectsUsingBlock:^(__kindof UICollectionViewCell * _Nonnull cell, NSUInteger idx, BOOL * _Nonnull stop) {
        if (didHighlightCell) {
            // we do not want to highlight cells if a cell has already been highlighted
            unhighlightCell(cell);
            return;
        }
        
        // only highlight the cell if more than 50% of it is visible
        
        CGRect const frame = [[self view] convertRect:[cell frame] fromView:[cell superview]];
        if (CGRectGetMinX(frame) + CGRectGetWidth(frame) > CGRectGetWidth(frame) * 0.50) {
            highlightCell(cell);
            didHighlightCell = YES;
        } else {
            unhighlightCell(cell);
        }
    }];
    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM