【问题标题】:CAAnimation animationDidStop not called with NSAnimationContext未使用 NSAnimationContext 调用 CAAnimation animationDidStop
【发布时间】:2012-08-17 11:42:06
【问题描述】:

我正在使用这段代码为 NSScrollView 滚动制作动画:

    [NSAnimationContext beginGrouping];
    NSClipView* clipView = [self contentView];
    NSPoint newOrigin = [clipView bounds].origin;
    newOrigin.x = page*kGalleryWidth;
    [[clipView animator] setBoundsOrigin:newOrigin];
    [NSAnimationContext endGrouping];

现在我正在尝试获取触发动画结束的事件。 我已经阅读了几行代码,使用 CAAnimation 可以轻松实现,但我做不到。

我已经尝试了以下代码:

CAAnimation *moveAnimation = [[self.contentView animationForKey:@"frameOrigin"] copy];
moveAnimation.delegate = self;
[self.contentView setAnimations:[NSDictionary dictionaryWithObject:moveAnimation forKey:@"frameOrigin"]];
  • (void)animationDidStop:(CAAnimation *)动画完成:(BOOL)flag{

    NSLog(@"STOP!");

}

有人可以帮助我吗?

谢谢!

【问题讨论】:

    标签: objective-c macos cocoa delegates caanimation


    【解决方案1】:

    你可以使用这个功能:

    [[NSAnimationContext currentContext] setCompletionHandler:<#(void (^)(void))completionHandler#>]
    
     [NSAnimationContext beginGrouping];
        [[NSAnimationContext currentContext] setCompletionHandler:^{
            NSLog(@"animation stop!");
        }];
        NSClipView* clipView = [self contentView];
        NSPoint newOrigin = [clipView bounds].origin;
        newOrigin.x = page*kGalleryWidth;
        [[clipView animator] setBoundsOrigin:newOrigin];
        [NSAnimationContext endGrouping];
    

    【讨论】:

      【解决方案2】:
      [UIView beginAnimations:@"goback" context:nil];
      [UIView setAnimationDuration:0.3f];
      [UIView setAnimationBeginsFromCurrentState:NO];
      dragOperation.draggable.center =dragOperation.initialPoint;
      [UIView setAnimationDelegate:self];
      [UIView setAnimationDidStopSelector: @selector(animationDidStop:finished:context:)];
      UIView commitAnimations];
      
      
      
      
      -(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
          [self.layer removeAnimationForKey:@"goback"];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多