collectionView 取消默认的点击效果

博客内容提及代码‘cell.isMultipleTouchEnabled = false’,表明是在进行禁用cell多点触摸功能的操作,这属于移动开发相关信息技术内容。

cell.isMultipleTouchEnabled = false


// // OnlineDeviceListViewController2.swift // SurveillanceHome // // Created by MaCong on 2025/10/10. // Copyright © 2025 tplink. All rights reserved. // import UIKit class OnlineDeviceListViewController2: OnlineDeviceListViewController { private var customCollectionView: UICollectionView? private let image: UIImage = { if #available(iOS 13.0, *) { return UIImage(named: "device_evaluation_fiveStars") ?? UIImage(systemName: "photo")! } else { return UIImage(named: "device_evaluation_fiveStars") ?? UIImage(named: "defaultImage") ?? UIImage() } }() override func viewDidLoad() { super.viewDidLoad() setupCustomCollectionView() } private func setupCustomCollectionView() { let layout = UICollectionViewFlowLayout() let cellWidth = (view.bounds.width - 60) / 2 layout.itemSize = CGSize(width: cellWidth, height: cellWidth) // 关键修改1:调整 sectionInset 的 top 为 0 layout.sectionInset = UIEdgeInsets(top: 0, left: 20, bottom: 20, right: 20) layout.minimumInteritemSpacing = 20 customCollectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) guard let collectionView = customCollectionView else { return } collectionView.backgroundColor = .white collectionView.register(ImageCell.self, forCellWithReuseIdentifier: "ImageCell") collectionView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(collectionView) // 关键修改2:正常约束集合视图位置(不固定高度) NSLayoutConstraint.activate([ collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor), collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor), collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor), collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor) ]) // 关键修改3:禁用集合视图的自动调整内容惯性 collectionView.contentInsetAdjustmentBehavior = .never collectionView.dataSource = self collectionView.delegate = self } } // MARK: - 数据源协议 extension OnlineDeviceListViewController2: UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 2 // 返回2个单元格 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as! ImageCell cell.configure(with: image) return cell } } // MARK: - 代理协议 extension OnlineDeviceListViewController2 { func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { print("图片 \(indexPath.row + 1) 被点击") } } // ImageCell 实现保持不变... class ImageCell: UICollectionViewCell { private let imageView = UIImageView() override init(frame: CGRect) { super.init(frame: frame) setupUI() } required init?(coder: NSCoder) { fatalError("init(coder:)未实现") } private func setupUI() { backgroundColor = .lightGray layer.cornerRadius = 8 clipsToBounds = true // imageView.contentMode = .scaleAspectFit imageView.translatesAutoresizingMaskIntoConstraints = false contentView.addSubview(imageView) NSLayoutConstraint.activate([ imageView.topAnchor.constraint(equalTo: contentView.topAnchor), imageView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor), imageView.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.9), imageView.heightAnchor.constraint(equalTo: contentView.heightAnchor, multiplier: 0.9) ]) } func configure(with image: UIImage) { imageView.image = image } } 这个展现出来的效果理论上是什么样子的?
最新发布
10-14
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值