问题
I do not know why it is proving to be so difficult, but basically I want to animate an NSView across my screen, and once it is done that animation, remove that NSView. It however seems that I can find absolutely no reference on how to do this. Can someone please help?
I am starting my animation like this
NSRect frame = blob.frame;
frame.origin.x = animationStopX;
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:timeToDisappear];
[[blob animator] setFrame:frame];
[NSAnimationContext endGrouping];
I have no way of getting a callback from this once it completes or anything.
回答1:
Right after your beginGrouping
statement, add this:
[[NSAnimationContext currentContext] setCompletionHandler:^{
[self.blob removeFromSuperview];
}];
setCompletionHandler:
is a method in the NSAnimationContext
class.
回答2:
In case 10.6 or below (and thus NSAnimationContext's completionHandler
) is not an option this approach just uses blocks and will work on 10.6+:
double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[mySubView removeFromSuperview];
});
来源:https://stackoverflow.com/questions/10360150/nsview-remove-after-animation