奇怪的@IBAction 冲突或错误?(迅速) [英] Strange @IBAction conflict or bug? (Swift)
问题描述
所以我为我的简单 iOS 应用程序获得了这段代码.当我按下 touchPressed 按钮时,该按钮应该在屏幕上获得一个新的随机位置,并且 labelScore 应该使用按钮触摸次数更新自身.我的一个朋友在 Objective-C 中尝试了这个,并且它有效.
So I got this code for my simple iOS app. When I press the touchPressed button, the button should get a new random location on the screen and the labelScore should update itself with the number of button touches. A friend of mine tried this in Objective-C and it works.
问题是:我在 touchPressed 中有 newScore() 并且按钮没有获得新位置.如果我评论 newScore(),则随机位置操作有效.
The problem is: I have the newScore() in touchedPressed and the button doesn't get a new location. If I comment newScore(), the random location action works.
提前致谢.
// 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 已经确定了问题,我还有一个关于如何处理的建议:
Now that @matt has identified the problem, I have another suggestion about how to handle it:
1) 向您的 ViewController
添加两个新变量:
1) Add two new vars to your ViewController
:
var newButtonX: CGFloat?
var newButtonY: CGFloat?
2) 在更新按钮位置时在 touchPressed()
中设置这些.
2) Set these in touchPressed()
when updating the button's location.
// Offset the button's center by the random offsets.
newButtonX = xoffset + buttonWidth / 2
newButtonY = yoffset + buttonHeight / 2
button.center.x = newButtonX!
button.center.y = newButtonY!
3) 将按钮的 IBOutlet
添加到您的 ViewController
:
3) Add an IBOutlet
for the button to your ViewController
:
@IBOutlet weak var button: UIButton!
并在 Interface Builder 中连接起来.
and wire it up in Interface Builder.
4) 然后像这样覆盖 viewDidLayoutSubviews
:
4) Then override viewDidLayoutSubviews
like so:
override func viewDidLayoutSubviews() {
if let buttonX = newButtonX {
button.center.x = buttonX
}
if let buttonY = newButtonY {
button.center.y = buttonY
}
}
这篇关于奇怪的@IBAction 冲突或错误?(迅速)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!