简体   繁体   English

UICollectionView滚动/滑动到下一个项目

[英]UICollectionView scrolling/swipe to next item swift

I have a UICollectionView in a view controller. 我在视图控制器中有一个UICollectionView。 When the user clicks one of the images, it goes to a separate page to open the image. 当用户单击其中一张图像时,它将转到一个单独的页面以打开图像。 I would like instead of going back to the gallery each time to change pictures, to swipe left or right to get the next or previous picture. 我不想每次都回到画廊来更改图片,而是向左或向右滑动以获得下一张或上一张图片。

I have seen answers to do "pagingEnabled = true" but where do you put this exactly? 我已经看到了执行“ pagingEnabled = true”的答案,但是您将其放在哪里呢?

I am new to swift. 我是新手。

Below is my code. 下面是我的代码。 Thanks for the help in advance. 我在这里先向您的帮助表示感谢。

ViewController.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 CollectionViewCell.swift

import UIKit

class CollectionViewCell: UICollectionViewCell
{
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
}

NewViewController.swift 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.
}
*/

}

In the view controller that is presented after a tap, you should make a horizontally scrolling UICollectionView . 在轻按后显示的视图控制器中,应进行水平滚动UICollectionView On this collection view, set its pagingEnabled property to true. 在此集合视图上,将其pagingEnabled属性设置为true。

The next thing to do is reformat your prepareForSegue method in your parent view controller to pass the entire array of images to the presented view controller as well as the selected index path. 接下来要做的是在父视图控制器中重新格式化您的prepareForSegue方法,以将整个图像数组传递给所呈现的视图控制器以及选定的索引路径。

Then, in your presented view controller, you should have the collection view's data source be configured to show all the images but make sure to start at the initial, selected index path that was passed by the parent view controller. 然后,在显示的视图控制器中,应该将集合视图的数据源配置为显示所有图像,但确保从父视图控制器传递的初始选定索引路径开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM