簡體   English   中英

使用 Monotouch 時如何將 CGColor 轉換為 NSObject?

[英]How do I convert a CGColor into an NSObject when using Monotouch?

我正在嘗試為 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"];
}

這會按預期設置顏色變化的動畫。 當我將它移植到 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");
    }

但 animation 從不播放。 animationStarted 事件確實被引發了,所以大概它正在嘗試運行 animation,但我在屏幕上看不到任何可見的證據。

我在一天的大部分時間里都在玩這個,我認為這是從 CGColor 到 NSObject 的轉換——我試過 NSObject.FromObject、NSValue.ValueFromHandle 等,但還沒有找到任何方法來獲得animation 正確拾取開始和結束值。

為 animation 提供 CGColor 作為 NSObject 的正確方法是什么?

謝謝!

標記,

您可以使用

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

為 animation 獲得正確的 object。

感謝 https://stackoverflow.com/users/187720/geoff-norton實際上給了我使用 Runtime.GetNSObject 的答案並解決了這個問題。

希望這可以幫助,

克里斯NTR

也可以這樣做:

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

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

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

對於CGPath和其他不是NSObjectINativeObject也是如此。

我剛剛查看了在我們的應用程序中編寫的一些類似代碼(誠然CAKeyFrameAnimation而不是CABasicAnimation ),它們似乎已將.AddAnimation()包裝在 UIView animation 塊中。 例如:

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

我不確定這是否是正確的做法,但它似乎適用於我們的應用程序。

暫無
暫無

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

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