触摸CollectionView时禁用动画?

时间:2016-12-28 01:03:45

标签: ios swift animation uicollectionview

我有一个在滚动时执行动画的集合视图。但是,当这个动画正在进行时,我无法触摸屏幕来停止滚动。我必须等待滚动才能完全停止。

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {

    let cellsAcross: CGFloat = 1
    let spaceBetweenCells: CGFloat = 1
    let dim = (collectionView.bounds.width - (cellsAcross - 1) * spaceBetweenCells) / cellsAcross
    let finalCellFrame = cell.frame
    //check the scrolling direction to verify from which side of the screen the cell should come.
    let translation = collectionView.panGestureRecognizer.translation(in: collectionView.superview!)
    if translation.x > 0 {
        cell.frame = CGRect(x: CGFloat(4), y: CGFloat(finalCellFrame.origin.y), width: CGFloat(dim), height: CGFloat(44))
    } else {
        cell.frame = CGRect(x: CGFloat(4), y: CGFloat(finalCellFrame.origin.y), width: CGFloat(dim), height: CGFloat(44))
    }
    UIView.animate(withDuration: 0.25, delay: 0.4, animations: {(_: Void) -> Void in
        cell.frame = finalCellFrame
    })

}

当我删除动画时,只要我的手指触摸屏幕,我就可以自由地中断滚动。如何在保持动画的同时中断滚动?

1 个答案:

答案 0 :(得分:2)

尝试从

更改动画块
2

UIView.animate(withDuration: 0.25, delay: 0.4, animations: {(_: Void) -> Void in
        cell.frame = finalCellFrame
})

添加 UIView.animate(withDuration: 0.25, delay: 0.4, options: .allowUserInteraction, animations: { cell.frame = finalCellFrame }, completion: nil) }) 作为选项可以解决问题。