NSView Remove after animation

余生长醉 提交于 2019-12-08 06:15:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!