延迟更改UIImageView.image

时间:2016-03-08 01:06:47

标签: ios xcode swift function swift2

好的,所以我试图通过创建一个存储UIImages数组的类来创建一个“幻灯片”,并且有一个函数可以从数组中选择一个随机索引然后分配关联图像与ViewController.h

中的图像视图
import UIKit
import AVFoundation

class ViewController: UIViewController {


var audioPlayer = AVAudioPlayer()
var audioPath = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("1-08 Patient Love", ofType: "mp3")!)
var playPauseBtn = UIBarButtonItem()
var musicPaused: Bool = false

@IBOutlet var toolbar: UIToolbar!  
@IBOutlet var bg1: UIImageView!
@IBOutlet var bg2: UIImageView!
@IBOutlet var bg3: UIImageView!
@IBOutlet var bg4: UIImageView!
@IBOutlet var bg5: UIImageView!
@IBOutlet var bg6: UIImageView!
@IBOutlet var bg7: UIImageView!
@IBOutlet var bg8: UIImageView!
@IBOutlet var bg9: UIImageView!
@IBOutlet var bg10: UIImageView!
@IBOutlet var bg11: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    do{
        audioPlayer = try AVAudioPlayer(contentsOfURL: audioPath)}
    catch {
            print("Audio file not found.")
        }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func playPause(sender: UIBarButtonItem){

    var items = toolbar.items!

    if musicPaused == false {

        playPauseBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "playPause:")

        items[0] = playPauseBtn

        toolbar.setItems(items, animated: true)

        audioPlayer.play()
        musicPaused = true

    }else{

        var items = toolbar.items!

        playPauseBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playPause:")
        items [0] = playPauseBtn
        toolbar.setItems(items, animated: true)

        audioPlayer.pause()
        musicPaused = false
    }
}

@IBAction func stopButton(sender: AnyObject) {

    var items = toolbar.items!
    audioPlayer.stop()
    audioPlayer.currentTime = 0
    playPauseBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playPause:")
    items[0] = playPauseBtn
    toolbar.setItems(items, animated: true)
    musicPaused = false
}
}

这是我的视图controller.swift文件。我还没有参考我正在尝试在这里的slideshow.swift文件中编写的函数

import UIKit

var imageArray: [UIImage] = [
UIImage(named: "dec11.jpg")!,
UIImage(named: "dec18.jpg")!,
UIImage(named: "dec19.jpg")!,
UIImage(named: "dec19-2.jpg")!,
UIImage(named: "dec24.jpg")!,
UIImage(named: "dec25.jpg")!,
UIImage(named: "dec30.jpg")!,
UIImage(named: "jan1.jpg")!,
UIImage(named: "jan20.jpg")!,
UIImage(named: "jan24.jpg")!,
UIImage(named: "jan30.jpg")!
]

幻灯片课程

class slideshow: NSObject {

func changeImage() -> UIImage{
    //wait 2 secs
    let seconds = 2.0
    let delay = seconds * Double(NSEC_PER_SEC)  // nanoseconds per seconds
    let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))

    dispatch_after(dispatchTime, dispatch_get_main_queue(), {

        //here code perfomed with delay
        //generate random number
        let randomIndex = Int(arc4random_uniform(11))

        var imageAtIndex = imageArray[randomIndex]

        return imageAtIndex
    })
}
}

在视图控制器中编写全部内容会更容易吗?我根据其他用户的建议更改了函数声明,但我仍然遇到同样的错误。

2 个答案:

答案 0 :(得分:1)

您无法对变量image进行两次声明。你有一个传递给函数:

@IBAction func changeImage (image: UIImage){

另一个是延迟行动的一部分:

    //return image at index
    var image = imageAtIndex

如果你的数组包含UIImageView,你不想要返回图像,你想要将UIImage的图像属性设置为数组中的图像。

所以将函数decalaration更改为

func changeImage (imageViewToChange: UIImageView){

然后将dispatch_after代码更改为

dispatch_after(dispatchTime, dispatch_get_main_queue(), {

    //here code performed after delay
    //generate random number
    let randomIndex = Int(arc4random_uniform(11))

    //return image at index
    imageViewToChange.image = imageArray[randomIndex]
})

答案 1 :(得分:0)

以下代码未经测试,但您应该可以执行以下操作。幻灯片放映类包含具有启动和停止方法的可能图像列表。 animate方法以递归方式调用,因此您不必每次都从vc中调用它。

import UIKit
import AVFoundation

class ViewController: UIViewController {

var audioPlayer = AVAudioPlayer()
var audioPath = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("1-08 Patient Love", ofType: "mp3")!)
var playPauseBtn = UIBarButtonItem()
var musicPaused: Bool = false

@IBOutlet var toolbar: UIToolbar!  
@IBOutlet var bg: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    do{
        audioPlayer = try AVAudioPlayer(contentsOfURL: audioPath)}
    catch {
            print("Audio file not found.")
        }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func playPause(sender: UIBarButtonItem){

    var items = toolbar.items!

    if musicPaused == false {

        playPauseBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "playPause:")

        items[0] = playPauseBtn

        toolbar.setItems(items, animated: true)

        audioPlayer.play()
        musicPaused = true
        slideshow.startSlideshow(bg)

    }else{

        var items = toolbar.items!

        playPauseBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playPause:")
        items [0] = playPauseBtn
        toolbar.setItems(items, animated: true)

        audioPlayer.pause()
        slideshow.stopSlideshow()
        musicPaused = false
    }
}

@IBAction func stopButton(sender: AnyObject) {

    var items = toolbar.items!
    audioPlayer.stop()
    audioPlayer.currentTime = 0
    playPauseBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playPause:")
    items[0] = playPauseBtn
    toolbar.setItems(items, animated: true)
    musicPaused = false
    slideshow.stopSlideshow()
}
}


class slideshow: NSObject {

var imageArray: [UIImage] = [
UIImage(named: "dec11.jpg")!,
UIImage(named: "dec18.jpg")!,
UIImage(named: "dec19.jpg")!,
UIImage(named: "dec19-2.jpg")!,
UIImage(named: "dec24.jpg")!,
UIImage(named: "dec25.jpg")!,
UIImage(named: "dec30.jpg")!,
UIImage(named: "jan1.jpg")!,
UIImage(named: "jan20.jpg")!,
UIImage(named: "jan24.jpg")!,
UIImage(named: "jan30.jpg")!
]
var shouldAnimate: boolean?

func startSlideshow(imageView: UIImageView) {
    shouldAnimate = true
    self.animate()
}

func stopSlideshow() {
    shouldAnimate = false
}

func animate() {
        //wait 2 secs
    let seconds = 2.0
    let delay = seconds * Double(NSEC_PER_SEC)  // nanoseconds per seconds
    let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))

    dispatch_after(dispatchTime, dispatch_get_main_queue(), {

        //here code perfomed with delay
        //generate random number
        let randomIndex = Int(arc4random_uniform(11))

        var imageAtIndex = imageArray[randomIndex]

        imageView.image = imageAtIndex
        if (shouldAnimate) {
          self.animate()
        }
    })
  }
}