ios - UICollectionView 快速滚动/滑动到下一个项目
<p><p>我在 ViewController 中有一个 UICollectionView。当用户单击其中一个图像时,它会转到一个单独的页面以打开该图像。我不想每次都回到画廊更换图片,而是向左或向右滑动以获取下一张或上一张图片。 </p>
<p>我已经看到了“pagingEnabled = true”的答案,但是你把它放在哪里呢? </p>
<p>我是 swift 新手。 </p>
<p>下面是我的代码。我在这里先向您的帮助表示感谢。 </p>
<p>ViewController.swift</p>
<pre><code>import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate
{
@IBOutlet weak var collectionView: UICollectionView!
let appleProducts = ["iPhone", "Apple Watch", "Mac", "iPad"]
let imageArray =
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
cell.titleLabel?.text = self.appleProducts
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 as NSIndexPath
let vc = segue.destinationViewController as! NewViewController
vc.image = self.imageArray!
vc.title = self.appleProducts
}
}
}
</code></pre>
<p>CollectionViewCell.swift</p>
<pre><code>import UIKit
class CollectionViewCell: UICollectionViewCell
{
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
}
</code></pre>
<p>NewViewController.swift</p>
<pre><code>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.
}
*/
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>在点击后显示的 ViewController 中,您应该制作一个水平滚动的<code>UICollectionView</code>。在此 Collection View 上,将其 <code>pagingEnabled</code> 属性设置为 true。</p>
<p>接下来要做的是在父 ViewController 中重新格式化 <code>prepareForSegue</code> 方法,以将整个图像数组传递给呈现的 ViewController 以及选定的索引路径。 </p>
<p>然后,在您呈现的 ViewController 中,您应该将 Collection View 的数据源配置为显示所有图像,但确保从父 ViewController 传递的初始选定索引路径开始。</p ></p>
<p style="font-size: 20px;">关于ios - UICollectionView 快速滚动/滑动到下一个项目,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/37501901/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/37501901/
</a>
</p>
页:
[1]