滚动时如何旋转集合视图单元格

问题描述

我想在滚动浏览集合视图时添加并同步旋转过渡效果,到目前为止,我设法通过调整大小,添加淡入淡出效果以及当集合视图单元格靠近集合中心时对齐来完成以下操作滚动视图。

预期结果应该是这样的:

enter image description here

滚动时使集合视图褪色的效果

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    
    // When the cell is at the center it has it's the original size
    // When the cell is moving left | right make it smaller by multiplying its size with a smaller scale
    // The scale is determined by the distance from the center of the cell to the center of the collection view

    let centerX = collectionView.center.x

    for (index,cell) in collectionView.visibleCells.enumerated()  {
        // Coordinate of the cell in the viewcontroller's root view coordinate space
        let cell = cell as! PlayermixerCollectionCell
        let basePosition = cell.convert(CGPoint.zero,to: self.view)
        let cellCenterX = basePosition.x + collectionView.frame.size.height / 2.0
        let tolerance = 0.02

        let distance = abs(cellCenterX - centerX)
        var scale = 1.0 + tolerance - (Double(( distance / centerX )) * 0.105)
        
        // Set maximum scale for centered item
        if (scale > 1.0) {
            scale = 1.0
        }

        // Set minimum scale so the prevIoUs and next item will have the same size
        if (scale < 0.860091) {
            scale = 0.860091
        }
        
        if (scale > 0.860091 && scale < 1.0) {
            // Transform the prevIoUs and next item cell size based on the scale
            cell.setItemAsInactive(withScale: CGFloat(scale))
        } else {
            // Transform the centered item cell size based on the scale
            cell.setItemAsActive(withScale: CGFloat(scale))
        }
    }
}

捕捉到最近的集合视图单元格并将其放置在中心:

func scrollViewWillEndDragging(_ scrollView: UIScrollView,withVeLocity veLocity: CGPoint,targetContentOffset: UnsafeMutablePointer<CGPoint>) {

    // Stop scrolling to this point
    targetContentOffset.pointee = collectionView.contentOffset

    // Calculate conditions
    let indexOfMajorItem = determineIndexOfMajorItem()
    let itemCount = cells.count - 1

    let swipeVeLocityThreshold: CGFloat = 0.5
    let hasEnoughVeLocityToSlidetoNextItem = indexOfCellBeforeDragging + 1 < itemCount && veLocity.x > swipeVeLocityThreshold
    let hasEnoughVeLocityToSlidetoPrevItem = indexOfCellBeforeDragging - 1 >= 0 && veLocity.x < -swipeVeLocityThreshold

    let isMajorItemmatchPrevItem = indexOfMajorItem == indexOfCellBeforeDragging
    let didSwipetoSkipItem = isMajorItemmatchPrevItem && (hasEnoughVeLocityToSlidetoNextItem || hasEnoughVeLocityToSlidetoPrevItem)

    // If possible animate so that swipe animation continues or pop back to the expected item
    let snappedItemIndex = didSwipetoSkipItem ? indexOfCellBeforeDragging + (hasEnoughVeLocityToSlidetoNextItem ? 1 : -1) : indexOfMajorItem
    let snappedItemIndexPath = IndexPath(item: snappedItemIndex,section: 0)
    
    indexOfCenteredCell = snappedItemIndexPath
    
    // Scroll to the snapped item
    collectionView.scrollToItem(at: snappedItemIndexPath,at: .centeredHorizontally,animated: true)
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)