我在 View Controller 中有一个 UICollectionView。当用户单击其中一个图像时,它会转到一个单独的页面以打开该图像。我不想每次都回到画廊更换图片,而是向左或向右滑动以获取下一张或上一张图片。
我已经看到了“pagingEnabled = true”的答案,但是你把它放在哪里呢?
我是 swift 新手。
下面是我的代码。我在这里先向您的帮助表示感谢。
ViewController.swift
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate
{
@IBOutlet weak var collectionView: UICollectionView!
let appleProducts = ["iPhone", "Apple Watch", "Mac", "iPad"]
let imageArray = [UIImage(named: "pug"), UIImage(named: "pug2"), UIImage(named: "pug3"), UIImage(named: "pug4")]
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return self.appleProducts.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell
cell.imageView?.image = self.imageArray[indexPath.row]
cell.titleLabel?.text = self.appleProducts[indexPath.row]
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
self.performSegueWithIdentifier("showImage", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "showImage"
{
let indexPaths = self.collectionView!.indexPathsForSelectedItems()!
let indexPath = indexPaths[0] as NSIndexPath
let vc = segue.destinationViewController as! NewViewController
vc.image = self.imageArray[indexPath.row]!
vc.title = self.appleProducts[indexPath.row]
}
}
}
CollectionViewCell.swift
import UIKit
class CollectionViewCell: UICollectionViewCell
{
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
}
NewViewController.swift
import UIKit
class NewViewController: UIViewController
{
@IBOutlet weak var imageView: UIImageView!
var image = UIImage()
override func viewDidLoad()
{
super.viewDidLoad()
self.imageView.image = self.image
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
在点击后显示的 View Controller 中,您应该制作一个水平滚动的UICollectionView
。在此 Collection View 上,将其 pagingEnabled
属性设置为 true。
接下来要做的是在父 View Controller 中重新格式化 prepareForSegue
方法,以将整个图像数组传递给呈现的 View Controller 以及选定的索引路径。
然后,在您呈现的 View Controller 中,您应该将 Collection View 的数据源配置为显示所有图像,但确保从父 View Controller 传递的初始选定索引路径开始。
关于ios - UICollectionView 快速滚动/滑动到下一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37501901/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) | Powered by Discuz! X3.4 |