How to check when animation finishes if animation block is
I have a controller which adds as subviews a custom UIView
class called Circle
. Let's call a particular instance of Circle
, "circle".
I have a method in Circle
, animateExpand
, which expands the circle by animating the view.
In the following code (which lives in the controller) I want to alloc
and init
a circle, add it to a NSMutableArray circleArray
, animate the expansion, and at the end of the expansion, i want to remove the object from the array. My attempt:
Circle *circle = [[Circle alloc] init];
[circleArray addObject:circle];
[circle animateExpand];
[circleArray removeObjectIdenticalTo:ci开发者_JAVA百科rcle];
[circle release];
The problem is [circleArray removeObjectIdenticalTo:circle];
gets called before the animation finishes. Presumbly because the animation is done on a seperate thread. I cant implement the deletion in completion:^(BOOL finished){ }
, because the Circle
class does not know about a circleArray
.
Any solutions would be helpful, thanks!
Try:
Circle *circle = [[Circle alloc] init];
if (circle) {
[circleArray addObject: circle];
[UIView
animateWithDuration: 1.0 // or whatever
animations: ^ {
// the animations in -animateExpand "belong to" this outer animation block
[circle animateExpand];
}
completion: ^ (BOOL finished) {
[circleArray removeObjectIdenticalTo: circle];
}];
[circle release];
}
If I'm not mistaken, there is no way just to stop current view animation. However if a new animation is applied to a view, it will stop the currently run one. In this case, before removing your circle, you may start very short animation cycle (say, 0.1 sec), which doesn't do much. And then execute remove.
精彩评论