2016-07-24 28 views
1

Ich verwende das INSPhotoGallery-Framework (https://github.com/inspace-io/INSPhotoGallery), die ein Fotogallerie-Framework ist. Ich möchte beim Start eine bestimmte Methode aufrufen, die eine Vorschau beim Start statt der Übersicht/Tabellenansicht erzeugt. Ich möchte das Vorschaubild der ersten Zelle anzeigen (das beim Klicken auf das Vorschaubild angezeigt wird).So rufen Sie eine Funktion von einer Erweiterung in Swift

// 
// ViewController.swift 
// INSPhotoGallery 
// 
// Created by Michal Zaborowski on 04.04.2016. 
// Copyright © 2016 Inspace Labs Sp z o. o. Spółka Komandytowa. All rights reserved. 
// 

import UIKit 
import INSPhotoGalleryFramework 

class ViewController: UIViewController { 

    @IBOutlet weak var collectionView: UICollectionView! 
    var useCustomOverlay = false 



    lazy var photos: [INSPhotoViewable] = { 
     return [ 
      INSPhoto(imageURL: NSURL(string: "http://i.imgur.com/JXY2d4A.jpg"), thumbnailImageURL: NSURL(string: "http://i.imgur.com/JXY2d4A.jpg")), 
      INSPhoto(imageURL: NSURL(string: "http://i.imgur.com/NC4bqLB.jpg"), thumbnailImageURL: NSURL(string: "http://i.imgur.com/NC4bqLB.jpg")), 
      INSPhoto(imageURL: NSURL(string: "http://i.imgur.com/jBbQXNz.jpg"), thumbnailImageURL: NSURL(string: "http://i.imgur.com/jBbQXNz.jpg")), 
      INSPhoto(imageURL: NSURL(string: "http://i.imgur.com/WCXkdwW.jpg"), thumbnailImageURL: NSURL(string: "http://i.imgur.com/WCXkdwW.jpg")), 
      INSPhoto(imageURL: NSURL(string: "http://i.imgur.com/p7ujK0t.jpg"), thumbnailImageURL: NSURL(string: "http://i.imgur.com/p7ujK0t.jpg")), 
      INSPhoto(imageURL: NSURL(string: "http://i.imgur.com/Ak4qwsS.jpg"), thumbnailImageURL: NSURL(string: "http://i.imgur.com/Ak4qwsS.jpg")), 
      INSPhoto(imageURL: NSURL(string: "http://i.imgur.com/w2JJtDf.jpg"), thumbnailImageURL: NSURL(string: "http://i.imgur.com/w2JJtDf.jpg")), 
      INSPhoto(imageURL: NSURL(string: "http://i.imgur.com/HCCSco3.jpg"), thumbnailImageURL: NSURL(string: "http://i.imgur.com/HCCSco3.jpg")), 
      INSPhoto(imageURL: NSURL(string: "http://i.imgur.com/Za6Ialf.jpg"), thumbnailImageURL: NSURL(string: "http://i.imgur.com/Za6Ialf.jpg")), 

      INSPhoto(imageURL: NSURL(string: "http://i.imgur.com/Pqc6k4v.jpg"), thumbnailImageURL: NSURL(string: "http://i.imgur.com/Pqc6k4v.jpg")), 
      INSPhoto(imageURL: NSURL(string: "http://i.imgur.com/D8BBMd4.jpg"), thumbnailImageURL: NSURL(string: "http://i.imgur.com/D8BBMd4.jpg")), 
      INSPhoto(imageURL: NSURL(string: "http://i.imgur.com/bggxrss.jpg"), thumbnailImageURL: NSURL(string: "http://i.imgur.com/bggxrss.jpg")), 
      INSPhoto(imageURL: NSURL(string: "http://i.imgur.com/w1Lnl2c.jpg"), thumbnailImageURL: NSURL(string: "http://i.imgur.com/w1Lnl2c.jpg")), 
      INSPhoto(imageURL: NSURL(string: "http://i.imgur.com/qoA0qA9.jpg"), thumbnailImageURL: NSURL(string: "http://i.imgur.com/qoA0qA9.jpg")), 
      INSPhoto(imageURL: NSURL(string: "http://i.imgur.com/DkCEfkw.jpg"), thumbnailImageURL: NSURL(string: "http://i.imgur.com/DkCEfkw.jpg")), 
      INSPhoto(imageURL: NSURL(string: "http://i.imgur.com/U4ihOo6.jpg"), thumbnailImageURL: NSURL(string: "http://i.imgur.com/U4ihOo6.jpg")), 
      INSPhoto(imageURL: NSURL(string: "http://i.imgur.com/QvLBs7A.jpg"), thumbnailImageURL: NSURL(string: "http://i.imgur.com/QvLBs7A.jpg")), 
      INSPhoto(imageURL: NSURL(string: "http://i.imgur.com/ZytdIk1.jpg"), thumbnailImageURL: NSURL(string: "http://i.imgur.com/ZytdIk1.jpg")), 

     ] 
    }() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     collectionView.delegate = self 
     collectionView.dataSource = self 

     for photo in photos { 
      if let photo = photo as? INSPhoto { 
       photo.attributedTitle = NSAttributedString(string: "Note: Click top right to download wallpaper, \nscroll left or right to browse", attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()]) 
      } 
     } 


    } 
} 

