2017-08-04 74 views
1

我有一個具有圖像數組的集合視圖。當我按下任何圖像時,它將在另一個課程中全屏顯示該圖像。我試圖在第二個視圖控制器中添加滑動手勢識別器,但我不知道如何訪問第一個視圖控制器中的數組。訪問從1個視圖控制器到另一個圖像陣列,添加滑動手勢識別器

這是我顯示的圖像集合視圖

class sowrController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{ 


    @IBOutlet weak var collectionView: UICollectionView! 

    var albums = [AlbumModel]() 
    let db : DBHelperMo2lfat = DBHelperMo2lfat() 
    var selectedIndex : Int = -1 


    var posts : Post! 

    override func viewDidLoad() { 
     super.viewDidLoad() 



     collectionView.delegate = self 
     collectionView.dataSource = self 
     self.albums.removeAll() 
     self.albums.append(contentsOf: self.db.fetchAllImages()) 
     self.collectionView.reloadData() 
     DataService.ds.REF_POSTS_SOWR.observe(.value, with: { (snapshot) in 

      if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] { 
       self.albums.removeAll() 
       for snap in snapshot { 
        print ("SNAP: \(snap)") 

        if let postDict = snap.value as? Dictionary<String, AnyObject>{ 
         let album : AlbumModel = AlbumModel(id: postDict["id"] as! String, name: postDict["image_name"] as! String, path: postDict["image_path"] as! String, url: postDict["image_path"] as! String, localPath: "") 

         if let items = snap.children.allObjects as? [FIRDataSnapshot] { 
          for itemSnap in items { 
           if let albumSnap = itemSnap.value as? Dictionary<String, AnyObject> { 
            album.childAlbums.append(AlbumModel(id: albumSnap["id"] as! String, name: albumSnap["image_name"] as! String, path: albumSnap["image_path"] as! String, url: albumSnap["image_path"] as! String, localPath: "")) 
           } 
          } 
         } 
         self.albums.append(album) 

        } 
       } 
       self.collectionView.reloadData() 
      } 

     }) 

    } 


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 


     return self.albums.count 

    } 

    func numberOfSections(in collectionView: UICollectionView) -> Int { 
     return 1 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Constants.BookCellReuseIdentifier, for: indexPath) as? collectionViewCellSowr { 

      let album = albums[indexPath.item] 
      cell.initWithAlbumModel(album: album) 

      return cell 
     }else { 
      return collectionViewCellSowr() 
     } 

    } 

    private struct Constants { 
     static let BookCellReuseIdentifier = "cell" 
    } 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     self.selectedIndex = indexPath.row 
     self.performSegue(withIdentifier: "showAlbum", sender: self) 
    } 


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
    { 
     if segue.identifier == "showAlbum" 
     { 

      let vc = segue.destination as! imageFullScreen 

      vc.images = self.albums[self.selectedIndex] 

     } 
    } 

這是第二個視圖控制器,使圖像在全屏幕去

class imageFullScreen: UIViewController{ 


    var images : AlbumModel? 
    let db : DBHelperMo2lfat = DBHelperMo2lfat() 

    @IBAction func pictureSwipe(_ sender: Any) { 

    } 

    @IBOutlet weak var caption: UILabel! 
    @IBOutlet weak var imageView: UIImageView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.caption.text = images?.imageName 
     let url = URL(string: (images?.imagePath)!) 
     self.imageView.sd_setImage(with: url, placeholderImage: nil, options: [.progressiveDownload,.retryFailed]) 
    } 

回答

0

編輯:

好了,所以這裏是創建圖像視圖作爲一個子視圖和響應滑動手勢集合視圖控制器。請確保您的資產文件夾中有兩張圖像「圖像」和「圖像-1」。

// 
// CollectionViewController.swift 
// test 
// 
// Created by Yonatan Vainer on 05/08/2017. 
// Copyright © 2017 Sensus Healthcare LLC. All rights reserved. 
// 

import UIKit 

private let reuseIdentifier = "id" 

class CollectionViewController: UICollectionViewController { 

