按钮文本更改后,Swift 2 iOS 9动画消失
我有一个动画运行良好,直到我改变按钮文本从开始到停止。文本会更改,但动画本身会消失。我做错了什么 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) } } } 导入UIKit 类ViewController:UIViewController{ 变量计数器=1 var timer=NSTimer() var isAnimating=false @IBVAR按钮:UIButton! @IBVAR frogsImage:UIImageView! @iAction func updateImage(发件人:AnyObject){ 如果isAnimating==false{ timer=NSTimer.scheduledTimerWithTimeInterval(0.1,目标:self,选择器:选择器(“doAnimation”),userInfo:nil,repeats:true) isAnimating=true 按钮.setTitle(“停止跳跃”,用于状态:uicontrol状态.正常) }否则{ timer.invalidate() isAnimating=false 按钮.setTitle(“开始跳转”,用于状态:uicontrol状态.Normal) } } 重写func viewDidLoad(){ super.viewDidLoad() //加载视图后,通常从nib执行任何其他设置。 } func doAnimation(){ 如果计数器==4{ 计数器=1 }否则{ 柜台++ } frogsImage.image=UIImage(名为:“frame\(counter).png” ) } 重写函数didReceiveMemoryWarning(){ 超级。我收到了记忆警告() //处置所有可以重新创建的资源。 } 重写func viewdilayoutsubviews(){ //躲开屏幕 frogsImage.center=CGPointMake(frogsImage.center.x-400,frogsImage.center.y) } 覆盖功能视图显示(动画:Bool){ UIView.animateWithDuration(1){()->Void in self.frogsImage.center=CGPointMake(self.frogsImage.center.x+400,self.frogsImage.center.y) } } }按钮文本更改后,Swift 2 iOS 9动画消失,ios,swift2,Ios,Swift2,我有一个动画运行良好,直到我改变按钮文本从开始到停止。文本会更改,但动画本身会消失。我做错了什么 import UIKit class ViewController: UIViewController { var counter = 1 var timer = NSTimer() var isAnimating = false @IBOutlet weak var button: UIButton! @IBOutlet weak var fro
动画已停止,因为正在使计时器无效 使计时器无效后,应重新启动计时器。因此,您将再次运行动画
希望这有帮助。动画停止,因为您正在使计时器无效 使计时器无效后,应重新启动计时器。因此,您将再次运行动画
希望这能有所帮助。以下是一些我想做的不同的事情,但我没有在项目中运行它,所以我不能保证它都能工作
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)
}
}
}
}
这里有几件事我会做不同的,但我没有在一个项目中运行它,所以我不能保证它将所有的工作
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)
}
}
}
}
发生的事情是,你的图像又回到了400像素的左边。您可以通过将左移改为40px并运行应用程序来检查这一点。这是因为在多个实例中调用了
viewdilayoutsubviews
,并将图像的位置重置为最初指定的位置
换句话说,当
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)
}
}
}
导入UIKit
类ViewController:UIViewController{
var viewCount=0//添加此
变量计数器=1
var timer=NSTimer()
var isAnimating=false
@IBVAR按钮:UIButton!
@IBVAR frogsImage:UIImageView!
@iAction func updateImage(发件人:AnyObject){
如果isAnimating==false{
timer=NSTimer.scheduledTimerWithTimeInterval(0.1,目标:self,选择器:选择器(“doAnimation”),userInfo:nil,repeats:true)
isAnimating=true
按钮.setTitle(“停止跳跃”,用于状态:uicontrol状态.正常)
}否则{
timer.invalidate()
isAnimating=false
按钮.setTitle(“开始跳转”,用于状态:uicontrol状态.Normal)
}
}
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后,通常从nib执行任何其他设置。
}
func doAnimation(){
如果计数器==4{
计数器=1
}否则{
柜台++
}
frogsImage.image=UIImage(名为:“frame\(counter.png”)
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
重写func viewdilayoutsubviews(){
//添加此IF块
如果viewCount<2{
frogsImage.center=CGPointMake(frogsImage.center.x-400,frogsImage.center.y)
视图计数++
}
}
覆盖功能视图显示(动画:Bool){
UIView.animateWithDuration(1){()->Void in
self.frogsImage.center=CGPointMake(self.frogsImage.center.x+400,self.frogsImage.center.y)
}
}
}
发生的情况是,您的图像再次向左返回400像素。您可以通过将左移改为40px并运行应用程序来检查这一点。这是因为在多个实例中调用了viewdilayoutsubviews
,并将图像的位置重置为最初指定的位置
换句话说,当
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)
}
}
}
导入UIKit
类ViewController:UIViewController{
var viewCount=0//添加此
变量计数器=1
var timer=NSTimer()
var isAnimating=false
@IBVAR按钮:UIButton!
@IBVAR frogsImage:UIImageView!
@iAction func updateImage(发件人:AnyObject){
如果isAnimating==false{
timer=NSTimer.scheduledTimerWithTimeInterval(0.1,目标:self,选择器:选择器(“doAnimation”),userInfo:nil,repeats:true)
isAnimating=true
按钮。设置标题(“停止Jum