2016-05-28 11 views
2

私はビューコントローラにUICollectionViewを持っています。ユーザーが画像の1つをクリックすると、別のページに移動して画像を開きます。私は、写真を変更するたびにギャラリーに戻ったり、左または右にスワイプして次の写真または前の写真を取得したりするのではなく、UICollectionViewスクロールして次のアイテムにスクロール

私は「pagingEnabled = true」を行うには答えを見てきましたが、どこに正確にこれを置けばいいの?

私は迅速です。

以下は私のコードです。事前に助けてくれてありがとう。タップした後に提示されたビューコントローラで

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

} 

答えて

0

、あなたは水平に行う必要がありますスクロールUICollectionView。このコレクションビューで、pagingEnabledプロパティをtrueに設定します。

次に、親View ControllerでprepareForSegueメソッドを再フォーマットして、イメージの配列全体を、提示されたView Controllerと選択したインデックスパスに渡します。

次に、表示されたビューコントローラでは、すべてのイメージを表示するようにコレクションビューのデータソースを構成する必要がありますが、親ビューコントローラによって渡された最初の選択インデックスパスから開始してください。

関連する問題