我想动画一个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的正确方法是什么?
谢谢!
太棒了!这适用于我的简单示例,它不适合我的完整应用程序,但至少现在我知道它*可以*激活颜色!谢谢 – 2011-04-28 21:44:54