简体   繁体   English

NSTimer取消了我的UIView.animateWithDuration

[英]NSTimer cancel my UIView.animateWithDuration

Every time NSTimer is called my animation is canceled 每次调用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"
}

How can I prevent NSTimer cancel my animations? 如何防止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()
    })
}

I am relatively new to the development of IOS, someone can help me with this, thanks. 我对IOS的开发还比较陌生,可以有人帮助我。

Sure paulvs , Sorry it is a misspelling. 当然paulvs,对不起这是一个拼写错误。 The problem is with the NSTimer :( 问题出在NSTimer :(

I think the code you posted will crash straight away with an exception like this 我认为您发布的代码将立即崩溃,并出现类似这样的异常

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

because the timer function has one argument 因为timer功能只有一个参数

func timer(timer:NSTimer!){

while your NSTimer initialisation code passes a selector with no arguments 而您的NSTimer初始化代码会传递一个不带参数的选择器

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

To fix this, change this line to 要解决此问题,请将此行更改为

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

notice the colon : near the end. 注意冒号:快要结束了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM