为什么 UIImage 不会改变?
Why won't the UIImage change?
我查阅了很多资源,none 对我有用。我正在尝试根据星期几更改图像。我在操场上测试了日历方法,它会 return 正确的预期数字。但是,我对 Swift 和编程以及一般知识还很陌生,对于为什么 imageChange 方法不会改变我一无所知。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var schedule: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
var monday = UIImage(named: "monday")
var tuesday = UIImage(named: "tuesday")
var wednesday = UIImage(named: "wednesday")
var thursday = UIImage(named: "thurday")
var friday = UIImage(named: "friday")
var other = UIImage(named:"other")
func calendar()->Int{
let todayDate = NSDate()
let myCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)
let myComponents = myCalendar?.components(.WeekdayCalendarUnit, fromDate: todayDate)
let weekDay = myComponents?.weekday
return weekDay!
}
func imageChange() {
if calendar() == 2 {
schedule.image = monday
}
else if calendar() == 3 {
schedule.image = tuesday
}
else if calendar() == 4 {
schedule.image = wednesday
}
else if calendar() == 5 {
schedule.image = thursday
}
else if calendar() == 6 {
schedule.image = friday
}
else if calendar() == 7 {
schedule.image = other
}
else if calendar() == 1 {
schedule.image = other
}
else {
schedule.image = other
}
}
}
您可以让您的函数更短更简单:
//Add all your images to an array(other two times for saturday and sunday)
//0 = saturday, 1 = sunday, 2 = monday etc
var imageArray = [other, other, monday, tuesday, wednesday, thursday, friday]
func imageChange() {
//Call the function only one time. No need to call it each time you test
var calendarDay = calendar()
//You want to know which image you need from your array. -1 because an array starts at 0
var imageIndex = calendarDay - 1
schedule.image = imageArray[imageIndex]
}
要调用您的函数,您必须从函数内部进行。您不能 运行 仅来自 class 主体的函数。因此,例如在您的 viewDidLoad
方法中:
override func viewDidLoad() {
super.viewDidLoad()
imageChange()
}
我认为您的问题可能出在这段代码中:
var monday = UIImage(named: "monday")
var tuesday = UIImage(named: "tuesday")
var wednesday = UIImage(named: "wednesday")
var thursday = UIImage(named: "thurday")
var friday = UIImage(named: "friday")
var other = UIImage(named:"other")
您可能需要说明图片的全名。例如,而不是:
var monday = UIImage(named: "monday")
您的代码可能必须是:
1) var monday = UIImage(named: "monday.png")
2) var monday = UIImage(named: "monday.jpg")
或任何其他图像类型。对所有图像执行此操作。
希望这能帮助您解决问题。
我查阅了很多资源,none 对我有用。我正在尝试根据星期几更改图像。我在操场上测试了日历方法,它会 return 正确的预期数字。但是,我对 Swift 和编程以及一般知识还很陌生,对于为什么 imageChange 方法不会改变我一无所知。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var schedule: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
var monday = UIImage(named: "monday")
var tuesday = UIImage(named: "tuesday")
var wednesday = UIImage(named: "wednesday")
var thursday = UIImage(named: "thurday")
var friday = UIImage(named: "friday")
var other = UIImage(named:"other")
func calendar()->Int{
let todayDate = NSDate()
let myCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)
let myComponents = myCalendar?.components(.WeekdayCalendarUnit, fromDate: todayDate)
let weekDay = myComponents?.weekday
return weekDay!
}
func imageChange() {
if calendar() == 2 {
schedule.image = monday
}
else if calendar() == 3 {
schedule.image = tuesday
}
else if calendar() == 4 {
schedule.image = wednesday
}
else if calendar() == 5 {
schedule.image = thursday
}
else if calendar() == 6 {
schedule.image = friday
}
else if calendar() == 7 {
schedule.image = other
}
else if calendar() == 1 {
schedule.image = other
}
else {
schedule.image = other
}
}
}
您可以让您的函数更短更简单:
//Add all your images to an array(other two times for saturday and sunday)
//0 = saturday, 1 = sunday, 2 = monday etc
var imageArray = [other, other, monday, tuesday, wednesday, thursday, friday]
func imageChange() {
//Call the function only one time. No need to call it each time you test
var calendarDay = calendar()
//You want to know which image you need from your array. -1 because an array starts at 0
var imageIndex = calendarDay - 1
schedule.image = imageArray[imageIndex]
}
要调用您的函数,您必须从函数内部进行。您不能 运行 仅来自 class 主体的函数。因此,例如在您的 viewDidLoad
方法中:
override func viewDidLoad() {
super.viewDidLoad()
imageChange()
}
我认为您的问题可能出在这段代码中:
var monday = UIImage(named: "monday")
var tuesday = UIImage(named: "tuesday")
var wednesday = UIImage(named: "wednesday")
var thursday = UIImage(named: "thurday")
var friday = UIImage(named: "friday")
var other = UIImage(named:"other")
您可能需要说明图片的全名。例如,而不是:
var monday = UIImage(named: "monday")
您的代码可能必须是:
1) var monday = UIImage(named: "monday.png")
2) var monday = UIImage(named: "monday.jpg")
或任何其他图像类型。对所有图像执行此操作。
希望这能帮助您解决问题。