我试图在CAShapeLayer上设置CGColor fillColor属性的动画.我可以使用Objective-C使用以下语法使其正常工作:
- (void)viewDidLoad { [super viewDidLoad]; // Create the path thisPath = CGPathCreateMutable(); CGPathMoveToPoint(thisPath,NULL,100.0f,50.0f); CGPathAddLineToPoint(thisPath,10.0f,140.0f); CGPathAddLineToPoint(thisPath,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的正确方法是什么?
谢谢!
解决方法
标记,
您可以使用
colorAnimation.To = Runtime.GetNSObject (UIColor.Blue.CGColor.Handle);
获取动画的正确对象.
Kudos到https://stackoverflow.com/users/187720/geoff-norton实际上给了我使用Runtime.GetNSObject的答案并解决了这个问题.
希望这可以帮助,
ChrisNTR
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。