首页 > 解决方案 > NSSplitViewItem.isCollapsed ignores animation durations

问题描述

I am trying to collapse an NSSplitViewItem like so

NSAnimationContext.runAnimationGroup({ context in
    context.duration = 0.1
    context.allowsImplicitAnimation = true
    searchItem.isCollapsed = collapsed
}, completionHandler: {
    // do stuff
})

No matter what I set for the duration, the animation duration of the collapse animation does not change.

Setting the duration on a CATransaction also does not work.

Checking the header files it mentions this:

The exact animation used can be customized by setting it in the -animations dictionary with a key of "collapsed".

That raises even more questions. When do I set this animation? What keypath do I animate with this animation? What to/from values does it expect? etc... All I want to do is change its duration.

Solution:

As per @Loengard's answer this is what I went with

NSAnimationContext.runAnimationGroup { _ in
    let animation = CABasicAnimation(keyPath: nil)
    animation.duration = 0.1

    searchItem.animations["collapsed"] = animation
    searchItem.animator().isCollapsed = collapsed
}

标签: swiftmacoscocoa

解决方案


The dictionary the header file refers to is searchItem.animations. You don't need to specify fromValue or toValue, just customize duration like this:

    NSAnimationContext.runAnimationGroup({ context in
        context.duration = 0.1
        context.allowsImplicitAnimation = true

        let collapseAnimation = CABasicAnimation(keyPath: "collapsed")
        collapseAnimation.duration = 0.1

        var existingAnimations = searchItem.animations
        existingAnimations["collapsed"] = collapseAnimation
        searchItem.animations = existingAnimations

        searchItem.isCollapsed = !searchItem.isCollapsed

    }) {  }

推荐阅读