我试图在选择视图时使用幻灯片为NSTabView的每个视图设置动画. 我有一种方式工作,但它只在我第一次选择一个新的选项卡视图时动画.之后我在切换标签视图时看不到动画,虽然我可以看
我有一种方式工作,但它只在我第一次选择一个新的选项卡视图时动画.之后我在切换标签视图时看不到动画,虽然我可以看到每次都触发该功能.
override func tabView(tabView: NSTabView, didSelectTabViewItem tabViewItem: NSTabViewItem) { tabViewItem.view!.setFrameOrigin(NSPoint(x: tabViewItem.view!.frame.origin.x + 300, y: tabViewItem.view!.frame.origin.y)) tabViewItem.view!.animator().setFrameOrigin(NSPoint(x: tabViewItem.view!.frame.origin.x - 300, y: tabViewItem.view!.frame.origin.y)) // i can see this fires every time i switch tab views but the animation only works the fist time }我已经改变了一点来解决第一次问题.只需定义一个整数来存储代理中的上一个选项卡索引.
- (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem{ CABasicAnimation *controlPosAnim = [CABasicAnimation animationWithKeyPath:@"position"]; if (prevTabViewIndex>[tabView indexOfTabViewItem:tabViewItem]) { [controlPosAnim setFromValue:[NSValue valueWithPoint:CGPointMake(tabViewItem.view.frame.origin.x - 500,tabViewItem.view.frame.origin.y)]]; } else { [controlPosAnim setFromValue:[NSValue valueWithPoint:CGPointMake(tabViewItem.view.frame.origin.x + 500,tabViewItem.view.frame.origin.y)]]; } [controlPosAnim setToValue:[NSValue valueWithPoint:CGPointMake(tabViewItem.view.frame.origin.x,tabViewItem.view.frame.origin.y)]]; [[tabViewItem.view layer] addAnimation:controlPosAnim forKey:@"controlViewPosition"]; [tabViewItem.view setAnimations:[NSDictionary dictionaryWithObjectsAndKeys:controlPosAnim, @"frameOrigin", nil]]; [tabViewItem.view.animator setFrameOrigin : CGPointMake(tabViewItem.view.frame.origin.x,tabViewItem.view.frame.origin.y)]; prevTabViewIndex = [tabView indexOfTabViewItem:tabViewItem]; }