【发布时间】:2026-02-20 08:25:02
【问题描述】:
我有两个滚动视图,其中一个带有约束,使其占据整个父视图,另一个紧挨着它,但隐藏在父视图的边界之外。我想为两个向左滑动设置动画,直到第二个滚动视图占据整个父视图,而第一个滚动视图现在超出了左侧的范围。如何为使用 Swift 的 OS X 应用程序执行此操作?
【问题讨论】:
标签: xcode swift macos animation
我有两个滚动视图,其中一个带有约束,使其占据整个父视图,另一个紧挨着它,但隐藏在父视图的边界之外。我想为两个向左滑动设置动画,直到第二个滚动视图占据整个父视图,而第一个滚动视图现在超出了左侧的范围。如何为使用 Swift 的 OS X 应用程序执行此操作?
【问题讨论】:
标签: xcode swift macos animation
通过一些狩猎和拼凑来解决这个问题。
为要在动画中更改的约束创建一个 IBOutlet。在这种情况下,为每个滚动视图使用前导约束。
@IBOutlet weak var ScrollView1LeadingConstraint: NSLayoutConstraint!
@IBOutlet weak var ScrollView2LeadingConstraint: NSLayoutConstraint!
然后,使用以下内容:
NSAnimationContext.runAnimationGroup({ (context) -> Void in
context.duration = //length of the animation time in seconds
self. ScrollView1LeadingConstraint.animator().constant = //negative width of scroll view
self.ScrollView2LeadingConstraint.animator().constant = 0
}, completionHandler: { () -> Void in
//insert any completion code here
})
这将使第一个滚动视图动画离开左侧的框架,并将第二个滚动视图移动到它原来的位置。
【讨论】: