[iOS]UITableView或UICollectionView的cell中嵌套UICollectionView后,第二层的CollectionViewCell点击无响应的问题

本文主要是介绍[iOS]UITableView或UICollectionView的cell中嵌套UICollectionView后,第二层的CollectionViewCell点击无响应的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

如图,UITableView中嵌套UICollectionView后无法点击分享.
之前为了解决这个问题,已经放弃了方式(collectionView:didSelectItemAtIndexPath:),选择使用cell中响应按钮点击的方式.
现在适配iOS13发现,上面这种方式也已经无法响应点击,所以现在适配时选择了点击穿透的方式来处理.

例:

解决:

点击穿透

OC

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{CGPoint bluePoint = [self convertPoint:point toView:self.collectionView];NSInteger index = -1;// 这种方式在iphone 7plus会顺序错误
//    NSArray *cellArr = self.collectionView.visibleCells;
//    for (int i = 0 ; i < cellArr.count ; i ++) {
//        WXYZ_ShareItemCell *curCell = [cellArr objectAtIndex:i];
//        // 判断给定的点是否被一个CGRect包含,可以用CGRectContainsPoint函数
//        BOOL contains = CGRectContainsPoint(curCell.frame, bluePoint);
//        if (contains) {
//            index = i;
//            break;
//        }
//    }NSArray<NSIndexPath *> *indexPathsForVisibleItems = self.collectionView.indexPathsForVisibleItems;for (int i = 0 ; i < indexPathsForVisibleItems.count ; i ++) {NSIndexPath *indexPath = indexPathsForVisibleItems[i];WXYZ_ShareItemCell *curCell = (WXYZ_ShareItemCell *)[self.collectionView cellForItemAtIndexPath:indexPath];// 判断给定的点是否被一个CGRect包含,可以用CGRectContainsPoint函数BOOL contains = CGRectContainsPoint(curCell.frame, bluePoint);if (contains) {index = indexPath.item;break;}}if ([self.collectionView pointInside:bluePoint withEvent:event]) {if (index >= 0) {[self selectCellWithItem:index];}return self.collectionView;} else {return [super hitTest:point withEvent:event];}
}- (void)selectCellWithItem:(NSInteger)index {}

Swift

extension MineListCell {override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {let bluePoint: CGPoint = self.convert(point, to: self.collectionView)var index = -1let indexPathsForVisibleItems = self.collectionView.indexPathsForVisibleItemsfor (ind,obj) in indexPathsForVisibleItems.enumerated() {let indexPath = objif let curCell = self.collectionView.cellForItem(at: indexPath) {let contains = curCell.frame.contains(bluePoint)if contains {index = indexPath.itembreak}}}if self.collectionView.point(inside: bluePoint, with: event) {if index >= 0 {self.selectCellWithItem(index: index)}return self.collectionView} else {return super.hitTest(point, with: event)}}func selectCellWithItem(index: Int) {print(index)}
}

经过实际使用体验, 发现上面点击穿透的方式解决得并不完美. 用这种方式, 你会发现,点击动作还未完,但操作就已经响应.

下面对点击穿透方式进行优化:

1.使用点击穿透记录当前点击的位置

class MineListCell: UICollectionViewCell {// 使用点击穿透记录当前点击的位置var curTapIndex: Int = -1 ......}extension MineListCell {override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {let bluePoint: CGPoint = self.convert(point, to: self.collectionView)var index = -1let indexPathsForVisibleItems = self.collectionView.indexPathsForVisibleItemsfor (ind,obj) in indexPathsForVisibleItems.enumerated() {let indexPath = objif let curCell = self.collectionView.cellForItem(at: indexPath) {let contains = curCell.frame.contains(bluePoint)if contains {index = indexPath.itembreak}}}curTapIndex = indexif self.collectionView.point(inside: bluePoint, with: event) {return super.hitTest(point, with: event)} else {return super.hitTest(point, with: event)}}}

2.在第一层UITableView或UICollectionView代理方法中接受到回调后进行相应的处理

extension MineVC: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {......func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {let cell: MineListCell = collectionView.cellForItem(at: indexPath) as! MineListCellif let items = cell.items, cell.curTapIndex >= 0, items.count > cell.curTapIndex  {let obj: MineItemModel = items[cell.curTapIndex]if let route = obj.route {if let topVC = ScreenUIManager.topViewController() {PushWithRoute(vc: topVC, route: route)}}}}
}

这篇关于[iOS]UITableView或UICollectionView的cell中嵌套UICollectionView后,第二层的CollectionViewCell点击无响应的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1091431

相关文章

深度解析Nginx日志分析与499状态码问题解决

《深度解析Nginx日志分析与499状态码问题解决》在Web服务器运维和性能优化过程中,Nginx日志是排查问题的重要依据,本文将围绕Nginx日志分析、499状态码的成因、排查方法及解决方案展开讨论... 目录前言1. Nginx日志基础1.1 Nginx日志存放位置1.2 Nginx日志格式2. 499

kkFileView启动报错:报错2003端口占用的问题及解决

《kkFileView启动报错:报错2003端口占用的问题及解决》kkFileView启动报错因office组件2003端口未关闭,解决:查杀占用端口的进程,终止Java进程,使用shutdown.s... 目录原因解决总结kkFileViewjavascript启动报错启动office组件失败,请检查of

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束

Python错误AttributeError: 'NoneType' object has no attribute问题的彻底解决方法

《Python错误AttributeError:NoneTypeobjecthasnoattribute问题的彻底解决方法》在Python项目开发和调试过程中,经常会碰到这样一个异常信息... 目录问题背景与概述错误解读:AttributeError: 'NoneType' object has no at

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

Kotlin Map映射转换问题小结

《KotlinMap映射转换问题小结》文章介绍了Kotlin集合转换的多种方法,包括map(一对一转换)、mapIndexed(带索引)、mapNotNull(过滤null)、mapKeys/map... 目录Kotlin 集合转换:map、mapIndexed、mapNotNull、mapKeys、map

nginx中端口无权限的问题解决

《nginx中端口无权限的问题解决》当Nginx日志报错bind()to80failed(13:Permissiondenied)时,这通常是由于权限不足导致Nginx无法绑定到80端口,下面就来... 目录一、问题原因分析二、解决方案1. 以 root 权限运行 Nginx(不推荐)2. 为 Nginx

解决1093 - You can‘t specify target table报错问题及原因分析

《解决1093-Youcan‘tspecifytargettable报错问题及原因分析》MySQL1093错误因UPDATE/DELETE语句的FROM子句直接引用目标表或嵌套子查询导致,... 目录报js错原因分析具体原因解决办法方法一:使用临时表方法二:使用JOIN方法三:使用EXISTS示例总结报错原

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

SpringSecurity整合redission序列化问题小结(最新整理)

《SpringSecurity整合redission序列化问题小结(最新整理)》文章详解SpringSecurity整合Redisson时的序列化问题,指出需排除官方Jackson依赖,通过自定义反序... 目录1. 前言2. Redission配置2.1 RedissonProperties2.2 Red