pujiaxin33 / JXCategoryView

A powerful and easy to use category view (segmentedcontrol, segmentview, pagingview, pagerview, pagecontrol) (腾讯新闻、今日头条、QQ音乐、网易云音乐、京东、爱奇艺、腾讯视频、淘宝、天猫、简书、微博等所有主流APP分类切换滚动视图)
MIT License
6.1k stars 1.16k forks source link

子页面分为上下两部分,顶部为横向滑动的UICollectionView。UICollectionView不能滑动 #526

Open fpower666 opened 2 hours ago

fpower666 commented 2 hours ago

private func setupGestureRecognizers() { // 为topBgView添加手势识别器

    let panGesture1 = UIPanGestureRecognizer(target: self, action: #selector(handleTopBgPanGesture(_:)))
    panGesture1.delegate = self // 设置手势代理
    topBgView.addGestureRecognizer(panGesture1)

    let panGesture2 = UIPanGestureRecognizer(target: self, action: #selector(handleTopListPanGesture(_:)))
    panGesture2.delegate = self // 设置手势代理
    listCollectionView.addGestureRecognizer(panGesture2)

}

@objc private func handleTopBgPanGesture(_ gesture: UIPanGestureRecognizer) {

}

@objc private func handleTopListPanGesture(_ gesture: UIPanGestureRecognizer) {
    printLog(self.listCollectionView.isScrollEnabled)
    // 只在手势结束时触发一次刷新
    guard gesture.state == .ended else { return }

    // 获取手势的最终位移
    let translation = gesture.translation(in: self.listCollectionView)

    // 判断滑动方向:如果是横向滑动则进行刷新处理
    if abs(translation.x) > abs(translation.y) {
        // 临时启用滚动
        listCollectionView.isScrollEnabled = true

        if translation.x > 0 {
            // 向右滑动时触发右侧刷新
        } else {
            // 向左滑动时触发左侧刷新
            printLog("listCollectionView----向左滑动")
        }
    }
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    if (gestureRecognizer.view == listCollectionView || gestureRecognizer.view == topBgView) &&
        (otherGestureRecognizer.view == listCollectionView || otherGestureRecognizer.view == topBgView) {
        self.listCollectionView.isScrollEnabled = true
        return true
    }
    return false
}
fpower666 commented 2 hours ago

上面代码在左右滑动listCollectionView的时候手动触发刷新,但是感觉效果太生硬了。