我是XCode(swift)的新手,我正在尝试让UIImageView随机显示三个图像中的一个,但当我在模拟器中切换到第二个viewController时,没有任何反应,XCode突出显示结束ViewDidLoad函数说线程1:断点1.1。
如果有更好或更可靠的方法让UIImageView切换图像,我肯定想知道。
我目前的代码:
import UIKit
let imageInitialIdentity = arc4random_uniform(3)
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if(imageInitialIdentity == 0){
imageGuess.image = UIImage(named: "0ps.pngs")
imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
}
if(imageInitialIdentity == 1){
imageGuess.image = UIImage(named: "250ps.png")
imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
}
if(imageInitialIdentity == 2){
imageGuess.image = UIImage(named: "500ps.png")
imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
}
// 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.
}
@IBOutlet weak var imageGuess: UIImageView!
var imageIdentity = imageInitialIdentity
var score = 0
let imageZero = UIImage.init(named: "0ps")
let image250 = UIImage.init(named: "250ps")
let image500 = UIImage.init(named: "500ps")
@IBAction func guessZero(sender: UIButton) {
//if zero matches the picture 0, add a point
if imageIdentity == 0{
score++
}
else{
//print incorrect, corect answer is 0
}
//randomizes another picture
let rand1 = arc4random_uniform(3)
if rand1 == 0 {
imageGuess.image = UIImage(named: "0ps.png")
imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
imageIdentity = 0
}
else if rand1 == 1{
imageGuess.image = UIImage(named:"250ps.png")
imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
imageIdentity = 1
}
else if rand1 == 2{
imageGuess.image = UIImage(named:"500ps.png")
imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
imageIdentity = 2
}
}
@IBAction func guess250(sender: UIButton) {
// if 150 matches the picture of 250, return true
//randomizes another picture
//adds one to score?
if imageIdentity == 1{
score++
}
let rand2 = arc4random_uniform(3)
if rand2 == 0 {
imageGuess.image = UIImage(named:"0ps.png")
imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
imageIdentity = 0
}
else if rand2 == 1{
imageGuess.image = UIImage(named:"250ps.png")
imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
imageIdentity = 1
}
else if rand2 == 2{
imageGuess.image = UIImage(named:"500ps.png")
imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
imageIdentity = 2
}
}
@IBAction func guess500(sender: UIButton) {
//if 500 matches the picture of 500, return true
//randomizes another picture
if imageIdentity == 2{
score++
}
let rand3 = arc4random_uniform(3)
if rand3 == 0 {
imageGuess.image = UIImage(named:"0ps.png")
imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
imageIdentity = 0
}
else if rand3 == 1{
imageGuess.image = UIImage(named:"250ps.png")
imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
imageIdentity = 1
}
else if rand3 == 2{
imageGuess.image = UIImage(named:"500ps.png")
imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
imageIdentity = 2
}
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
如果代码从一个ViewController运行到此SecondViewController,并且程序在viewDidLoad中断,您可能想要检查代码如何使用其先前的类完成以及它运行SecondViewController类的原因。完成检查后,您可能希望将随机方法移出viewDidLoad以找出导致崩溃的确切原因。类似的东西:
override func viewDidLoad()
{
super.viewDidLoad()
randomMethod()
}
func randomMethod() // put a break point here if viewDidLoad passes
{
if(imageInitialIdentity == 0){
..... your code ....
}
}
答案 2 :(得分:0)
要回答您的问题,"如果有更好或更可靠的方式让UIImageView切换图像"。
简短的回答,不,这就是它的完成方式。 您应该注意的一件事是您必须在主线程上设置图像以查看更改。 viewDidLoad方法当然在主线程上运行所以不用担心。 如果您决定稍后更新imageview的图片,可以使用GCD正确更新您的用户界面。
dispatch_async(dispatch_get_main_queue(), ^{
// update your UI here
});
如果需要在imageview中快速切换两个图像,可以使用highlightImage属性。 您将此属性设置为与图像属性相同(当然使用不同的图像)。 然后,您需要做的就是将突出显示的属性设置为YES或NO。
UIImageView *iv = [UIImageView new];
UIImage *firstImage = [UIImage imageNamed:@"first_image.png"];
iv.image = firstImage;
iv.highlightedImage = [UIImage imageNamed:@"second_image.png"];
// Call below code whenever you want to switch the image, by setting the boolean.
dispatch_async(dispatch_get_main_queue(), ^{
iv.highlighted = YES;
});
我建议的一件事是使用imageview框架的图像尺寸属性。
iv.frame = CGRectMake(0, 0, firstImage.size.width, firstImage.size.height);