首先,我想为我的无知道歉,因为我刚刚开始学习如何编码。我正在开发一个基本的测验应用程序。我的问题是如何根据具体情况为每个问题添加图像?我想要的是为每个问题显示不同的图像。
到目前为止,这是我的代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet var QuestionLabel: UILabel!
@IBOutlet var Button1: UIButton!
@IBOutlet var Button2: UIButton!
@IBOutlet var Button3: UIButton!
@IBOutlet var Button4: UIButton!
@IBOutlet var LabelEnd: UILabel!
var CorrectAnswer = String()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Hide()
RandomQuestions()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func RandomQuestions(){
var RandomNumber = arc4random() % 4
RandomNumber += 1
switch (RandomNumber){
case 1:
QuestionLabel.text = "Guess the Athlete"
Button1.setTitle("Kobe Bryant", forState: UIControlState.Normal)
Button2.setTitle("Lebron James", forState: UIControlState.Normal)
Button3.setTitle("Magic Johnson", forState: UIControlState.Normal)
Button4.setTitle("Shaq", forState: UIControlState.Normal)
CorrectAnswer = "1"
case 2:
QuestionLabel.text = "Guess the Athlete"
Button1.setTitle("Paul Gasol", forState: UIControlState.Normal)
Button2.setTitle("Allen Iverson", forState: UIControlState.Normal)
Button3.setTitle("Tim Duncan", forState: UIControlState.Normal)
Button4.setTitle("Jeremy Lin", forState: UIControlState.Normal)
CorrectAnswer = "1"
break
case 3:
QuestionLabel.text = "Guess the Athlete"
Button1.setTitle("Blake Griffin", forState: UIControlState.Normal)
Button2.setTitle("Chris Paul", forState: UIControlState.Normal)
Button3.setTitle("Shaq", forState: UIControlState.Normal)
Button4.setTitle("Tony Parker", forState: UIControlState.Normal)
CorrectAnswer = "2"
break
case 4:
QuestionLabel.text = "Guess the Athlete"
Button1.setTitle("Dirk Nowitzki", forState: UIControlState.Normal)
Button2.setTitle("JJ Redick", forState: UIControlState.Normal)
Button3.setTitle("Ray Allen", forState: UIControlState.Normal)
Button4.setTitle("Chris Anderson", forState: UIControlState.Normal)
CorrectAnswer = "4"
break
default:
break
}
}
var timer = NSTimer()
func Hide() {
LabelEnd.hidden = true
timer.invalidate() //You can remove timer here.
}
func UnHide(){
LabelEnd.hidden = false
}
@IBAction func Button1Action(sender: AnyObject) {
UnHide()
if(CorrectAnswer == "1"){
LabelEnd.text = "Correct!"
LabelEnd.textColor = UIColor.greenColor()
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Hide"), userInfo: nil, repeats: false)
RandomQuestions()
}
else{
LabelEnd.text = "Wrong"
LabelEnd.textColor = UIColor.redColor()
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Hide"), userInfo: nil, repeats: false)
}
}
@IBAction func Button2Action(sender: AnyObject) {
UnHide()
if(CorrectAnswer == "2"){
LabelEnd.text = "Correct!"
LabelEnd.textColor = UIColor.greenColor()
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Hide"), userInfo: nil, repeats: false)
RandomQuestions()
}
else{
LabelEnd.text = "Wrong"
LabelEnd.textColor = UIColor.redColor()
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Hide"), userInfo: nil, repeats: false)
}
}
@IBAction func Button3Action(sender: AnyObject) {
UnHide()
if(CorrectAnswer == "3"){
LabelEnd.text = "Correct!"
LabelEnd.textColor = UIColor.greenColor()
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Hide"), userInfo: nil, repeats: false)
RandomQuestions()
}
else{
LabelEnd.text = "Wrong"
LabelEnd.textColor = UIColor.redColor()
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Hide"), userInfo: nil, repeats: false)
}
}
@IBAction func Button4Action(sender: AnyObject) {
UnHide()
if(CorrectAnswer == "4"){
LabelEnd.text = "Correct!"
LabelEnd.textColor = UIColor.greenColor()
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Hide"), userInfo: nil, repeats: false)
RandomQuestions()
}
else{
LabelEnd.text = "Wrong"
LabelEnd.textColor = UIColor.redColor()
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Hide"), userInfo: nil, repeats: false)
}
}
}
答案 0 :(得分:0)
您正在寻找的课程是UIImage
和UIImageView
。您可以在Interface Builder UIImageView
文件中添加xib
实例,就像按钮和标签一样。然后,您可以为其添加另一个@IBOutlet
属性,例如:
@IBOutlet var imageView: UIImageView!
接下来,您需要添加图片文件。我们假设您有一个名为" Kobe.png
"。将其拖放到Xcode项目中,或者放在源文件旁边,或者在项目中可能包含的Images.xcassets
文件中创建新的图像资源。资产方法更现代化,但它涉及的更多,将它拖入您的项目对初学者来说很好。
在项目中拥有该文件后,您可以将其作为图像对象访问,如下所示:
UIImage(named: "Kobe")
然后,要将它们放在一起,您可以将其分配给图像视图的image
属性:
imageView.image = UIImage(named: "Kobe")
答案 1 :(得分:0)
您需要创建UIImageView
插座才能显示图片
答案 2 :(得分:0)
我对Swift相当新......但在创建新的UIImageView
之后
您可以使用此代码来确定视图在其边界更改时如何布置其内容。哪个可能对您有用,否则您可能需要调整图像大小?
imageView.contentMode = UIViewContentMode.ScaleAspectFit
其他例子如下:
imageView.contentMode= UIViewContentMode.ScaleToFill
imageView.contentMode = UIViewContentMode.ScaleAspectFill