我有一個應用程序CALayers
從屏幕的一側移動到另一側。我試圖讓時機正確,以便他們都將相同時間從第一方傳遞到我已經決定的任意分隔符(靠近另一端),然後繼續運行以從屏幕消失。CoreAnimation圖層並非都以相同的速度移動:我錯過了什麼?
這裏是我的意思的例子:
兩個箱子應該以相同的速度移動(動畫具有線性時間),所以當小盒子打的分隔符,間隙小兩者應該是一樣的。
不過,我更願意看到這一點:
用言語來表達,看來我的大箱子被移動比小盒子快。
這是我的代碼的相關部分。我決定以每秒像素爲單位的速度,然後考慮盒子必須行進的像素數量來設置動畫的持續時間。
const CGRect viewBounds = CGRectMake(0,0, 500, 400);
const CGFloat distanceToCross = viewBounds.size.width - delimiterX;
const CFTimeInterval timeToCross = 5;
const CGFloat pixelsPerSecond = distanceToCross/timeToCross;
const CGFloat y = /* y coordinate of the box */;
CALayer* box = [CALayer layer];
// the layer should lie outside the viewport at first
box.frame = CGRectMake(viewBounds.size.width, y, /* size here */);
box.anchorPoint = CGPointMake(0, 0.5);
// snip
[CATransaction begin];
CATransaction.animationTimingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
CATransaction.animationDuration = (viewBounds.size.width + box.frame.size.width)/pixelsPerSecond;
// and the layer should cross the viewport to outside of it, too
box.position = CGPointMake(-box.frame.size.width, y);
[CATransaction commit];
這不是我的圖形示例所示,但同樣大小的箱子都去以同樣的速度,所以很明顯我做錯了我的時間計算。我錯過了什麼?
我忘了提,但我設置了'anchorPoint'屬性改爲'{0,0.5}',所以沿X軸的距離應該是正確的。此外,我非常確定'anchorPoint'不會影響到旅行的距離,除非在中途改變,因爲起點將以與終點相同的方式受到影響。 – zneak
起始點是使用'frame'定義的,而不是'position',而'frame'已經隱式地考慮了'anchorPoint'。 –
@zneak:你可以嘗試在末尾設置'frame'而不是'position',看看是否有所作爲。 'CALayer'文檔'frame'不是直接可以動畫的,但我認爲它仍然支持隱式動畫(通過修改「bounds' /'position'屬性)。 –