我试图通过定义应由我显示什么图像并且应用程序正常工作的定时事件,将UIImageView中显示的图像切换为不同的图像,但是当计时器变为5时,应用程序崩溃。如果有人可以帮助我,那就太好了(广告也是图片名称的开头,所以AD_1-AD_4)
import UIKit
class ViewController: UIViewController {
//this is for declaring all the profile buttons
@IBOutlet weak var Icon_1: UIButton!
@IBOutlet weak var Icon_2: UIButton!
@IBOutlet weak var Icon_3: UIButton!
@IBOutlet weak var Icon_4: UIButton!
@IBOutlet weak var Icon_5: UIButton!
@IBOutlet weak var Icon_6: UIButton!
@IBOutlet weak var Icon_7: UIButton!
@IBOutlet weak var Icon_8: UIButton!
//this is used to swich the images
var i = Int(1)
let timer = Timer.scheduledTimer(timeInterval: 5.0,target: self,selector: #selector(fire),userInfo: nil,repeats: false)
//this is for the view
@IBOutlet weak var ad_view: UIView!
//this is for when the user swipes left
@IBaction func Icon_Swipe_Left(_ sender: Any) {
Icon_1.isHidden = true
Icon_2.isHidden = true
Icon_3.isHidden = true
Icon_4.isHidden = true
Icon_5.isHidden = false
Icon_6.isHidden = false
Icon_7.isHidden = false
Icon_8.isHidden = false
}
//this is when the user swipes right
@IBaction func Icon_Swipe_Right(_ sender: Any) {
Icon_1.isHidden = false
Icon_2.isHidden = false
Icon_3.isHidden = false
Icon_4.isHidden = false
Icon_5.isHidden = true
Icon_6.isHidden = true
Icon_7.isHidden = true
Icon_8.isHidden = true
}
// this is for decareing the image frame for the adds
@IBOutlet weak var Ad_Frame: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//this is for showing the fist set of icons
Icon_1.isHidden = false
Icon_2.isHidden = false
Icon_3.isHidden = false
Icon_4.isHidden = false
//this is for hideing the second set of icons
Icon_5.isHidden = true
Icon_6.isHidden = true
Icon_7.isHidden = true
Icon_8.isHidden = true
self.Ad_Frame.image = UIImage(named: "AD_\(i)")
}
@objc func fire()
{
i += 1
self.Ad_Frame.image = UIImage(named: "AD_\(i)")
}
}