我想動畫一個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