2011-04-28 27 views
3

我想動畫一個CAShapeLayer上的CGColor fillColor屬性。我可以用下面的語法使用Objective-C正常工作:如何在使用Monotouch時將CGColor轉換爲NSObject?

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // Create the path 
    thisPath = CGPathCreateMutable(); 
    CGPathMoveToPoint(thisPath, NULL, 100.0f, 50.0f); 
    CGPathAddLineToPoint(thisPath, NULL, 10.0f, 140.0f); 
    CGPathAddLineToPoint(thisPath, NULL, 180.0f, 140.0f); 
    CGPathCloseSubpath(thisPath); 

    // Create shape layer 
    shapeLayer = [CAShapeLayer layer]; 
    shapeLayer.frame = self.view.bounds; 
    shapeLayer.path = thisPath; 
    shapeLayer.fillColor = [UIColor redColor].CGColor; 

    [self.view.layer addSublayer:shapeLayer]; 

    // Add the animation 
    CABasicAnimation* colorAnimation = [CABasicAnimation animationWithKeyPath:@"fillColor"]; 
    colorAnimation.duration = 4.0; 
    colorAnimation.repeatCount = 1e100f; 
    colorAnimation.autoreverses = YES; 
    colorAnimation.fromValue = (id) [UIColor redColor].CGColor; 
    colorAnimation.toValue = (id) [UIColor blueColor].CGColor; 
    [shapeLayer addAnimation:colorAnimation forKey:@"animateColor"]; 
} 

這會按預期動畫化顏色偏移。當我端口這MonoTouch的,我想:

public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 

     thisPath = new CGPath(); 
     thisPath.MoveToPoint(100,50); 
     thisPath.AddLineToPoint(10,140); 
     thisPath.AddLineToPoint(180,140); 
     thisPath.CloseSubpath(); 

     shapeLayer = new CAShapeLayer(); 
     shapeLayer.Path = thisPath; 
     shapeLayer.FillColor = UIColor.Red.CGColor; 

     View.Layer.AddSublayer(shapeLayer); 

     CABasicAnimation colorAnimation = CABasicAnimation.FromKeyPath("fillColor"); 
     colorAnimation.Duration = 4; 
     colorAnimation.RepeatCount = float.PositiveInfinity; 
     colorAnimation.AutoReverses = true; 
     colorAnimation.From = NSObject.FromObject(UIColor.Red.CGColor); 
     colorAnimation.To = NSObject.FromObject(UIColor.Blue.CGColor); 

     shapeLayer.AddAnimation(colorAnimation, "animateColor"); 
    } 

但動畫從來不玩。 animationStarted事件確實會被引發,因此推測它試圖運行動畫,但我在屏幕上看不到任何可見的證據。

我一直在玩這一個更好的一天的一部分,我認爲這是從CGColor到NSObject的轉換 - 我試過NSObject.FromObject,NSValue.ValueFromHandle等,但還沒有找到任何讓動畫正確拾取開始和結束值的方法。

提供CGColor作爲動畫的NSObject的正確方法是什麼?

謝謝!

回答

9

馬克,

您可以使用

colorAnimation.To = Runtime.GetNSObject (UIColor.Blue.CGColor.Handle); 

獲得動畫的合適的對象。

榮譽爲https://stackoverflow.com/users/187720/geoff-norton爲實際給我使用Runtime.GetNSObject和解決此問題的答案。

希望這有助於

ChrisNTR

+0

太棒了!這適用於我的簡單示例,它不適合我的完整應用程序,但至少現在我知道它*可以*激活顏色!謝謝 – 2011-04-28 21:44:54

0

我剛剛看了一些類似的代碼寫在我們的應用程序(承認CAKeyFrameAnimation而不是CABasicAnimation),他們似乎已經包裝在一個UIView動畫塊.AddAnimation()。例如:

UIView.Animate(4, delegate{ 
    shapeLayer.AddAnimation(colorAnimation, "animateColor"); 
}); 

我不確定這是否是正確的做法,但它似乎在我們的應用程序中工作。

+0

有趣 - 我得看看,謝謝! – 2011-04-28 21:45:54

3

它也可以做到:

colorAnimation.From = NSObject.FromObject (UIColor.Red.CGColor.Handle); 

和MonoTouch的下一個版本(6.0.8+)將允許:

colorAnimation.From = NSObject.FromObject (UIColor.Red.CGColor); 

and the same for CGPath and other INativeObject th在不是NSObject

相關問題