Итак, я получил этот код для моего простого приложения для iOS. Когда я нажимаю кнопку touchPressed, кнопка должна получить новое случайное местоположение на экране, а labelScore должен обновить себя с помощью нажатия кнопки. Один мой друг попытался это сделать в Objective-C, и он работает.Странный конфликт @IBAction или ошибка? (Swift)
Проблема: у меня есть newScore() в touchpedPressed, и кнопка не получает новое местоположение. Если я прокомментирую newScore(), действие случайного местоположения будет работать.
Заранее спасибо.
// ViewController.swift
import UIKit
import SpriteKit
class ViewController: UIViewController {
@IBOutlet var backgroundImage: UIImageView!
@IBOutlet var scoreLabel: UILabel!
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.
}
// Initial score
var score:Int = 0
// Update the actual score with the one matching th number of touches
func newScore() {
score = score + 1
scoreLabel.text = "SCORE: \(score)"
}
// Get the random height for the button (with limits)
func randomButtonHeight(min: Float, max:Float) -> Float {
return min + Float(arc4random_uniform(UInt32((max-90) - min + 1)))
}
@IBAction func touchPressed(button: UIButton) {
// Find the button's width and height
var buttonWidth = button.frame.width
var buttonHeight = button.frame.height
// Find the width and height of the enclosing view
var viewWidth = button.superview!.bounds.width
var viewHeight = button.superview!.bounds.height
// Compute width and height of the area to contain the button's center
var xwidth = viewWidth - buttonWidth
var yheight = viewHeight - buttonHeight
// Generate a random x and y offset
var xoffset = CGFloat(arc4random_uniform(UInt32(xwidth)))
var yoffset = CGFloat(self.randomButtonHeight(100, max: Float(viewHeight)))
// Offset the button's center by the random offsets.
button.center.x = xoffset + buttonWidth/2
button.center.y = yoffset + buttonHeight/2
newScore() // this works but the action above this doesn't and if I comment this line, the action above works.
}
}
Очень хорошо спросил - я могу легко воспроизвести проблему. – matt
Возможно, кто-то нашел обходное решение для этого. – perte