swift - 我无法设置背景图片(Swift)
问题描述
我做了一个简单的计时器应用程序,我想在后台添加背景图片或视频。但是,我不能这样做,我不知道为什么。
谁能告诉我为什么图片没有出现在背景中?viewDidLoad
我在方法中设置了背景。也许@IBOutlet var backgroundView: UIView!
部分是错误的?UIView
当我制作这个 Xcode 项目时,我将它与给定的连接起来。
class ViewController: UIViewController, UITableViewDelegate {
let mainStopwatch = Stopwatch()
let lapStopwatch = Stopwatch()
var isPlay: Bool = false
var laps: [String] = []
//UI components
@IBOutlet weak var lapRestButton: UIButton!
@IBOutlet weak var playPauseButton: UIButton!
@IBOutlet weak var lapTimerLabel: UILabel!
@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var lapsTableView: UITableView!
@IBOutlet var backgroundView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
self.view.addBackground(name: "nathan-dumlao-iXXyrEwZgiU-unsplash")
lapRestButton.isEnabled = false
lapsTableView.dataSource = self
lapsTableView.delegate = self
}
//UI Settings
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait
}
//Actions
@IBAction func playPauseTimer(_ sender: Any) {
lapRestButton.isEnabled = true
changeButton(lapRestButton, title: "Lap", color: .black)
}
}
这是另一个用于设置背景图片的 swift 文件。
extension UIView {
func addBackground(name: String) {
let width = UIScreen.main.bounds.size.width
let height = UIScreen.main.bounds.size.height
let imageViewBackground = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
imageViewBackground.image = UIImage(named: name)
imageViewBackground.contentMode = .scaleAspectFill
self.addSubview(imageViewBackground)
self.sendSubviewToBack(imageViewBackground)
}
解决方案
var timer = Timer()
var counter = 0 // the counter is use for the index of array in below code
var imageArr = ["image","image1","image2"] //Image arr
override func viewDidLoad() {
super.viewDidLoad()
self.addBackground(name: "image") // set default image on your view
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true) // timer will start and it will change view background image after one min you can change time according to your requirement.
}
// called every time interval from the timer
@objc func timerAction() {
self.addBackground(name: self.imageArr[counter]) // here we are change our view background image when timer function is run on every min
counter += 1 // every-time counter will increment by one so the image array index will also increment and we will get our new image
}
// that's your code only you need to change one line that last one
func addBackground(name: String) {
let width = UIScreen.main.bounds.size.width
let height = UIScreen.main.bounds.size.height
let imageViewBackground = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
imageViewBackground.image = UIImage(named: name)
imageViewBackground.contentMode = .scaleAspectFit
self.viewref.addSubview(imageViewBackground) // here we will set the image view to our view
}
希望这将帮助您改变您的视图背景。谢谢。
推荐阅读
- firebase - 自定义角色 - Firebase 控制台
- r - 使用 dplyr 从一个数据帧创建数据帧列表
- python - 在类中使用 next() 函数
- c++ - 二维字符数组的膨胀/腐蚀
- autocomplete - HERE 自动完成 API:非常不准确的结果,忽略门牌号码
- python-3.x - 无法将字符串转换为十六进制 int 以在 chr() 中使用
- java - removeSpan(styleSpans[i]) 从样式文本中删除以前的样式
- php - 按组数据表计算行数
- docker - Docker compose 在烧瓶应用程序启动之前完全启动 mysql db
- django - 如何查找字符串是否在 Django 中的查询集的结果中