2012-02-19 62 views
0

我已通過添加圖層掩碼向我的子類UIView添加了圓角。以下是Montouch代碼,但應該是很容易獲得的意義也ObjC:以CAShapeLayer爲掩碼的UIView的子類不能正確動畫

// Add a layer that holds the rounded corners. 
UIBezierPath oMaskPath = UIBezierPath.FromRoundedRect (this.Bounds, this.eRoundedCorners, new SizeF (this.fCornerRadius, this.fCornerRadius)); 

private void UpdateMask() 
{   
if(this.Layer.Mask == null) 
{ 
    CAShapeLayer oMaskLayer = new CAShapeLayer(); 
    oMaskLayer.Frame = this.Bounds; 

    // Set the newly created shape layer as the mask for the image view's layer 
    this.Layer.Mask = oMaskLayer; 
} 
((CAShapeLayer)this.Layer.Mask).Path = oMaskPath.CGPath; 
} 

我已經超載了Frame財產。設置框架會調整蒙版以保持圓角的形狀。

但是,如果我爲框架大小的變化設置了動畫效果,那麼掩碼會立即設置爲動畫的目標值。

有沒有辦法讓我的視圖檢測到它是動畫的一部分,並使屏幕正確動畫?

回答

1

這是CoreAnimation如何工作的顯式動畫。

使用顯式動畫,您絕對不會實際更改原始對象的值,您只需爲表示層中的對象設置動畫。所以你需要確保你的對象已經設置好,然後用最後想要的值開始動畫。

有關如何解決此類問題的完整說明,請參見WWDC 2011在線視頻中的「Core Animation Essentials」。尋找會議421

+0

我看了視頻。我沒有使用明確的動畫。我正在調整UIView的屬性,這會導致圖層屬性的更改,而且不會生成動畫。我試圖爲UIView的圖層設置動畫,但沒有成功:支持UIView的圖層從不動畫。然而,我可以使用隱式動畫來動畫化CAShapeLayer,但同時也會動畫UIView。我迷失在這裏......我怎麼能把兩者結合起來? – Krumelur 2012-02-19 21:09:53