macOS animation works once, then not again


Steve Mills
 

I'm probably missing something obvious here. I have an NSScrollView. Its document view is an NSImageView. At some point, I add a custom NSView subclass as a subview of the image view. This custom view draws a bezier path over the image. All good so far.

At certain points, I need to animate a zoom/scroll to focus on a particular area of the image, then show the bezier view, and finally fade out the bezier view (a simple "look here" signal to the user). This works the first time (the bezier draws and fades out), but I can't get the bezier view to show itself again.

I've tried many different forms of animation: NSAnimationContext's runAnimationGroup; adding a CABasicAnimation to the bezier view's layer; NSAnimationContext beginGrouping/endGrouping; possibly others. The property I'm changing is bezView.animator.alphaValue or bezView.layer.opacity.

I've tried forcing the bezier view to be layer-based (as well as replacing the host layer with a CAShapeLayer) or just drawing the bezier in the drawRect method. Here's the code that works just peachy on iOS:

[UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:1 options:0 animations:^{
if(!CGRectIsEmpty(boxToZoomTo))
[self.scrollView zoomToRect:boxToZoomTo animated:NO];
} completion:^(BOOL finished) {
self.bezView.alpha = 1;

[UIView animateWithDuration:1 animations:^{
self.bezView.alpha = 0;
}];
}];

And here's one attempt that's similar on macOS (setting allowsImplicitAnimation was something else I tried):

[NSAnimationContext runAnimationGroup:^(NSAnimationContext* _Nonnull context) {
context.duration = 1;
context.allowsImplicitAnimation = YES;

if(!CGRectIsEmpty(boxToZoomTo))
[self.scrollView.animator magnifyToFitRect:boxToZoomTo];
} completionHandler:^{
self.bezView.alphaValue = 1;

[NSAnimationContext runAnimationGroup:^(NSAnimationContext* _Nonnull context) {
context.duration = 1;
context.allowsImplicitAnimation = YES;
self.bezView.animator.alphaValue = 0;
} completionHandler:^{
}];
}];

Shouldn't that just work? Why isn't it redrawing the bez view at alphaValue 1 before fading to 0?

--
Steve Mills
Drummer, Mac geek

Join cocoa@apple-dev.groups.io to automatically receive all group messages.