使用Monotouch时如何将CGColor转换为NSObject?

Mar*_*ith 3 cgcolor nsobject xamarin.ios ios

我试图在CAShapeLayer上设置CGColor fillColor属性的动画.我可以使用Objective-C使用以下语法使其正常工作:

- (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"];
}
Run Code Online (Sandbox Code Playgroud)

这会按预期动画颜色偏移.当我把它移植到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");
    }
Run Code Online (Sandbox Code Playgroud)

但动画永远不会播放.animationStarted事件确实被提升,因此可能是它试图运行动画,但我没有在屏幕上看到任何可见的证据.

我已经玩了一天中的大部分时间,我认为这是从CGColor转换为NSObject - 我尝试过NSObject.FromObject,NSValue.ValueFromHandle等,但是还没有找到任何方法来获取动画以正确拾取开始和结束值.

为动画提供CGColor作为NSObject的正确方法是什么?

谢谢!

chr*_*ntr 9

标记,

您可以使用

colorAnimation.To = Runtime.GetNSObject (UIColor.Blue.CGColor.Handle);
Run Code Online (Sandbox Code Playgroud)

获取动画的正确对象.

荣誉对/sf/users/13140431/的实际上是给我的答案用Runtime.GetNSObject和解决这个问题.

希望这可以帮助,

ChrisNTR