mardi 5 mai 2015

NSTimer starts, stops, and continues after window closes

So here is what's happening in human logic (but not suppose to be).

  1. My View controller loads
  2. retrieves my images from Parse successfully
  3. sets the first retrieved image equal to my UIImageView image
  4. starts my timer of 5 Seconds (I think)
  5. And then it doesn't do anything, just remains on the very first image
  6. Once I close the window (which was presented modally by previous view controller), I see in the console my println() outputs and my timer begins to work.

The GOAL: To present an image in my UIImageView for 5 seconds or so, and then display the next one in line (imageArray).

Is it my timer that's causing this pause in function, or does it have something to do with how my code is arranged? All suggestions/help are very much appreciated as this is a very frustrating issue.

This is in Swift language, but I can read Objective-C.

Here is my code for the class:

//
//  ImageDisplayViewController.swift
//  
//
//  Created by on 5/3/15.
//
//

import Cocoa
import ParseOSX

class ImageDisplayViewController: NSViewController {


@IBOutlet weak var imageView: NSImageView!

var venue: String!
var imageArray = [(Int, NSImage)]()
var image: NSImage!
var timeToDisplay: Int = 0

var timer = NSTimer()

override func viewDidLoad() {
    super.viewDidLoad()

    venue = "Bees"
    loadImages()


}

var k: Int = 0

func presentNextImage() {
    if(k<imageArray.count) {
    imageView.image = imageArray[k].1
    timer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: Selector("presentNextImage"), userInfo: nil, repeats: false)
        println(timer.timeInterval)
        //imageView.image = imageArray[k].1
        println("k is \(k)")
        k++
        println("Just set a new imageView.image")
    } else {
        println("K is n longer <imageArray.count")
    }
}



func loadImages() {
    var query = PFQuery(className:groupz)
    query.whereKeyExists("imageFile")
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in

        if error == nil {
            println("Successfully retrieved \(objects!.count) objects.")
            // Do something with the found objects
            if let objects = objects as? [PFObject] {
                for object in objects {
                    let thumbNail = object["imageFile"] as! PFFile
                    self.image = NSImage(data: thumbNail.getData()!)
                    let time: AnyObject? = object["displayTime"]
                    self.timeToDisplay = time!.integerValue

                    let tempTuple = (self.timeToDisplay, self.image!)
                    self.imageArray.append(tempTuple)

                }
            }
            println("imageArray.count is: \(self.imageArray.count)")
            self.presentNextImage()
        } else {
            // Log details of the failure
            println("Error: \(error!) \(error!.userInfo!)")
        }
    }
}

}

Aucun commentaire:

Enregistrer un commentaire