2017-04-26 33 views
2

我與拖點計數細胞「:」在這裏閃爍, 是閃爍代碼:iOS的遞歸動畫例外

func blinkLable(){ 

     if blinkingLabel.alpha == 1 { 

      UIView.animateWithDuration(1, animations: { 

       self.blinkingLabel.alpha = 0 

       }, completion: { (true) in 

        UIView.animateWithDuration(1, animations: { 

         self.blinkingLabel.alpha = 1 

         }, completion: { (true) in 

          self.blinkLable() 
        }) 

      }) 
     } 
    } 

該功能被稱爲在筆尖的awakeFromNib功能,我提交了之後應用存儲有時我得到這個奇怪的例外:

Crashed: com.apple.main-thread 
0 libobjc.A.dylib    0x19412bbd0 objc_msgSend + 16 
1 UIKit       0x18709bbfc +[UIView(UIViewAnimationWithBlocks) animateWithDuration:animations:completion:] + 64 
2 RTA       0x100df3374 TimeReminingCell.(blinkLable() ->()).(closure #2).(closure #2) (TimeReminingCell.swift:73) 
3 UIKit       0x186f5855c -[UIViewAnimationBlockDelegate _didEndBlockAnimation:finished:context:] + 408 
4 UIKit       0x186f580c4 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 188 
5 UIKit       0x186f57fcc -[UIViewAnimationState animationDidStop:finished:] + 104 
6 QuartzCore      0x18686162c CA::Layer::run_animation_callbacks(void*) + 296 
7 libdispatch.dylib    0x194795954 _dispatch_client_callout + 16 
8 libdispatch.dylib    0x19479a20c _dispatch_main_queue_callback_4CF + 1608 
9 CoreFoundation     0x18245b544 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12 
10 CoreFoundation     0x1824595ec __CFRunLoopRun + 1492 
11 CoreFoundation     0x182384f74 CFRunLoopRunSpecific + 396 
12 GraphicsServices    0x18bde76fc GSEventRunModal + 168 
13 UIKit       0x186f86d94 UIApplicationMain + 1488 
14 RTA       0x100943fe0 main (AppDelegate.swift:35) 
15 libdyld.dylib     0x1947c2a08 start + 4 

普萊斯這個問題的任何幫助

回答

3

遞歸動畫錯誤,因爲你的動畫,以及遞歸:在blinkLable()代碼的最裏面完成塊調用blinkLable(),完成遞歸鏈。

有沒有必要做那個但是,因爲動畫UIView支持重複:

func blinkLable() { 
    UIView.animateWithDuration(1, delay: 0, options: [.repeat], animations: { 
     self.blinkingLabel.alpha = 1 - self.blinkingLabel.alpha 
    }, completion: nil) 
} 

1 - alpha值爲零時alpha是一個,和一個當alpha爲零。

0

你在這裏什麼是經典堆棧Ø verflow。你永遠從自己調用相同的函數。

相反,你需要重複動畫:發生

UIView.animate(withDuration: 1.0, delay: 0, options: [.repeat, .autoreverse], animations: { 
    self.blinkingLabel.alpha = 0 
}, completion: nil) 
+0

你爲什麼把.autoreverse,然後手動扭轉呢? – SeanLintern88

+0

@ SeanLintern88我的錯誤。固定。 –

3

你必須使用動畫選項:

UIView.animate(withDuration: 0.1, delay: 0, options: UIViewAnimationOptions.repeat, animations: { 
     self.blinkingLabel.alpha = 0; 

    }, completion: { (bool) in 
     self.blinkingLabel.alpha = 1; 

    }) 
+1

請勿在動畫完成中使用方法回調。這將無限調用,這就是應用程序崩潰的原因。對於動畫的遞歸調用,您必須使用動畫選項,如:UIViewAnimationOptions.repeat –