gpt4 book ai didi

ios - NSTimer 取消我的 UIView.animateWithDuration

转载 作者:行者123 更新时间:2023-11-28 08:44:33 25 4
gpt4 key购买 nike

每次调用 NSTimer 时我的动画都会被取消

override func viewDidLoad() {
super.viewDidLoad()

_ = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timer:", userInfo: nil, repeats: true)

self.mover()
}

/**
Selector

- parameter timer: NStimer
*/
func timer(timer:NSTimer!){
self.lblTiempo.text = "Te haz movido"
}

如何防止 NSTimer 取消我的动画?

/**
Mueve la imagen en la pantalla
*/
func mover(){


let x = Int(arc4random_uniform(UInt32( 400 )))
let y = Int(arc4random_uniform(UInt32( 400 )))

UIView.animateWithDuration(self.juego!.velocidad,
delay: 0.0,
options: [.CurveEaseInOut, .AllowUserInteraction],
animations: {
self.imgBicho.center = CGPoint(x: x, y: y)
},
completion: { finished in
self.mover()
})
}

我对IOS的开发比较陌生,有人可以帮助我,谢谢。

当然 paulvs,抱歉,这是一个拼写错误。问题出在 NSTimer :(

最佳答案

我认为您发布的代码会立即崩溃,出现这样的异常

'NSInvalidArgumentException', reason: '-[TestTimer.ViewController timer]: unrecognized selector sent to instance 0x7fb501c3faa0'

因为 timer 函数只有一个参数

func timer(timer:NSTimer!){

当你的 NSTimer 初始化代码传递一个没有参数的选择器时

_ = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timer", userInfo: nil, repeats: true)

要解决此问题,请将此行更改为

_ = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timer:", userInfo: nil, repeats: true)

注意靠近末尾的冒号 :

关于ios - NSTimer 取消我的 UIView.animateWithDuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35685933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com