extension ViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("ExampleCollectionViewCell", forIndexPath: indexPath) as! ExampleCollectionViewCell 
     cell.populateWithPhoto(photos[indexPath.row]) 



     return cell 
    } 
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return photos.count 
    } 

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
     print(indexPath) 
     let cell = collectionView.cellForItemAtIndexPath(indexPath) as! ExampleCollectionViewCell 
     let currentPhoto = photos[indexPath.row] 
     let galleryPreview = INSPhotosViewController(photos: photos, initialPhoto: currentPhoto, referenceView: cell) 
     if useCustomOverlay { 
      galleryPreview.overlayView = CustomOverlayView(frame: CGRect.zero) 
     } 

     galleryPreview.referenceViewForPhotoWhenDismissingHandler = { [weak self] photo in 
      if let index = self?.photos.indexOf({$0 === photo}) { 
       let indexPath = NSIndexPath(forItem: index, inSection: 0) 
       return collectionView.cellForItemAtIndexPath(indexPath) as? ExampleCollectionViewCell 
      } 
      return nil 
     } 

dieses:

presentViewController(galleryPreview, animated: true, completion: nil) 
     } 
    } 
+0

Es ist nicht klar zu sein, was gefragt wird. Die 'self'-Variable ist in Erweiterungen verfügbar, sodass Sie die meisten Funktionen aufrufen können, die in einer Instanzmethode verfügbar wären. –

+0

Wie würde ich eine Zelleninstanz erstellen, damit ich die Funktion aufrufen kann? – Brandex07

Antwort

0

Sie können es in viewDidAppear präsentieren. Zum Beispiel:

override func viewDidAppear(animated: Bool) 
{ 
    super.viewDidAppear(animated) 
    let cell = self.collectionView.cellForItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 0)) 
    let currentPhoto = photos.first! 
    let galleryPreview = INSPhotosViewController(photos: photos \* if you only want to swipe the first image replace"photos" with "[photos.first!]", initialPhoto: currentPhoto, referenceView: cell) 

    galleryPreview.referenceViewForPhotoWhenDismissingHandler = { [weak self] photo in 
     if let index = self?.photos.indexOf({$0 === photo}) { 
      let indexPath = NSIndexPath(forItem: index, inSection: 0) 
      return self.collectionView.cellForItemAtIndexPath(indexPath) 
     } 
     return nil 
    } 
    self.presentViewController(galleryPreview, animated: true, completion: nil) 
} 

Funktionierte das nicht für Sie?

+0

Danke das hat funktioniert :) – Brandex07

+0

Hallo, gibt es einen Weg, dass ich diese Methode in objektiven C-Code nennen kann. Ich habe einen schnellen Header generiert. Kann aber keine Methode finden, die Methode innerhalb der Erweiterung aufzurufen – kemdo

Verwandte Themen