【发布时间】: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;当洗涤器滚动时,我不断收到这些呼叫,当滚动停止时呼叫停止。这感觉最接近我想要的,但我不知道为什么动画结束时会有这么多电话而不是一个。
由于NSScrubber 的NSView 或NSScrollView 没有暴露,我不确定我是否正在查询正确的对象以到达正确的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