在Swift中,Action需要2次点击而不是1次

时间:2015-08-28 04:46:08

标签: ios swift

我的重置按钮需要2次点击才能重置小狗的位置,而不是1.我想知道我哪里出错了。此外,当我在重置后再次点击tapButton时,小狗会回到重置前的位置。关于为什么会发生的任何想法?

我很新,所以我可能把一些东西放错了地方。

<!DOCTYPE html>
<html>
    <body>
         <embed id="foo" src="https://example.com/redir"></embed>
         <!-- Remember that example.com/redir will be automatically redirected to example.com/hi.SWF?message=Message+Value -->

         <!-- The code to get the message value must go here -->

    </body>
</html>

包含重置按钮的代码的主要部分在这里:

var tapsCount = 0
var countdownNumber = 10
var Timer : NSTimer = NSTimer()


class ViewController: UIViewController, ADBannerViewDelegate {

@IBOutlet weak var puppy: UIImageView!
@IBOutlet weak var secondsLeft: UILabel!
@IBOutlet weak var tapsCounter: UILabel!
@IBOutlet weak var currentHighScore: UILabel!
@IBOutlet weak var colon: UILabel!
@IBOutlet weak var Banner: ADBannerView!

@IBOutlet weak var endMessage: UILabel!
@IBAction func tapButton() {
    if tapsCount == 0 {
        Timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("UpdateTimer"), userInfo: nil, repeats: true)
    } else {

    }
    tapsCount = tapsCount+1
    tapsCounter.text = "\(tapsCount)"

    if countdownNumber<0 {
        secondsLeft.text = ""
        tapsCounter.text = ""
    } else {


  //-------moving the puppy----------------------


        UIView.animateWithDuration(0, delay: 100, options: UIViewAnimationOptions.CurveLinear, animations: {
            self.puppy.alpha = 1
            self.puppy.center.y = self.puppy.center.y + 1  //this will change Y position of your imageView center every time you press button
            }, completion: nil)

        if self.puppy.center.y >= 131 {
            puppy.center.y = 131
        }
        if self.puppy.center.y <= -41 {
            puppy.center.y = -41
        }
    }
       }

@IBAction func resetButton() {
    tapsCount = 0
    countdownNumber = 10
    colon.text = ":"
    Timer.valid
    secondsLeft.text = "10"
    tapsCounter.text = "0"
    endMessage.text = ""
    Timer.invalidate()

    puppy.frame = CGRectMake( 187, 325, puppy.frame.size.width, puppy.frame.size.height );


}


func UpdateTimer() {
    secondsLeft.text = String(countdownNumber--)
    if countdownNumber == (-1) {
        secondsLeft.text = ""
        colon.text = ""
        var finalScore = tapsCount
        var highest = currentHighScore.text
        tapsCounter.text = ""


    } else if countdownNumber<(0) {
        Timer.invalidate()
        secondsLeft.text = ""
        tapsCounter.text = ""
    } else {
    }
}


override func viewDidLoad() {

    Banner.hidden = true
    Banner.delegate = self
    self.canDisplayBannerAds = true

    secondsLeft.text = String(countdownNumber)
    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.
}

func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
    return true
}

func bannerViewDidLoadAd(banner: ADBannerView!) {
    Banner.hidden = false
}

}

1 个答案:

答案 0 :(得分:1)

以这种方式更新puppy.frame

dispatch_async(dispatch_get_main_queue()) {
    self.puppy.frame = CGRectMake( 187, 325, self.puppy.frame.size.width, self.puppy.frame.size.height );
}