斯威夫特:播放停止按钮

时间:2015-11-18 20:09:42

标签: ios swift

我有一个移动按钮的应用程序,每次用户点击/点击它时都会记录。我需要帮助创建另一个按钮的功能来玩和停止游戏。 这是到目前为止的代码:

class ViewController: UIViewController {
@IBOutlet var button:UIButton!
@IBOutlet var startButton:UIButton!
@IBOutlet var scoreLabel:UILabel!
var score = 0;
var timer : NSTimer!

@IBAction func processHit(sender:AnyObject){
    score += 1;
    scoreLabel.text = "Score: " + String(score);
}

func moveButton(t:NSTimer){
    let aWidth = self.view.bounds.size.width;
    let aHeight = self.view.bounds.size.height;
    let btnX = random() % (Int)(aWidth-60);
    let btnY = random() % (Int)(aHeight-60);
    button.frame = CGRectMake(CGFloat(btnX), CGFloat(btnY), 30.0, 30.0);



}

override func viewDidLoad() {

    super.viewDidLoad()
    srandom(arc4random());
    timer = NSTimer.scheduledTimerWithTimeInterval(1.5, target: self, selector: Selector("moveButton:"), userInfo: nil, repeats: true);
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

}

1 个答案:

答案 0 :(得分:0)

添加"播放"按钮到您的故事板,然后通过控制并点击"播放"创建一个动作插座。按钮并将其拖动到视图控制器。

enter image description here

enter image description here

相关问题