gpt4 book ai didi

iOS递归动画异常

转载 作者:可可西里 更新时间:2023-11-01 00:57:13 25 4
gpt4 key购买 nike

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

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

请大家帮忙解决这个问题

最佳答案

递归动画错误的发生是因为你的动画是递归的:blinkLable() 中代码的最内层完成 block 调用 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 的值为零,当 alpha 为零时,值为 1。

关于iOS递归动画异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43633702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com