2014-09-27 63 views
3

所以我得到了我的简单iOS应用程序的代码。当我按下touchPressed按钮时,按钮应该在屏幕上获得一个新的随机位置,并且标签Score应该用按钮触摸的数量更新自身。我的一个朋友在Objective-C中尝试了这一点,它的工作原理。奇怪的@IBAction冲突或错误? (Swift)

问题是:我有touchPressed中的newScore(),并且按钮没有得到新的位置。如果我评论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. 

} 



} 
+0

很好问 - 我可以很容易地重现该问题。 – matt 2014-09-27 17:42:47

+0

也许有人找到了解决方法.. – perte 2014-09-27 17:45:38

回答

3

现在已@马特发现的问题,我对如何处理它的另一个建议:在touchPressed()设置这些

var newButtonX: CGFloat? 
var newButtonY: CGFloat? 

2):

1)添加了两个新的增值经销商向您ViewController更新按钮的位置时。

// 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)添加按钮的IBOutletViewController

@IBOutlet weak var button: UIButton! 

和电线它在界面生成器。

4)然后覆盖viewDidLayoutSubviews像这样:

override func viewDidLayoutSubviews() { 
    if let buttonX = newButtonX { 
     button.center.x = buttonX 
    } 
    if let buttonY = newButtonY { 
     button.center.y = buttonY 
    } 
} 
+0

致命错误:在解包可选值时意外发现为零 (lldb)on - button.center.x = buttonX – perte 2014-09-27 18:40:22

+0

确保将您的按钮连接到故事板的插座 – vacawama 2014-09-27 18:43:40

+0

应该创建另一个吗?我的按钮已经连线到touchPressed – perte 2014-09-27 18:50:22

2

这是因为绘图/布局系统的工作原理。你有一个小时间问题,就是这样。实际上发生的事情是,按钮正在移动,但布局在之后立即发生并再次放回。一个解决方案是在很短的延迟,除了通话包裹一切newScore,像这样:

@IBAction func touchPressed(button: UIButton) { 
    delay(0) { 
     // Find the button's width and height 
     var buttonWidth = button.frame.width 
     // ... 
    } 
    self.newScore() 
} 

或者,关闭自动布局在这个情节提要/ XIB文件。这也解决了它(虽然这可能是不能接受的其他原因)。

+0

对于'延迟'功能请参阅我的答案在这里:http://stackoverflow.com/a/24318861/341994 – matt 2014-09-27 17:47:03

+0

使用延迟部分工作,因为有些时候按钮仍然但是关闭自动布局效果会更好。 – perte 2014-09-27 17:56:20

+0

是的,我也注意到了。这可能有助于使标签比需要的更大(更宽)。 – matt 2014-09-27 18:15:20