2011-04-28 76 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

相关问题