[英]Swift 2 iOS 9 animation disappears after button text changed
I have an animation that is running fine until I change the button text from start to stop. 在从开始到停止更改按钮文本之前,我的动画运行良好。 The text changes but the animation itself disappears.
文本更改,但动画本身消失。 What am I doing wrong?
我究竟做错了什么?
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) } } }
The animation is stopped because you're invalidating the timer. 动画已停止,因为您使计时器无效。
After invalidating the timer, you should restart it again. 使计时器无效后,应重新启动它。 So, you'll have the animation running again.
因此,您将再次运行动画。
Hope this helps. 希望这可以帮助。
Here are a few things I would do differently, but I don't have it running in a project so I can't guarantee it will all work. 我会做一些不同的事情,但是我没有在项目中运行它,所以我不能保证它都能正常工作。
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)
}
}
}
}
What's happening is that your image is going back 400 pixels to the left again. 发生的情况是您的图像再次向左移了400像素。 You can check this for yourself by changing the leftward shift to 40px instead and running the app.
您可以通过将左移更改为40px并运行应用程序来自己检查。 This is due to the fact that
viewDidLayoutSubviews
is called in several instances and resets the location of the image to whatever you specified originally. 这是由于在多个实例中都会调用
viewDidLayoutSubviews
并将图像的位置重置为您最初指定的位置。
In other words, the method is called when 换句话说,当
How I suggest bypassing this is to add a count variable like so 我建议绕过的方法是像这样添加一个计数变量
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)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.