- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在研究 Swift UI 的线性动画技术时发现,与我的预期相反,增加持续时间似乎并没有使动画发生得更慢。这是故意的吗?如果是这样,我该如何制作较慢的动画?
示例代码:
struct ButtonView: View {
@State var show: Bool = false
var body: some View {
ZStack{
if show {
withAnimation(.linear(duration: 50)) {
CollapsibleView()
}
}
}
Button(action: { show = !show }) {
Text("Press Me")
}
}
}
struct CollapsibleView: View {
var body: some View {
VStack {
Text("Text 1")
Text("Text 2")
Text("Text 3")
}
}
}
@main
struct app: App {
var body: some Scene {
WindowGroup {
ButtonView()
}
}
}
尝试更改持续时间参数,看看您是否能注意到较慢的动画。我达到了 5000(我假设这是以秒为单位?)并且它仍然以看似相同的速度进行动画处理。
最佳答案
您已将 withAnimation
放置在 View 层次结构中。您真正想要它的地方是在 Button
的操作中:
struct ButtonView: View {
@State var show: Bool = false
var body: some View {
ZStack{
if show {
CollapsibleView()
}
}
Button(action: {
withAnimation(.linear(duration: 10)) {
show.toggle()
}
}) {
Text("Press Me")
}
}
}
或者,您可以在 ZStack
上使用 .animation
:
struct ButtonView: View {
@State var show: Bool = false
var body: some View {
ZStack{
if show {
CollapsibleView()
}
}
.animation(.linear(duration: 10), value: show)
Button(action: {
show.toggle()
}) {
Text("Press Me")
}
}
}
关于swift - Swiftui withAnimation(.linear) 中的持续时间参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69487825/
我注意到使用 swiftUI 的 withAnimation{} 的 XCode 有一个奇怪的行为。 我创建了以下工作示例: import SwiftUI class Block: Hashable,
我想要一个仅在有条件的情况下可见的动画 View 。当我这样做时,我会出现不可预测的行为。特别是,在以下情况下,我希望在 onAppear 中调用一个永远重复的动画,无论它在何时何地初始化,它总是可以
我有一个基于某些状态的 swiftUI 动画: withAnimation(.linear(duration: 0.1)) { self.someState = newState } 上述动画
我在研究 Swift UI 的线性动画技术时发现,与我的预期相反,增加持续时间似乎并没有使动画发生得更慢。这是故意的吗?如果是这样,我该如何制作较慢的动画? 示例代码: struct ButtonVi
我有一个包含多个部分的 UITableView,部分的数量是动态的,也就是说,它们可以随时增加。在 TableView 上重新加载带有动画的部分,其中部分的数量没什么大不了的。我只是这样称呼: [no
我的目标是在将对象添加到@State 事件数组时控制动画类型。 withAnimation 仅在第一次附加到事件数组时发生。然后在附加追加时忽略它。 我目前正在 Xcode 11 beta 4 上运行
我有一个带有可拖动组件(“弹性”文本)的屏幕,当释放时会弹回一个非常突出的 Spring 动画。 有些文本是异步传入的(在本例中,使用 Timer),我希望它能够很好地淡入,同时 Springy 标签
比方说,我有一个像这样的简单个人资料屏幕: class Model: ObservableObject { @Published var isSignedIn = false
我收到了这个警告: 'UIView' may not respond to 'addSubview:withAnimation:' 产生该警告的代码行是这样的: [self.masterView ad
这是我的代码(来自 Core Data 教程): [eventsArray insertObject:event atIndex:0]; NSIndexPath *indexPath = [NSInd
我正在构建一个动画进度按钮,它显示下载进度,然后在下载完成后展开以显示按钮文本(有点像 App Store 按钮)。 我有一个自定义按钮 View ,它采用 @Binding 到外部进度 CGFloa
我看到在 iOS 9 中,setStatusBarHidden(_:withAnimation:) 现在已被弃用,文档说使用 [UIViewController prefersStatusBarHid
我有一个隐藏了状态栏的应用程序。在主视图中,我使用 UIButtons 加载 subview View 。出于某种原因, subview 显示时带有隐藏的状态栏,但是,UI View 不会为此进行调整
我的 Root View Controller 中有代码观察 -[UIApplication sharedApplication] 的 @"statusBarHidden" 属性并调整其 View 的
我是一名优秀的程序员,十分优秀!