簡體   English   中英

使用CABasicAnimation為框架屬性設置動畫

[英]Animate frame property using CABasicAnimation

我正在嘗試對這個基於UIView塊的動畫代碼進行精確的“翻譯”:

[UIView animateWithDuration:0.5
                      delay:0.0
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                               someView.frame = CGRect(0, 100, 200, 200);
                             }
                 completion:nil];

而是使用CABasicAnimation

我完全知道frame屬性實際上是底層的positionboundsanchorPoint的組合,如下所述: http//developer.apple.com/library/mac/#qa/qa1620/_index html的
......我已經做了這樣的解決方案,使用兩個CABasicAnimations設置位置 ,一個用於邊界 ,它適用於那個視圖。

但問題是我在視圖中有子視圖。 someView有一個UIScrollView類型的子視圖,我在其中放置了另一個UIImageView類型的子視圖。 UIScrollView子視圖將autoresizingMask設置為UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight 如果我使用基於塊的UIView版本,那么一切都很完美,但是當我嘗試使用CABasicAnimations時 ,子視圖開始出現意外行為(即調整大小不正確的寬度)。 所以當使用CABasicAnimations時,似乎autoresizingMask無法正常工作。 我還注意到子視圖沒有接收到對setFrame的調用盡管在更改圖層位置和邊界后父視圖的frame屬性確實發生了變化。

這就是為什么我想知道用CABasicAnimation復制的正確代碼是什么,當使用UIView的animateWithDuration方法時會發生什么。

我完全清楚frame屬性實際上是底層的position,bounds和anchorPoint的組合

很好,但重要的是要注意frame不是圖層的可動畫屬性。 如果要使用CABasicAnimation進行動畫處理,則必須使用可為圖層設置動畫的屬性。 CALayer文檔將每個此類屬性標記為顯式“可動畫”。 使用boundsposition的想法是正確的。

因此,此代碼基本上完成了您之前所做的事情:

[CATransaction setDisableActions:YES];
// set final bounds and position
v.layer.bounds = CGRectMake(0,0,200,200);
v.layer.position = CGPointMake(100,200);

// cause those changes to be animated
CABasicAnimation* a1 = [CABasicAnimation animationWithKeyPath:@"bounds"];
a1.duration = 0.5;
CABasicAnimation* a2 = [CABasicAnimation animationWithKeyPath:@"position"];
a2.duration = 0.5;
[v.layer addAnimation:a1 forKey:nil];
[v.layer addAnimation:a2 forKey:nil];

但是,該代碼對v.layer的任何子層的大小沒有影響。 v的子視圖由v.layer的子層v.layer 。)不幸的是,這是你要解決的問題。 我相信通過降低到圖層和直接顯式核心動畫的級別,您已經放棄了自動調整,這在視圖級別發生。 因此,您還需要為子圖層設置動畫。 這就是動畫視圖為你做的事情。

這是iOS的一個不幸功能。 Mac OS X具有圖層約束(CAConstraint),它在圖層級別執行自動調整在視圖級別執行的操作(以及更多)。 但是iOS缺少這個功能。

暫無
暫無

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

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