Pregunta

Estoy tratando de animar una propiedad CGColor FillColor en un Cashapelayer. Puedo hacer que funcione bien usando Objective-C con la siguiente sintaxis:

- (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"];
}

Esto anima el cambio de color como se esperaba. Cuando porté esto a Monotouch, lo intenté:

    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");
    }

Pero la animación nunca juega. El evento de animación se plantea, por lo que presumiblemente está tratando de ejecutar la animación, pero no veo ninguna evidencia visible en la pantalla.

He estado jugando con esto durante la mayor parte de un día y creo que es la conversión de un cgcolor en un nsobject: he probado nsobject.fromobject, nsvalue.valuefromhandle, etc. Pero no he encontrado ninguna forma de obtener La animación para recoger correctamente los valores de inicio y finalización.

¿Cuál es la forma correcta de suministrar un CGColor como un objeto NSObject para una animación?

¡Gracias!

¿Fue útil?

Solución

Marca,

Puedes usar

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

Para obtener el objeto correcto para la animación.

Felicitaciones a https://stackoverflow.com/users/187720/geoff-norton para darme la respuesta para usar el tiempo de ejecución.getNsObject y solucionar este problema.

Espero que esto ayude,

Chrisntr

Otros consejos

También es posible hacer:

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

y las próximas versiones de Monotouch (6.0.8+) permitirán:

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

y lo mismo para CGPath y otra INativeObject que no son NSObject.

Acabo de mirar un código similar escrito en nuestra aplicación (es cierto que CAKeyFrameAnimation más bien que CABasicAnimation) y parecen haber envuelto el .AddAnimation() En un bloque de animación UIView. P.ej:

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

No estoy seguro de si esta es la forma correcta de hacer esto, pero parece funcionar en nuestra aplicación.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top