Reputation: 95
I have a cell with counter with tow dots ":" that blinks, here is the blink code:
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()
})
})
}
}
This function is called on awakeFromNib function of nib, after I submitted the app to store sometimes i got this weird exception:
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
Pleas any help with this problem
Upvotes: 2
Views: 798
Reputation: 3494
You have to use animation options:
UIView.animate(withDuration: 0.1, delay: 0, options: UIViewAnimationOptions.repeat, animations: {
self.blinkingLabel.alpha = 0;
}, completion: { (bool) in
self.blinkingLabel.alpha = 1;
})
Upvotes: 0
Reputation: 726479
Recursive animation error happens because your animation is, well, recursive: the innermost completion block of the code in blinkLable()
invokes blinkLable()
, completing the recursive chain.
There is no need to do that, however, because UIView
animation supports repetitions:
func blinkLable() {
UIView.animateWithDuration(1, delay: 0, options: [.repeat], animations: {
self.blinkingLabel.alpha = 1 - self.blinkingLabel.alpha
}, completion: nil)
}
The value of 1 - alpha
is zero when alpha
is one, and one when alpha
is zero.
Upvotes: 3
Reputation: 4104
What you have here is a classical stack overflow. You invoke the same function from itself forever.
Instead you need to repeat the animation:
UIView.animate(withDuration: 1.0, delay: 0, options: [.repeat, .autoreverse], animations: {
self.blinkingLabel.alpha = 0
}, completion: nil)
Upvotes: 0