    var imageView = UIImageView(frame: CGRect(x: 0, y: 100, width: 300, height: 300)) 
    var index = 0; 
    let names = ["Image","Image-1"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     //For left swipe 
     let left = UISwipeGestureRecognizer(target: self, action: #selector(self.goLeft(_:))) 
     left.direction = .left 
     imageView.addGestureRecognizer(left) 

     //For right swipe 
     let right = UISwipeGestureRecognizer(target: self, action: #selector(self.goRight(_:))) 
     right.direction = .right 
     imageView.addGestureRecognizer(right) 

     imageView.isUserInteractionEnabled = true 

     self.view.addSubview(imageView) 
     self.view.layoutSubviews() 

     // Uncomment the following line to preserve selection between presentations 
     // self.clearsSelectionOnViewWillAppear = false 

     // Register cell classes 


     // Do any additional setup after loading the view. 
    } 

    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 prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     // Get the new view controller using [segue destinationViewController]. 
     // Pass the selected object to the new view controller. 
    } 
    */ 

    // MARK: UICollectionViewDataSource 

    override func numberOfSections(in collectionView: UICollectionView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return 1 
    } 


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     // #warning Incomplete implementation, return the number of items 
     return names.count 
    } 

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) 

     // Configure the cell 
     let nail = UIImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) 
     nail.image = UIImage(named: names[indexPath.row]) 

     cell.backgroundView = nail 
     return cell 
    } 

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     imageView.image = UIImage(named: names[indexPath.row]) 
     index = indexPath.row 
    } 

    func goLeft(_ gesture: UISwipeGestureRecognizer){ 
     index += 1 
     if index<0{ 
      index = 0 
     } 
     imageView.image = UIImage(named: names[index]) 
    } 

    func goRight(_ gesture: UISwipeGestureRecognizer){ 
     index -= 1 
     if index>1{ 
      index = 1 
     } 
     imageView.image = UIImage(named: names[index]) 
    } 

    // MARK: UICollectionViewDelegate 

    /* 
    // Uncomment this method to specify if the specified item should be highlighted during tracking 
    override func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool { 
     return true 
    } 
    */ 

    /* 
    // Uncomment this method to specify if the specified item should be selected 
    override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool { 
     return true 
    } 
    */ 

    /* 
    // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item 
    override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool { 
     return false 
    } 

    override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool { 
     return false 
    } 

    override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) { 

    } 
    */ 

} 

=========================================== =======================

在故事板中,單擊您的收藏視圖並嵌入導航控制器。 這將添加一個頂部欄和後退按鈕。 附加enter image description here圖片。

+0

我想要做的是在圖像數組之間滾動左右,類似於任何圖像庫 – abdo

+0

好的。有幾個選項。這裏是一個:在這種情況下,不要使用第二個視圖控制器。在集合視圖控制器內的任何單元格上單擊,以編程方式創建UIImageView,用圖像填充它,通過分配寬度和高度將其放入全屏,然後將其添加爲子視圖。然後,當您獲得滑動手勢時,只需分配不同的圖像並重新加載視圖即可。 –

+0

這是一個好主意,你能幫我做嗎? – abdo

1

我不知道第一個視圖控制器我完全理解你的問題,因爲我不明白數組與手勢識別器有什麼關係,但是如果你只是想從前一個ViewController訪問數組,那麼這應該工作如果你有一個導航控制器

let vcIndex = self.navigationController?.viewControllers.index(where: { (viewController) -> Bool in 

      if let _ = viewController as? sowrController { 
       return true 
      } 
      return false 
}) 

let prevVC = self.navigationController?.viewControllers[vcIndex!] as! sowrController 
let albums:[AlbumModel] = prevVC.albums 
+0

我想在圖片之間左右滑動 – abdo

+0

@abdo查看此鏈接(https://stackoverflow.com/questions/28123895/how-do-i-give-users-the-ability-to-輕掃貫通的陣列-圖像合迅速的)。鄧肯C的回答正是你需要的。 – RPatel99

相關問題