我正在尝试将 collectionView 的数据 (selectedItem) 传递给 imageViewController。我没有让计数器递增以向上滑动下一张图片
I am trying to pass data (selectedItem) of collectionView to imageViewController. I am not getting the counter incremented to swipe up the next image
我正在尝试将 collectionView 的数据 (selectedItem) 传递给 imageViewController。我没有让计数器递增以向上滑动下一张图像。当我单击单元格时,我正确地获得了第一张图像(所选图像)。但是当我滑动时,图像启动数组的第一个元素(imageViewController)。我哪里错了?
SelectedItem 是我从 collectionViewController 传递到 imageViewController 的值。
import UIKit
class ImageViewController: UIViewController,UIScrollViewDelegate {
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var myScrollView: UIScrollView!
var myImage = UIImage()
var selectedItem: String!
var images: [String] = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"]
let imageView = UIImageView()
var frame = CGRect(x:0,y:0,width:0,height:0)
override func viewDidLoad() {
super.viewDidLoad()
setupImageView()
}
private func setupImageView() {
var selectedImage = (selectedItem as NSString).integerValue
for var index in 0..<images.count {
frame.origin.x = myScrollView.frame.size.width * CGFloat(index)
frame.size = myScrollView.frame.size
let imageName = UIImageView(frame:frame)
imageName.image = myImage
index = selectedImage
myImage = UIImage(named: images[index])!
print(index)
myScrollView.showsVerticalScrollIndicator = false
self.myScrollView.addSubview(imageName)
}
myScrollView.contentSize = CGSize(width: (myScrollView.frame.size.width * CGFloat(images.count)), height: myScrollView.frame.size.height)
myScrollView.delegate = self
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pageIndex = round(scrollView.contentOffset.x/view.frame.width)
pageControl.currentPage = Int(pageIndex)
}
}
要使用选定的项目,您应该覆盖 collectionView didSelectItemAt 函数:
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
pageControl.currentPage = indexPath.row
}
此外,您的代码与您的问题有很大不同,我不太了解您的总体目标。如果你想创建一个滚动的图像列表,你应该继承 UICollectionViewCell 而不是将 UIImageView 添加到 UIScrollView。您可以在我的这个开源 snippet 中看到示例 GalleryViewController。
private func setupImageView() {
for var index in 0..<images.count {
frame.origin.x = myScrollView.frame.size.width * CGFloat(index)
frame.size = myScrollView.frame.size
let imageName = UIImageView(frame:frame)
imageName.image = UIImage(named: images[index])!
print(index)
myScrollView.showsVerticalScrollIndicator = false
self.myScrollView.addSubview(imageName)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
pageControl.currentPage = indexPath.row
}
myScrollView.contentSize = CGSize(width: (myScrollView.frame.size.width * CGFloat(images.count)), height: myScrollView.frame.size.height)
myScrollView.delegate = self
let indexpaths = UserDefaults.standard.value(forKey: "IndexPath") as! Int
let x = Double(414 * indexpaths) // here 414 is your current scroll view frame size
myScrollView.contentOffset = CGPoint(x: x, y: 0.0)
}
just change your code with this code and your problem is sloved.
Thank you
我正在尝试将 collectionView 的数据 (selectedItem) 传递给 imageViewController。我没有让计数器递增以向上滑动下一张图像。当我单击单元格时,我正确地获得了第一张图像(所选图像)。但是当我滑动时,图像启动数组的第一个元素(imageViewController)。我哪里错了?
SelectedItem 是我从 collectionViewController 传递到 imageViewController 的值。
import UIKit
class ImageViewController: UIViewController,UIScrollViewDelegate {
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var myScrollView: UIScrollView!
var myImage = UIImage()
var selectedItem: String!
var images: [String] = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"]
let imageView = UIImageView()
var frame = CGRect(x:0,y:0,width:0,height:0)
override func viewDidLoad() {
super.viewDidLoad()
setupImageView()
}
private func setupImageView() {
var selectedImage = (selectedItem as NSString).integerValue
for var index in 0..<images.count {
frame.origin.x = myScrollView.frame.size.width * CGFloat(index)
frame.size = myScrollView.frame.size
let imageName = UIImageView(frame:frame)
imageName.image = myImage
index = selectedImage
myImage = UIImage(named: images[index])!
print(index)
myScrollView.showsVerticalScrollIndicator = false
self.myScrollView.addSubview(imageName)
}
myScrollView.contentSize = CGSize(width: (myScrollView.frame.size.width * CGFloat(images.count)), height: myScrollView.frame.size.height)
myScrollView.delegate = self
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pageIndex = round(scrollView.contentOffset.x/view.frame.width)
pageControl.currentPage = Int(pageIndex)
}
}
要使用选定的项目,您应该覆盖 collectionView didSelectItemAt 函数:
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
pageControl.currentPage = indexPath.row
}
此外,您的代码与您的问题有很大不同,我不太了解您的总体目标。如果你想创建一个滚动的图像列表,你应该继承 UICollectionViewCell 而不是将 UIImageView 添加到 UIScrollView。您可以在我的这个开源 snippet 中看到示例 GalleryViewController。
private func setupImageView() {
for var index in 0..<images.count {
frame.origin.x = myScrollView.frame.size.width * CGFloat(index)
frame.size = myScrollView.frame.size
let imageName = UIImageView(frame:frame)
imageName.image = UIImage(named: images[index])!
print(index)
myScrollView.showsVerticalScrollIndicator = false
self.myScrollView.addSubview(imageName)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
pageControl.currentPage = indexPath.row
}
myScrollView.contentSize = CGSize(width: (myScrollView.frame.size.width * CGFloat(images.count)), height: myScrollView.frame.size.height)
myScrollView.delegate = self
let indexpaths = UserDefaults.standard.value(forKey: "IndexPath") as! Int
let x = Double(414 * indexpaths) // here 414 is your current scroll view frame size
myScrollView.contentOffset = CGPoint(x: x, y: 0.0)
}
just change your code with this code and your problem is sloved.
Thank you