Bug 8458 - ShadowPath animation doesn't work
Summary: ShadowPath animation doesn't work
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 6.0.x
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-11-16 06:58 UTC by BB
Modified: 2012-11-16 11:35 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description BB 2012-11-16 06:58:27 UTC
Animation for "ShadowPath" doesn't seem to work.

I have narrowed it down to this code which DOES WORK in Objective-C (a triangular shadow moves across the screen):

  UIView *parentView=((UIViewController *)self.sourceViewController).view;
  UIView *view=[[UIView alloc ] initWithFrame:CGRectMake(0.0f,0.0f,100.0f,100.0f)];
  view.backgroundColor=[UIColor redColor];
  [parentView addSubview:view];

  CALayer *layer=view.layer;
  layer.shadowColor=[UIColor blackColor].CGColor;
  layer.shadowOpacity=1.0f;
  layer.shadowOffset=CGSizeMake(0.0f,0.0f);
  layer.shadowRadius=0.0f;
  
  UIBezierPath *startShadowPath=[UIBezierPath bezierPath];
  [startShadowPath moveToPoint:CGPointMake(100.0f,100.0f)];
  [startShadowPath addLineToPoint:CGPointMake(140.0f, 100.0f)];
  [startShadowPath addLineToPoint:CGPointMake(140.0f, 60.0f)];
  [startShadowPath closePath];

  UIBezierPath *endShadowPath=[UIBezierPath bezierPath];
  [endShadowPath moveToPoint:CGPointMake(200.0f,200.0f)];
  [endShadowPath addLineToPoint:CGPointMake(240.0f, 200.0f)];
  [endShadowPath addLineToPoint:CGPointMake(240.0f, 160.0f)];
  [endShadowPath closePath];

  layer.shadowPath=[startShadowPath CGPath];

  [CATransaction begin];
  CABasicAnimation *shadowPathAnimation=[CABasicAnimation animationWithKeyPath:@"shadowPath"];
  id startShadow=(id)startShadowPath.CGPath;
  id endShadow=(id)endShadowPath.CGPath;
  shadowPathAnimation.fromValue=startShadow;
  shadowPathAnimation.toValue=endShadow;
  shadowPathAnimation.duration=3.0f;
  [CATransaction setCompletionBlock:^{
    NSLog(@"Complete");
  }];
  [layer addAnimation:shadowPathAnimation forKey:@"shadowPathAnimation"];
  [CATransaction commit];

and done a line-by-line translation to MonoTouch c# which DOES NOT WORK (the shadow doesn't move):

      UIView parentView=((UIViewController)this.SourceViewController).View;
      UIView view=new UIView(new RectangleF(0.0f,0.0f,100.0f,100.0f));
      view.BackgroundColor=UIColor.Red;
      parentView.AddSubview(view);
      CALayer layer=view.Layer;
      layer.ShadowColor=UIColor.Black.CGColor;
      layer.ShadowOpacity=1.0f;
      layer.ShadowOffset=new SizeF(0.0f,0.0f);
      layer.ShadowRadius=0.0f;

      UIBezierPath startShadowPath=new UIBezierPath();
      startShadowPath.MoveTo(new PointF(100.0f,100.0f));
      startShadowPath.AddLineTo(new PointF(140.0f, 100.0f));
      startShadowPath.AddLineTo(new PointF(140.0f, 60.0f));
      startShadowPath.ClosePath();

      UIBezierPath endShadowPath=new UIBezierPath();
      endShadowPath.MoveTo(new PointF(200.0f,200.0f));
      endShadowPath.AddLineTo(new PointF(240.0f, 200.0f));
      endShadowPath.AddLineTo(new PointF(240.0f, 160.0f));
      endShadowPath.ClosePath();

      layer.ShadowPath=startShadowPath.CGPath;

      CATransaction.Begin();
      CABasicAnimation shadowPathAnimation=CABasicAnimation.FromKeyPath("shadowPath");
      NSObject startShadow=NSValue.FromObject(startShadowPath.CGPath);
      NSObject endShadow=NSValue.FromObject(endShadowPath.CGPath);
      shadowPathAnimation.From=startShadow;
      shadowPathAnimation.To=endShadow;
      shadowPathAnimation.Duration=3.0f;
      CATransaction.CompletionBlock=delegate {
        Console.WriteLine(@"Complete");
       };
      layer.AddAnimation(shadowPathAnimation,"shadowPathAnimation");
      CATransaction.Commit();


I have other animations such as "shadowOpacity" and "transform" which are all working fine but there does seem to be a problem with shadowPath.
Comment 1 Sebastien Pouliot 2012-11-16 09:47:51 UTC
      NSObject startShadow=NSValue.FromObject(startShadowPath.CGPath);
      NSObject endShadow=NSValue.FromObject(endShadowPath.CGPath);

^ I do not think those conversion are valid. FromObject comes from NSObject, not NSValue like other conversion methods. I would have translated the code (not tried yet) to:

      shadowPathAnimation.From = startShadowPath;
      shadowPathAnimation.To = endShadowPath;
Comment 2 BB 2012-11-16 09:54:40 UTC
OK, I tried that, but it doesn't work either.
Comment 3 Sebastien Pouliot 2012-11-16 10:23:18 UTC
So CABasicAnimation does not handle UIBezier and CG* objects are not NSObject. However they both share the same (CoreFoundation) CFObject ancestor (which is why it works with ObjC).

To get the same result you need to add:

    using MonoTouch.ObjCRuntime;

and use:

    shadowPathAnimation.From= Runtime.GetNSObject (startShadowPath.CGPath.Handle);
    shadowPathAnimation.To = Runtime.GetNSObject (endShadowPath.CGPath.Handle);

That will create NSObject that are (on the native side) CGPath.
Comment 4 Sebastien Pouliot 2012-11-16 10:24:21 UTC
p.s. I'll look if we can roll this into FromObject so it becomes transparent to developers.
Comment 5 BB 2012-11-16 10:34:10 UTC
yea, that works fine now, but is a bit of a trap for developers.

Many thanks for your (very prompt) help.
Comment 6 Sebastien Pouliot 2012-11-16 11:35:30 UTC
Fixed in
ios6: 79cbd0cdcfb7332a7597ede04d47bb6eb2201123
master: 9f492cb39ab6ea6485be79e439d2cd3a71a09d47
QA: unit tests added to both branches.


Note that using the Handle with NSObject already works:

   NSObject.FromObject (endShadowPath.CGPath.Handle)

but the next release with allow an easier

   NSObject.FromObject (endShadowPath.CGPath)

to achieve the same.

Thanks for the feedback and providing test cases!