按钮文本更改后,Swift 2 iOS 9动画消失

时间:2015-08-10 09:11:34

标签: ios swift2

我有一个正常运行的动画,直到我从开始到停止更改按钮文本。文本更改但动画本身消失。我做错了什么?


import UIKit

class ViewController: UIViewController {

    var counter = 1

    var timer = NSTimer()

    var isAnimating = false

    @IBOutlet weak var button: UIButton!

    @IBOutlet weak var frogsImage: UIImageView!

    @IBAction func updateImage(sender: AnyObject) {

        if isAnimating == false {
            timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("doAnimation"), userInfo: nil, repeats: true)
            isAnimating = true
            button.setTitle("Stop Jumping", forState: UIControlState.Normal)

        } else {
            timer.invalidate()
            isAnimating = false
            button.setTitle("Start Jumping", forState: UIControlState.Normal)
        }

            }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    func doAnimation() {
        if counter == 4 {
            counter = 1
        } else {
            counter++
        }

        frogsImage.image = UIImage(named: "frame\(counter).png"
        )

    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewDidLayoutSubviews() {

        // Hiding off the screen
        frogsImage.center = CGPointMake(frogsImage.center.x - 400, frogsImage.center.y)

    }

    override func viewDidAppear(animated: Bool) {
        UIView.animateWithDuration(1) { () -> Void in
            self.frogsImage.center = CGPointMake(self.frogsImage.center.x + 400, self.frogsImage.center.y)
        }
    }


}

3 个答案:

答案 0 :(得分:0)

动画已停止,因为您使计时器无效。

使计时器失效后,您应该重新启动它。所以,你将再次运行动画。

希望这有帮助。

答案 1 :(得分:0)

以下是我会做的一些不同的事情,但我没有让它在项目中运行,因此我无法保证它会全部运作。

class ViewController: UIViewController {

    var counter = 1

    var timer = NSTimer()

    var isAnimating = false
    var didSetup = false
    var didAnimateIn = false

    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var frogsImage: UIImageView!
    @IBAction func updateImage(sender: AnyObject) {

        if isAnimating == false {
            timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("doAnimation"), userInfo: nil, repeats: true)
            isAnimating = true
            button.setTitle("Stop Jumping", forState: UIControlState.Normal)

        } else {
            timer.invalidate()
            isAnimating = false
            button.setTitle("Start Jumping", forState: UIControlState.Normal)
        }

    }

    func doAnimation() {
        if counter == 4 {
            counter = 1
        } else {
            counter++
        }

        frogsImage.image = UIImage(named: "frame\(counter).png"
        )

    }

    override func viewDidLayoutSubviews() {
        // Change this to always call super first
        super.viewDidLayoutSubviews()

        // Only do this once 
        if !didSetup {
            didSetup = true
            frogsImage.center = CGPointMake(frogsImage.center.x - 400, frogsImage.center.y)
        }

    }

    override func viewDidAppear(animated: Bool) {
        // Change this to always call super
        super.viewDidAppear(animated)

        // Only do this once, after setup
        if didSetup && !didAnimateIn {
            didAnimateIn = true
            UIView.animateWithDuration(1) { () -> Void in
                self.frogsImage.center = CGPointMake(self.frogsImage.center.x + 400, self.frogsImage.center.y)
            }
        }
    }
}

答案 2 :(得分:0)

发生的事情是你的图像再次向左移动400像素。您可以通过将向左移位更改为40px并运行应用程序来自行检查。这是因为在多个实例中调用viewDidLayoutSubviews并将图像的位置重置为您最初指定的任何位置。

换句话说,在

时调用该方法
  1. 应用程序已打开
  2. 再次在动画之前
  3. 点击按钮时
  4. 我建议绕过这个是如此添加一个计数变量

    import UIKit
    
    class ViewController : UIViewController {
        var viewCount = 0 //ADD THIS
    
        var counter = 1
    
        var timer = NSTimer()
    
        var isAnimating = false
    
        @IBOutlet weak var button: UIButton!
    
        @IBOutlet weak var frogsImage: UIImageView!
    
        @IBAction func updateImage(sender: AnyObject) {
    
            if isAnimating == false {
                timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("doAnimation"), userInfo: nil, repeats: true)
                isAnimating = true
                button.setTitle("Stop Jumping", forState: UIControlState.Normal)
    
            } else {
                timer.invalidate()
                isAnimating = false
                button.setTitle("Start Jumping", forState: UIControlState.Normal)
            }
    
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        func doAnimation() {
            if counter == 4 {
                counter = 1
            } else {
                counter++
            }
    
            frogsImage.image = UIImage(named: "frame\(counter).png")
        }
    
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        override func viewDidLayoutSubviews() {
    
            // ADD THIS IF BLOCK
            if viewCount < 2 {
                frogsImage.center = CGPointMake(frogsImage.center.x - 400, frogsImage.center.y)
                viewCount++
            }
    
        }
    
        override func viewDidAppear(animated: Bool) {
            UIView.animateWithDuration(1) { () -> Void in
                self.frogsImage.center = CGPointMake(self.frogsImage.center.x + 400, self.frogsImage.center.y)
            }
        }
    }