iOS递归动画异常

时间:2017-04-26 12:04:56

标签: ios swift xcode exception exception-handling

我有一个带有两个点“”“的计数器的单元格, 这是眨眼代码:

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()
                    })

            })
        }
    }

在nib的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 个答案:

答案 0 :(得分: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为1时alpha的值为零,alpha为零时为{。

答案 1 :(得分:0)

这里有一个经典的堆栈溢出。您永远调用自己的相同功能。

相反,你需要重复动画:

export function setBranch( branchName: string, twigStruct: Object ) {
    treeduxTrunk = treeduxTrunk.set( branchName, twigStruct );
} 

答案 2 :(得分:0)

您必须使用动画选项:

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

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

    })