在 SwiftUI 中,我尝试使用按钮启动和停止动画。我在 SwiftUI 中找不到可以停止自动化的方法。在 Swift 4 中,我曾经说过“startButton.layer.removeAllAnimations()”。SwiftUI 中是否有等价物?
对于下面的代码,我将如何使用 start 的值来启用/禁用动画。我试过 .animation(start ? Animation(...) : .none) 没有任何运气[在按钮内创建一些奇怪的动画]。
import SwiftUI
struct Repeating_WithDelay: View {
@State private var start = false
var body: some View {
VStack(spacing: 20) {
TitleText("Repeating")
SubtitleText("Repeat With Delay")
BannerText("You can add a delay between each repeat of the animation. You want to add the delay modifier BEFORE the repeat modifier.", backColor: .green)
Spacer()
Button("Start", action: { self.start.toggle() })
.font(.largeTitle)
.padding()
.foregroundColor(.white)
.background(RoundedRectangle(cornerRadius: 10).fill(Color.green))
.overlay(
RoundedRectangle(cornerRadius: 10) …Run Code Online (Sandbox Code Playgroud) 我有一个 TextField 并使用 UIKeyboardType.numberPad 作为键盘输入(只需要整数输入)。此键盘类型不带有 Return/Done 键。
\n\n我一直在努力寻找一种方法将其添加到键盘上。我过去用 Swift 做过这个,但不知道如何用 SwiftUI 做同样的事情。任何帮助将不胜感激。
\n我正在尝试将一个 SwiftUI 视图集成到现有的 UIKit 应用程序中,该视图对@State 变量的更改进行动画处理(最初的进度是@State 私有进度:CGFloat = 0.5在 SwiftUI 视图中)。我已经阅读了大量关于将 SwiftUI 集成到 UIKit(@State、@Binding、@Environment 等)的搜索结果,但似乎无法弄清楚这一点。
我创建了一个非常简单的示例来说明我正在尝试做什么,因为我相信一旦我看到这个答案,我就可以将它应用到我现有的应用程序中。
Storyboard 只是带有 UISlider 的视图控制器。下面的代码显示了 SwiftUI 视图,但我希望它在我移动 UISlider 时更新。
import SwiftUI
class ViewController: UIViewController {
var progress: CGFloat = 0.5
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let frame = CGRect(x: 20, y: 200, width: 400, height: 400)
let childView = UIHostingController(rootView: Animate_Trim(progress: progress))
addChild(childView)
childView.view.frame = frame
view.addSubview(childView.view)
childView.didMove(toParent: self)
}
@IBAction func sliderAction(_ sender: …Run Code Online (Sandbox Code Playgroud)