【问题标题】:iOS Recursive Animation ExceptioniOS 递归动画异常
【发布时间】:2017-04-26 12:04:55
【问题描述】:

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

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 

请帮助解决这个问题

【问题讨论】:

    标签: ios swift xcode exception exception-handling


    【解决方案1】:

    出现递归动画错误是因为您的动画是递归的:blinkLable() 中代码的最内层完成块调用 blinkLable(),完成了递归链。

    不过,没有必要这样做,因为UIView 动画支持重复:

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

    alpha 为 1 时,1 - alpha 的值为 0,alpha 为 0 时为 1。

    【讨论】:

    • 你能告诉我,递归动画有什么不好的吗?它永远不会停止吗?还是我们捕捉自我?
    • @SashaKid 递归动画最终会使您的系统耗尽堆栈空间。
    【解决方案2】:

    这里有一个经典的堆栈溢出。您永远从自身调用相同的函数。

    相反,您需要重复动画:

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

    【讨论】:

    • 你为什么要放 .autoreverse 然后手动反转它?
    • @SeanLintern88 我的错误。固定。
    【解决方案3】:

    你必须使用动画选项:

    UIView.animate(withDuration: 0.1, delay: 0, options: UIViewAnimationOptions.repeat, animations: {
            self.blinkingLabel.alpha = 0;
    
        }, completion: { (bool) in
            self.blinkingLabel.alpha = 1;
    
        })
    

    【讨论】:

    • 不要在动画完成中使用方法回调。这将进行无限调用,这就是应用程序崩溃的原因。对于动画的递归调用,您必须使用动画选项,例如: UIViewAnimationOptions.repeat
    猜你喜欢
    • 1970-01-01
    • 2015-11-11
    • 2018-01-03
    • 1970-01-01
    • 2013-11-15
    • 1970-01-01
    • 2016-02-19
    • 2012-07-25
    • 2012-10-25
    相关资源
    最近更新 更多