5

我试图在选择视图时使用幻灯片为 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
}
4

3 回答 3

0

您必须确保 NSTabView 项目是层支持的,所以一旦(例如在 windowDidLoad() 中)调用这个:

for tabViewItem in self.tabView.tabViewItems {
  if let tabViewItemView = tabViewItem.view {
    tabViewItemView.wantsLayer = true
  }
}

这是我对 Swift 的 muharrem 答案的更新版本,并带有正确的动画偏移量:

private var previousTabViewIndex: Int = 0

public func tabView(_ tabView: NSTabView, didSelect tabViewItem: NSTabViewItem?) {
  guard let tabViewItem = tabViewItem,
        let tabViewItemView = tabViewItem.view,
        let tabViewItemLayer = tabViewItemView.layer else { return }

  let animation = CABasicAnimation(keyPath: #keyPath(CALayer.position))
  let tabViewIndex = tabView.indexOfTabViewItem(tabViewItem)
  let origin = CGPoint(x: tabViewItemView.frame.origin.x, y: tabViewItemView.frame.origin.y)
  var offsetX = self.tabView.frame.width
  if self.previousTabViewIndex > tabViewIndex {
    offsetX *= -1
  }

  animation.fromValue = NSValue(point: CGPoint(x: origin.x + offsetX, y: origin.y))
  animation.toValue = NSValue(point: origin)
  tabViewItemLayer.add(animation, forKey: "controlViewPosition")

  self.previousTabViewIndex = tabViewIndex
}
于 2020-04-29T10:30:14.600 回答
0

我已经对其进行了一些更改以解决第一次出现的问题。只需定义一个整数以将先前的选项卡索引存储在您的委托中。

- (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];
}
于 2017-01-01T13:12:39.527 回答
0

如果您使用的是 NSTabViewController。您只需要在 viewDidLoad 方法中设置 transitionOptions 。

class TabVC: NSTabViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        /// When new tab will select, it will perform slide transition.
        self.transitionOptions = .slideRight
    }
    
}
于 2021-10-27T22:23:14.187 回答