簡體   English   中英

核心動畫(CABasicAnimation)的行為異常

[英]Unpredictable behavior with Core Animation (CABasicAnimation)

我正在為iOS動畫編寫一條線,其中在CALayer上繪制一條線作為CGPath中的一組點。 線從平面位置動畫化為“成型”形狀,並具有不同的y坐標,但始終具有相同的x坐標,就像折線圖動畫一樣。

為了使點插值更容易,我向CALayer子類添加了自定義CGFloat屬性。 我將此屬性稱為“動畫師”(更好的名稱可能是progress,interpolator等。)的想法是在此屬性上從0.0f到1.0f添加一個簡單的CABasicAnimation,這樣我就可以使用Core Animation的計時功能和內插支持,仍然能夠輕松編寫自定義動畫。 例如,如果一條線的點是從y = 100到y = 130,那么當動畫師為0.0f時,我為100,而動畫師為1.0f時,我便是點130,那么臨時值給了我臨時點,繼續用這些新點重新繪制線條以獲取動畫。

現在,動畫可以正常工作了,我禁用了圖層動作,添加了needsDisplayForKey等。但是我的問題是核心動畫不准確。 動畫師的最終值有時為0.95 .96等,而不是1.0。 很好,因為浮點數存在精度問題,但是,當我的模型值得到更新(將動畫添加到圖層之前設置為1.0f)時,線條應該重新繪制,並且我應該獲得准確的視覺效果。

這是另一個問題出現的地方。 有時,動畫不會立即刪除。 在大多數情況下,它會立即被刪除,我沒有任何問題,但有時它會保持幾秒鍾,有時甚至是幾分鍾。 為了測試我的動畫不會被刪除的理論,我在圖層上添加了一個簡單的BOOL標志,當我們在表示層上時該標志返回YES,並且可以肯定的是,有時我看到我的最后一次drawInContext調用在動畫層上的動畫值為0.98967f之類的東西,以及最后帶有動畫器1.0f且表現層標記為NO的drawInContext調用發生得很晚。 結果,除了明顯的可怕用戶體驗之外,我的視覺跳動並不准確。

如果有人想查看代碼,我會盡力解釋我的問題,我很樂意提供我的測試項目。 希望一些聰明的人看到這一點,並可以幫助我。

編輯1: 在此處上傳了整個Xcode項目(包括我討厭的編輯,以顯示我所做的一切)。

編輯2:有類似問題的人在完成后手動刪除了動畫http://lucas.tiz.ma/blog/2012/04/18/core-animation-is-a-bit-garbage-collection-y/

謝謝。

tl; dr在animationDidStop:finished:委托回調中顯式調用setNeedsDisplay。

根據盧卡斯·提茲瑪(Lucas Tizma)的帖子(請參閱上面的編輯2),我嘗試手動刪除動畫。 我選擇了一種比他的基於塊的方法更簡單的方法,方法是將圖層設置為在其上設置的動畫的代表。 這樣,動畫的開始和停止回調將直接到達相關圖層。 很好地明確刪除animationDidStop:finished中的動畫:回調不能解決問題。 例如,有時(請參閱下面的日志,尤其是時間戳記),動畫將停止並檢查是否已將其刪除,這表明該動畫已被刪除,但是稍后會使用實際模型值進行精確繪制。

// animationDidStop:finished: call back code

NSLog(@"layer animation stopped");

// check if any animations exist
NSLog(@"animation keys: %@", [self animationKeys]);

// remove animations and check
NSLog(@"removing animations");
[self removeAllAnimations];
NSLog(@"animation keys: %@", [self animationKeys]);

// log

2012-10-11 11:47:16.774 ASPathAnimationTest[3017:c07] on presentation layer 1
2012-10-11 11:47:16.774 ASPathAnimationTest[3017:c07] 335 animation draw update, animator is 0.982606
2012-10-11 11:47:16.775 ASPathAnimationTest[3017:c07] startPoint: {100, 90} - endPoint: {100, 100} - newPoint: {100, 99.8261}
<snip>
2012-10-11 11:47:16.791 ASPathAnimationTest[3017:c07] startPoint: {1000, 50} - endPoint: {1000, 100} - newPoint: {1000, 99.1303}
2012-10-11 11:47:16.792 ASPathAnimationTest[3017:c07] layer animation stopped
2012-10-11 11:47:16.792 ASPathAnimationTest[3017:c07] animation keys: (null)
2012-10-11 11:47:16.793 ASPathAnimationTest[3017:c07] removing animations
2012-10-11 11:47:16.793 ASPathAnimationTest[3017:c07] animation keys: (null)
2012-10-11 11:47:16.794 ASPathAnimationTest[3017:c07] layer animation stopped
2012-10-11 11:47:16.794 ASPathAnimationTest[3017:c07] animation keys: (null)
2012-10-11 11:47:16.794 ASPathAnimationTest[3017:c07] removing animations
2012-10-11 11:47:16.795 ASPathAnimationTest[3017:c07] animation keys: (null)
2012-10-11 11:47:16.795 ASPathAnimationTest[3017:c07] layer animation stopped
2012-10-11 11:47:16.819 ASPathAnimationTest[3017:c07] animation keys: (null)
2012-10-11 11:47:16.820 ASPathAnimationTest[3017:c07] removing animations
2012-10-11 11:47:16.820 ASPathAnimationTest[3017:c07] animation keys: (null)
2012-10-11 11:47:16.821 ASPathAnimationTest[3017:c07] layer animation stopped
2012-10-11 11:47:16.821 ASPathAnimationTest[3017:c07] animation keys: (null)
2012-10-11 11:47:16.821 ASPathAnimationTest[3017:c07] removing animations
2012-10-11 11:47:16.822 ASPathAnimationTest[3017:c07] animation keys: (null)
2012-10-11 11:47:16.822 ASPathAnimationTest[3017:c07] layer animation stopped
2012-10-11 11:47:16.822 ASPathAnimationTest[3017:c07] animation keys: (null)
2012-10-11 11:47:16.823 ASPathAnimationTest[3017:c07] removing animations
2012-10-11 11:47:16.823 ASPathAnimationTest[3017:c07] animation keys: (null)
2012-10-11 11:48:00.000 ASPathAnimationTest[3017:c07] on presentation layer 0
2012-10-11 11:48:00.000 ASPathAnimationTest[3017:c07] 336 animation draw update, animator is 1.000000
<snip, there are 5 lines so draw and point logs go here>
2012-10-11 11:48:00.021 ASPathAnimationTest[3017:c07] 340 animation draw update, animator is 1.000000
2012-10-11 11:48:00.023 ASPathAnimationTest[3017:c07] startPoint: {100, 90} - endPoint: {100, 100} - newPoint: {100, 100}
<snip>
2012-10-11 11:48:00.026 ASPathAnimationTest[3017:c07] startPoint: {1000, 50} - endPoint: {1000, 100} - newPoint: {1000, 100}

在看到日志並注意到動畫實際上已被刪除之后,只是有時直到幾秒鍾甚至幾分鍾后才發生使用實際准確的模型值進行的重新繪制,我更改了animationDidStop:finished:委托回顯調用setNeedsDisplay在圖層上。 答對了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM