animation - SwiftUI tvOS 按钮动画 - 如何停止 y 轴移动
问题描述
当文本是按钮的一部分时(在 tvOS 14.7 上),这个简单的 x 偏移动画代码会导致文本也在 y 轴上移动
struct Animate: View
{
@State var scrollText: Bool = false
var body: some View {
Button(action: {}, label: {
Text("This is My Text !")
.offset(x: scrollText ? 0 : 200, y: 0)
.animation(Animation.linear(duration: 1).repeatForever(autoreverses: true))
.onAppear {
self.scrollText.toggle()
}
})
}
}
在此处查看动画行为: 选框出错
如何停止 y 轴运动?
解决方案
你总是需要value
为你的动画设置一个。否则,SwiftUI 将对所有更改进行动画处理,包括不需要的定位。
struct Animate: View {
@State var scrollText: Bool = false
var body: some View {
Button(action: {}) {
Text("This is My Text !")
.offset(x: scrollText ? 0 : 200, y: 0)
.animation(Animation.linear(duration: 1).repeatForever(autoreverses: true), value: scrollText) /// only update animation when `scrollText` changes
.onAppear {
// DispatchQueue.main.async { /// you might also need this
self.scrollText.toggle()
// }
}
}
}
}
结果:
推荐阅读
- laravel - 扩展灯塔指令
- c++ - c ++如何结合std :: bind和可变元组?
- c# - 如何在以 *.log.1 开头的滚动 Log4Net 日志文件中自定义文件名?
- laravel - 如何在 laravel 中使用 aws sdk 嵌入 quicksight 仪表板?
- python - 从字符串列表中删除多余的组成字符串
- ios - 单击按钮时在 Swift 中不重复单词 - IOS
- c++ - C ++“翻转”数组中的行
- listview - 对点击的项目进行列表视图自定义 - 快速预览 -Xamarin Forms
- python - 如何使用 selenium python 中的一个类从任何网站获取所有数据
- api - Twitter API 注册审批问题