2016-02-28 36 views
1

每次的NSTimer被稱爲我的動畫被取消的NSTimer取消我UIView.animateWithDuration

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 :(

回答

0

我想你發佈的代碼會崩潰馬上像這樣

'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) 

通知結腸:接近尾聲。

+0

Sure paulvs,對不起,它是一個拼寫錯誤。 NSTimer的問題是: – Izak

+0

我把你的代碼複製到一個空的項目中,分別將'UILabel'和'UIImageView'連接到'lblTiempo'和'imgBicho',然後我將'self.juego!.velocidad'用'1'運行該項目並在屏幕上跳舞出一個圖像,那麼究竟是什麼問題?圖像是否完全移動? – paulvs

+0

當用定時器更新視圖時,圖像消失並出現另一方面,我的動畫沒有結束,請原諒我的英語,我不會說或寫得很好,如果我刪除了定時器,圖像會流暢地流動,不會消失 – Izak