Open fpower666 opened 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 }
上面代码在左右滑动listCollectionView的时候手动触发刷新,但是感觉效果太生硬了。
private func setupGestureRecognizers() { // 为topBgView添加手势识别器