ios - 滚动视图与集合视图。获取“可见图像”
问题描述
我想要实现的目标:我有一组图像,我想水平滑动/滚动浏览。这些图像代表联系人。当我找到我想要的联系人(图像)时,我按下一个单独的开始呼叫按钮。此按钮连接到电话视图控制器。我需要在那个电话视图控制器上显示我正在呼叫的人的图像。为此,我需要知道在启动 segue 之前我使用的是什么联系人图像。
我目前拥有的:我已经设置了一个滚动视图和一个单独的集合视图(它们无法相互连接。我两者都有,因为我试图找出哪个可以满足我的需求)。两者都功能齐全,允许我翻阅阵列中的图像。当我按下将启动 segue 到下一个视图控制器的按钮时,我无法弄清楚如何务实地获取正在显示的图像。
我最初试图标记图像,然后检索标记并通过 segue 将其传递给下一个视图控制器。然后我尝试获取“当前可见”的图像索引并通过 segue 传递它。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCollectionViewCell", for: indexPath) as! ImageCollectionViewCell
cell.imgImage.image = imageArray[indexPath.row]
//Get character for segue
selectedCharacter = indexPath.row
return cell
}
我还尝试获取图像名称并通过 segue 传递它。
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCollectionViewCell", for: indexPath) as! ImageCollectionViewCell
switch cell.imgImage.image{
case UIImage(named: "Van"):
selectedCharacter = 2
case UIImage(named: "Fel"):
selectedCharacter = 3
default:
selectedCharacter = 1
}
}
我还没有想出任何方法来获取滚动视图的信息。滚动视图 viewDidLoad 代码:
@IBOutlet var scrollView: UIScrollView!
for i in 0..<images.count {
let imageView = UIImageView()
let x = self.view.frame.size.width * CGFloat(i)
imageView.frame = CGRect(x: x, y: 0, width: 343, height: 244)
imageView.contentMode = .scaleAspectFit
imageView.image = images[i]
imageView.tag = i
scrollView.contentSize.width = scrollView.frame.size.width * CGFloat(i + 1)
scrollView.addSubview(imageView)
}
问题:是否有人对我如何获得要呼叫的联系人以及是否应该使用滚动视图或集合视图有任何想法?
谢谢!
解决方案
我不知道你在哪里点击,但是你可以用这个得到当前视图索引: let index = scrollView.contentOffset.x / scrollView.bounds.size.width collectionView or scrollview is the same
推荐阅读
- oracle - 为oracle sql中的两个不同列添加条件where子句
- javascript - 使用过渡更改 hr 数据内容的颜色
- c - 用C中的4个唯一随机数填充数组
- lua - 如何使lua搜索模块与首先调用require的模块位于同一文件夹中?
- php - 为什么不能为每个班级创建模拟?
- kubernetes - 如何在 GCP VM 中公开 minikube
- swift3 - 可以使用 URLSession swift 4 从 youtube 下载视频吗?
- tensorflow - 线性回归模型的 R 平方负数 - Tensorflow
- swift - Swift 通用协议问题?
- excel - 如何仅搜索每行的前 6 个字符的列(多行)?