CoreAnimation: confused on checking if it has ended

104 Views Asked by At

I have a bunch of NSView subviews which are laid out in a grid. The user can drag the subviews around the grid and on mouseUp I rearrange the subviews inserting the dragged view into the closes spot on the grid it was dropped.

The code to do that is like this:

for(int x=0; x<[pagesToMoveLeft count]; x++) { 

    //get a grid location
    NSRect thisSubviewFrame = NSRectFromString([subviewCoords objectAtIndex:x]);

    //get the view to move
    NSView* viewToMove = [pagesToMoveLeft objectAtIndex:x];
    [[viewToMove animator] setFrame: thisSubviewFrame];

}

I run a check at the end to make sure everything is nice and clean (i.e. - each grid coordinate has a view). To do this, on mouseDown I grab all the subviews frames and store them in an array. On mouseUp, after the animation I call a cleanUp method. In here I grab all the subview frames again and store them. Array 1 and Array 2 should match up, if not something didn't fall into place and I can fix it using Array 1.

I'm not getting the results I expected. When I move items on the screen and they all snap into the correct place, meaning Array 1 and 2 should be equal, they are not. Here's my log:

"{{25, 25}, {170, 220}}",
"{{29, 25}, {170, 220}}",
"{{390, 25}, {170, 220}}",
"{{585, 25}, {170, 220}}",
"{{755, 25}, {170, 220}}",
"{{950, 25}, {170, 220}}",
"{{25, 270}, {170, 220}}",
"{{220, 270}, {170, 220}}",
"{{390, 270}, {170, 220}}",
"{{585, 270}, {170, 220}}",
"{{755, 270}, {170, 220}}",
"{{950, 270}, {170, 220}}"
)
(
"{{25, 25}, {170, 220}}",
"{{220, 25}, {170, 220}}",
"{{390, 25}, {170, 220}}",
"{{585, 25}, {170, 220}}",
"{{755, 25}, {170, 220}}",
"{{950, 25}, {170, 220}}",
"{{25, 270}, {170, 220}}",
"{{220, 270}, {170, 220}}",
"{{390, 270}, {170, 220}}",
"{{585, 270}, {170, 220}}",
"{{755, 270}, {170, 220}}",
"{{950, 270}, {170, 220}}"
)

The first one are the frames after the move (the current locations), the second is their initial locations. Look at item 2 in each array, they should both read (220, 25). Instead the current one reads (29, 25). That's the view I dragged, I moved it from the second location to near the first one (25, 25). The 29 is where I mousedUp...the animation worked, view 2 snapped to the 25,25 location and view 1 moved to the view 2 location.

I think the animation is slower than the code and that's why I am getting the location at mouseUp and not animation end. I looked at the CoreAnimation docs and think I need to use something like animationDidStop but am not sure how to implement it.

Any help would be appreciated.

Edit:

I figured it out -

...
for(int x=0; x<[pagesToMoveLeft count]; x++) { 

        //get a grid location
        NSRect thisSubviewFrame = NSRectFromString([subviewCoords objectAtIndex:x]);

        //get the view to move
        viewToMove = [pagesToMoveLeft objectAtIndex:x];

        CAAnimation *animation = [[viewToMove animationForKey:@"frameOrigin"] copy];
        [viewToMove setAnimations:[NSDictionary dictionaryWithObject:animation forKey:@"frameOrigin"]];
        animation.delegate = self;
        [[viewToMove animator] setFrame: thisSubviewFrame];
}

...

- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag  {

    NSLog(@"ok");
}
0

There are 0 best solutions below