Ios Scrollview与Collectionview。获得;可见图像“;
我想要实现的目标:我有一组图像,我想水平滑动/滚动。这些图像表示联系人。当我找到我想要的联系人(图像)时,我会按一个单独的开始通话按钮。此按钮可连接到手机视图控制器。我需要在手机视图控制器上显示我正在呼叫的人的图像。要做到这一点,我需要知道在我启动segue之前,我的联系人图像是什么 我目前拥有的:我已经设置了一个scrollview和一个单独的collectionview(它们之间没有任何连接。我拥有这两个视图,因为我正在尝试找出哪一个可以满足我的需要)。两者都功能齐全,允许我从阵列中翻阅图像。 当我按下按钮启动到下一个视图控制器时,我不知道如何实际地获取显示的图像 我最初尝试标记图像,然后检索标记并通过segue将其传递给下一个视图控制器。 然后我尝试获取“当前可见”的图像索引,并通过segue传递它Ios Scrollview与Collectionview。获得;可见图像“;,ios,swift,xcode,xcode9,Ios,Swift,Xcode,Xcode9,我想要实现的目标:我有一组图像,我想水平滑动/滚动。这些图像表示联系人。当我找到我想要的联系人(图像)时,我会按一个单独的开始通话按钮。此按钮可连接到手机视图控制器。我需要在手机视图控制器上显示我正在呼叫的人的图像。要做到这一点,我需要知道在我启动segue之前,我的联系人图像是什么 我目前拥有的:我已经设置了一个scrollview和一个单独的collectionview(它们之间没有任何连接。我拥有这两个视图,因为我正在尝试找出哪一个可以满足我的需要)。两者都功能齐全,允许我从阵列中翻阅图
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
}
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
}
}
我还试图获得图像名称并通过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
}
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
}
}
我还没有找到任何方法来获取scrollview的信息。
Scrollview视图加载代码:
@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)
}
@IBOutlet var scrollView:UIScrollView!
对于0..中的i,可以使用集合视图。创建具有图像
属性的自定义单元格类。当您准备在cellForItem
函数中返回单元格时,可以设置image
属性
let cell = collectionView.dequeueReusableCellWithIdentifier("Your Cell's identifier", forIndexPath: indexPath)
cell.image = imageArray[indexPath.item]
然后,您可以覆盖didSelectItem
,并访问indepath
中传入的单元格的image
属性,如下所示:
let cell = collectionView!.cellForItemAtIndexPath(indexPath)
let image = cell.image
// do whatever you need with the image.
我不知道您在哪里单击,但您可以使用以下内容获取当前视图索引:
让index=scrollView.contentOffset.x/scrollView.bounds.size.width
collectionView或scrollview是相同的显示一些代码。你试过什么?什么不起作用?你的问题太广泛了,任何人都无法在这一点上提供帮助。@skladek好吧。做了一些编辑。这很有效!我将collectionView与一起使用,让indexHolder=Int(collectionViewer.contentOffset.x/collectionViewer.bounds.size.width)
,当我按下单独的调用按钮时,我能够将indexHolder传递给下一个视图控制器