方法collectionView(didSelectItemAt ...)在iTunesConnect上不起作用;但是,它确实可以在调试中工作

时间:2019-05-18 22:55:23

标签: swift4.2 xcode10.2 migrating

我已经将Xcode更新到最新版本,当前是10.2.1(10E1001),并将我的项目从Swift 4迁移到了Swift 5。

这给我带来了一些麻烦,但最终我建立了我的项目,并且可以从iPhone上的调试版本正常运行。

此后,我在归档项目时遇到了一些麻烦(也许这可能是一个原因)

我已经将其上传到App Store,然后在TestFlight上尝试了我的应用。 另外,由于某种原因,我的项目中很少有代码会出错。

似乎collectionViewdidSelectItemAtIndexPath ...)不起作用(但在Xcode中完全可以使用),而我的collectionView的自定义布局也不起作用(但也适用于Debug)。

似乎布局工作不正确,但是除了供应配置文件外,我无法理解Debug和Release版本之间的区别。

我可以为您分享更多您需要的视频,代码,我真的需要解决此问题。

我在网上没有找到其他类似的东西

我从https://codereview.stackexchange.com/questions/197017/page-and-center-uicollectionview-like-app-store那里获得了自定义布局代码

    class SnapPagingLayout: UICollectionViewFlowLayout {
    private var centerPosition = true
    private var peekWidth: CGFloat = 0
    private var indexOfCellBeforeDragging = 0

    convenience init(centerPosition: Bool = true, peekWidth: CGFloat = 40, spacing: CGFloat? = nil, inset: CGFloat? = nil) {
        self.init()

        self.scrollDirection = .horizontal
        self.centerPosition = centerPosition
        self.peekWidth = peekWidth

        if let spacing = spacing {
            self.minimumLineSpacing = spacing
        }

        if let inset = inset {
            self.sectionInset = UIEdgeInsets(top: 0, left: inset, bottom: 0, right: inset)
        }
    }

    override func prepare() {
        super.prepare()

        guard let collectionView = collectionView else { return }
        self.itemSize = calculateItemSize(from: collectionView.bounds.size)
    }

    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        guard let collectionView = collectionView,
            !newBounds.size.equalTo(collectionView.bounds.size) else {
                return false
        }

        itemSize = calculateItemSize(from: collectionView.bounds.size)
        return true
    }
}
private extension SnapPagingLayout {

    func calculateItemSize(from bounds: CGSize) -> CGSize {
        return CGSize(
            width: bounds.width - peekWidth * 2,
            height: (bounds.width - peekWidth * 2) / 1.77
        )
    }

    func indexOfMajorCell() -> Int {
        guard let collectionView = collectionView else { return 0 }

        let proportionalOffset = collectionView.contentOffset.x
            / (itemSize.width + minimumLineSpacing)

        return Int(round(proportionalOffset))
    }


}

extension SnapPagingLayout {

    func willBeginDragging() {
        indexOfCellBeforeDragging = indexOfMajorCell()
    }

    func willEndDragging(withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        guard let collectionView = collectionView else { return }

        // Stop scrollView sliding
        targetContentOffset.pointee = collectionView.contentOffset

        // Calculate where scrollView should snap to
        let indexOfMajorCell = self.indexOfMajorCell()

        guard let dataSourceCount = collectionView.dataSource?.collectionView(collectionView, numberOfItemsInSection: 0),
            dataSourceCount > 0 else {
                return
        }

        // Calculate conditions
        let swipeVelocityThreshold: CGFloat = 0.3 // After some trail and error
        let hasEnoughVelocityToSlideToTheNextCell = indexOfCellBeforeDragging + 1 < dataSourceCount && velocity.x > swipeVelocityThreshold
        let hasEnoughVelocityToSlideToThePreviousCell = indexOfCellBeforeDragging - 1 >= 0 && velocity.x < -swipeVelocityThreshold
        let majorCellIsTheCellBeforeDragging = indexOfMajorCell == indexOfCellBeforeDragging
        let didUseSwipeToSkipCell = majorCellIsTheCellBeforeDragging
            && (hasEnoughVelocityToSlideToTheNextCell || hasEnoughVelocityToSlideToThePreviousCell)

        guard didUseSwipeToSkipCell else {
            // Better way to scroll to a cell
            collectionView.scrollToItem(
                at: IndexPath(row: indexOfMajorCell, section: 0),
                at: centerPosition ? .centeredHorizontally : .left, // TODO: Left ignores inset
                animated: true
            )

            return
        }

        let snapToIndex = indexOfCellBeforeDragging + (hasEnoughVelocityToSlideToTheNextCell ? 1 : -1)
        var toValue = CGFloat(snapToIndex) * (itemSize.width + minimumLineSpacing)

        if centerPosition {
            // Back up a bit to center
            toValue = toValue - peekWidth + sectionInset.left
        }

        // Damping equal 1 => no oscillations => decay animation
        UIView.animate(
            withDuration: 0.3,
            delay: 0,
            usingSpringWithDamping: 1,
            initialSpringVelocity: velocity.x,
            options: .allowUserInteraction,
            animations: {
                collectionView.contentOffset = CGPoint(x: toValue, y: 0)
                collectionView.layoutIfNeeded()
        },
            completion: nil
        )
    }
}

我想在App Store中查看页面和中心集合视图。而且我也想让我的didSelect-method正常工作。

1 个答案:

答案 0 :(得分:0)

这是与参考相关的Swift 5.0编译器的错误:

https://bugs.swift.org/browse/SR-10257

更新: 进一步的搜索在this link on Stackoverflow

上找到了一个临时答案
  

您可以通过暂时使用@objc对其进行标记来解决该问题。