gpt4 book ai didi

ios - 延迟更改 UIImageView.image

转载 作者:行者123 更新时间:2023-11-29 01:10:09 24 4
gpt4 key购买 nike

好的,所以我正在尝试通过创建一个类来创建各种“幻灯片”,该类将存储 UIImages 数组,并具有一个从数组中选择随机索引的函数然后将关联图像分配给ViewController.h

中的 ImageView
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
}
}

这是我的 View 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
})
}
}

将这些全部写在 View Controller 中会更容易吗?我根据其他用户的建议更改了函数声明,但仍然遇到相同的错误。

最佳答案

变量 image 不能有两个声明。你有一个传递给函数的:

@IBAction func changeImage (image: UIImage){

还有一个是你延迟行动的一部分:

    //return image at index
var image = imageAtIndex

您不想返回图像,而是想将 UIImageView 的图像属性设置为数组中的图像,假设您的数组包含 UIImage

所以把你的函数声明改成

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]
})

关于ios - 延迟更改 UIImageView.image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35857132/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com