2011-04-28 3 views
3

CAShapeLayer에서 CGColor fillColor 속성에 애니메이션을 적용하려고합니다. 다음 구문을 사용하여 Objective-C를 사용하여 올바르게 작동하도록 할 수 있습니다.Monotouch를 사용할 때 어떻게 CGObject를 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로 제공하는 적절한 방법은 무엇입니까?

감사합니다.

답변

9

마크.

실제로는 나에게 Runtime.GetNSObject를 사용하고이 문제를 해결하기위한 답을주는 https://stackoverflow.com/users/187720/geoff-norton에 대한 명성입니다. 이 도움이

희망,

ChrisNTR

+0

굉장! 그건 내 간단한 예제를 위해 작동, 그것은 내 전체 응용 프로그램에 대한 아니지만 적어도 지금은 * can * can animate the color! 감사 –

0

나는 우리 애플 리케이션 (아마도 이 아닌 CABasicAnimation이 아닌)으로 작성된 유사한 코드를 보았으며 UIView 애니메이션 블록에 .AddAnimation()을 감쌌다. 예 :

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

이것이 올바른 방법인지 확실하지 않지만 Google 앱에서 작동하는 것으로 보입니다. 당신은 애니메이션에 적합한 개체를 가져

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

을 사용할 수 있습니다

+0

흥미 롭습니다 - 나는 그것을보아야 할 것입니다, 고마워요! –

3

그것은 할 수도 있습니다

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

및 MonoTouch의 다음 버전 (6.0.8+) 수 :

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

이고 CGPath 및 기타 INativeObject th 에 있지 않습니다. NSObject.

관련 문제