swift - 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
}
解决方案
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
}) { }
推荐阅读
- dart - 如何在颤振中提供路径 - 路径组合
- java - 使用自定义类加载器加载安全类
- string - Erlang 术语到 Unicode 字符串
- php - Laravel:更新时唯一的句柄
- angular - Angular:构建一个App后,我们怎么知道它是哪个Environment
- symfony - 工作单元异常 Symfony
- wpf - 覆盖 ScrollViewer 和 TextBox(在 WPF 中)后,滚动条的拇指不会根据 TextBox 的文本大小移动
- neo4j - Neo4J - 如何在 allShortestPaths 中排除自引用节点?
- python - Django - 在中间件中添加额外数据以响应
- angularjs - 如何计算离子3中的总量