2015-11-18 200 views
0

我有一個移動按鈕的應用程序,它記錄每次用戶點擊/點擊它。我需要幫助創建另一個按鈕的功能來播放和停止遊戲。 這是迄今爲止代碼: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() 
} 

}

回答

0

添加「播放」按鈕,你的故事板,然後創建通過保持控制並單擊「播放」按鈕,並將其拖動動作出口到視圖控制器。

enter image description here

enter image description here

+0

這篇幫助你呢? – adiaz004