【问题标题】:NSScrubber pan animation end notificationNSScrubber 平移动画结束通知
【发布时间】:2019-08-11 08:15:08
【问题描述】:

特定于触摸栏的NSScrubber 控件在平移手势上随惯性滚动。我希望收到此动画结束的通知以执行某些功能。

试试 1

NSScrubberDelegate 有一个我实现的 - didFinishInteractingWithScrubber: 方法。然而,在我停止直接操作擦洗器后不久——将手指从触摸栏上抬起——我收到了回调,但由于惯性,滚动继续发生。最后被选中的项目不是回调此委托方法时的项目。

试试 2

进一步挖掘,我遇到了NSAnimation。虽然没有清楚地记录,但我认为scrubber 也是NSAnimatablePropertyContainer,因为它的selectedIndex 属性文档说可以通过动画代理为选择设置动画:scrubber.animator.selectedIndex = i。鉴于此,假设平滑平移的动画属性是boundsOrigin,我尝试查询它。

我可以通过这样做获得CAAnimation

CAAnimation* a = [NSScrubber defaultAnimationForKey:@"boundsOrigin"];
// returns the same pointer value as above
// a = [myScrubber animationForKey:@"boundsOrigin"];
a.delegate = self;

...

- (void)animationDidStop:(CAAnimation *)anim
                finished:(BOOL)flag {
    if (flag == YES)
        NSLog(@"Animation ended!\n");
}

我得到了a 的有效指针值。然而,我接到了无数电话给animationDidStop,所有电话都是flag = YES;当洗涤器滚动时,我不断收到这些呼叫,当滚动停止时呼叫停止。这感觉最接近我想要的,但我不知道为什么动画结束时会有这么多电话而不是一个。

由于NSScrubberNSViewNSScrollView 没有暴露,我不确定我是否正在查询正确的对象以到达正确的NSAnimation

试试 3

我也尝试了徒劳的操作结束代码的hacky路线

-(void)didFinishInteractingWithScrubber:(NSScrubber *)scrubber {
    NSLog(@"Manipulation ended\n");
    NSAnimationContext*c = NSAnimationContext.currentContext;
    [c setCompletionHandler:^{
        NSLog(@"Inertial scrolling stopped!\n");
    }];
}

完成处理程序几乎立即被调用,惯性滚动停止之前:(

有没有办法知道scrubber的平移手势惯性动画什么时候结束?

【问题讨论】:

    标签: objective-c macos appkit caanimation nstouchbar


    【解决方案1】:

    我终于找到了一种方法,为平移手势的惯性滚动动画结束注册回调。

    与任何其他滚动视图一样,它也有NSScrollViewDidEndLiveScrollNotification。使用通知中心注册回调!

    NSScrollView *sv = myScrubber.enclosingScrollView;
    // register for NSScrollViewWillStartLiveScrollNotification if start is also needed
    [[NSNotificationCenter defaultCenter] addObserverForName:NSScrollViewDidEndLiveScrollNotification
                                                      object:sv
                                                       queue:nil
                                                  usingBlock:^(NSNotification * _Nonnull note) {
                                                      NSLog(@"Scroll complete");
                                                  }];
    

    感谢this answer 展示了这种方法。

    【讨论】:

      猜你喜欢
      • 2020-04-28
      • 2023-02-02
      • 2018-08-01
      • 2012-04-11
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多