Re: macOS animation works once, then not again


Alex Zavatone
 

Can you dump the properties of the object you are trying to animate and the view before and after each attempt?

What thread is this happening on? Anything else besides thread 1?

A year back, I noticed that animations simply weren’t happening in my iOS app, but only on the iPad. At one point, I gave up in frustration and went off to grab a coffee. When I came back, the animation was completed.

What was happening was that the animation was not happening on the main thread and between 15 and 26 seconds later, it executed.

If that’s not it, are you properly starting and ending the animationContext?

What if you dispatch the animation on the main thread as async with GCD?

Alex Zavatone

On Jan 30, 2018, at 11:52 AM, Steve Mills <sjmills@...> wrote:

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.