swiftui

SwiftUI - How to Make A Start/Stop Timer

…衆ロ難τιáo~ 提交于 2021-02-08 02:11:42
问题 My goal is to create a view in SwiftUI that starts with 0. When you press the view, a timer should start counting upwards, and tapping again stops the timer. Finally, when you tap again to start the timer, the timer should begin at 0. Here is my current code: import SwiftUI struct TimerView: View { @State var isTimerRunning = false @State private var endTime = Date() @State private var startTime = Date() let timer = Timer.publish(every: 0.001, on: .main, in: .common).autoconnect() var tap:

SwiftUI - How to Make A Start/Stop Timer

大兔子大兔子 提交于 2021-02-08 02:09:30
问题 My goal is to create a view in SwiftUI that starts with 0. When you press the view, a timer should start counting upwards, and tapping again stops the timer. Finally, when you tap again to start the timer, the timer should begin at 0. Here is my current code: import SwiftUI struct TimerView: View { @State var isTimerRunning = false @State private var endTime = Date() @State private var startTime = Date() let timer = Timer.publish(every: 0.001, on: .main, in: .common).autoconnect() var tap:

How to detect and take action when a button is being pressed in SwiftUI

半腔热情 提交于 2021-02-08 02:07:49
问题 I want to execute an action when the button press begins and then when the button stops being pressed. I was looking for a simple solution, but got into more complicated configurations. One option that is pretty simple and close is the one I got from BlueSpud. The button action is not used so I tried: struct MyView: View { @State private var pressing = false var body: some View { Text("Button") .background(self.pressing ? Color.red : Color.blue) .gesture(DragGesture(minimumDistance: 0.0)

Is this intended @State var behaviour in iOS14 or is it a bug?

瘦欲@ 提交于 2021-02-08 02:04:46
问题 When I run the following code compiled for iOS14 on iOS14 sim or device, the updated @State variable "selection" doesn’t get passed to a view displayed as .sheet(… The exact code runs OK on iOS 13.5 sim or 13.6 device The code runs as expected on iOS 14, if I place somewhere in the view a Text(“”) and display the variable being updated. It seems that only if I display the value, the .sheet get's rendered again updating the MyView(tenum: selection) with the correct value. In the code below

Issue with Menu title and decreased opacity

a 夏天 提交于 2021-02-07 23:24:12
问题 I want to use the new Menu with SwiftUI but unfortunately there is one issue which I can't solve. After selecting one value of the Menu the Menutitle looks like so: However I don't want the title to decrease it's opacity. How can I achieve this? Here is my code: struct ContentView: View { @State private var selectionVariable = 0 let sampleDict = [0: "Sample Title 1", 1: "Sample Title 2"] var body: some View { Menu { Picker(selection: $selectionVariable, label: Text("")) { ForEach(sampleDict

Presenting an Alert in SwiftUI using MVVM

泪湿孤枕 提交于 2021-02-07 20:13:50
问题 I'm trying to build an app using SwiftUI and an MVVM architecture. I'd like to have my View present an alert whenever its ViewModel deems it necessary—say, when it has a new result of some sort available from the Model. So suppose whenever the VM detects a new result it sets its status accordingly: The ViewModel: enum Status { case idle case computing case newResultAvailable } class MyViewModel: ObservableObject { @Published var status = Status.idle ... } The View: struct ContentView: View {

SwiftUI - iOS 13 UIViewRepresentable of WKWebView gets Thread 1: EXC_BREAKPOINT crash

佐手、 提交于 2021-02-07 19:27:38
问题 I'm trying to port WKWebView over to SwiftUI. Here's my code: import SwiftUI import WebKit struct ContentView: View { var body: some View { WebViewWrapper() } } /** WKWebView ported over to SwiftUI with `UIViewRepresentable`. */ final class WebViewWrapper: UIViewRepresentable { /// `UIViewRepresentable` required function #1. func makeUIView(context: Context) -> WKWebView { print("make") let webView = WKWebView() /// EXC_BREAKPOINT error here return webView } /// `UIViewRepresentable` required

Localization with String interpolation in SwiftUI

岁酱吖の 提交于 2021-02-07 13:57:48
问题 I am trying to localize my SwiftUI Watch app. I don't have any problems with static strings. I use LocalizedKeyStrings in my Text views and add my translations in Localizable.strings files. For example: Text("history") in Localizable.strings: "history" = "Historique"; Result : "Historique" But I also want to localize stings using interpolation. For example: Text("startCustom \(format: "%.1f",customDistance)") In Localizable.strings, I have tried with different syntax: "startCustom %@" =

Localization with String interpolation in SwiftUI

戏子无情 提交于 2021-02-07 13:57:39
问题 I am trying to localize my SwiftUI Watch app. I don't have any problems with static strings. I use LocalizedKeyStrings in my Text views and add my translations in Localizable.strings files. For example: Text("history") in Localizable.strings: "history" = "Historique"; Result : "Historique" But I also want to localize stings using interpolation. For example: Text("startCustom \(format: "%.1f",customDistance)") In Localizable.strings, I have tried with different syntax: "startCustom %@" =

How to login to Facebook on SwiftUI?

百般思念 提交于 2021-02-07 12:27:46
问题 There are not many resources explaining Facebook Login with SwiftUI. I'm not sure whether my code requires a ViewController or not because Facebook's LoginManager.login() contains a ViewController parameter - however this doesn't really translate to SwiftUI. Regardless, I am trying to login the user to Facebook when they click on the Button below: LoginView.swift import Foundation import SwiftUI import FBSDKLoginKit struct LoginView: View { @EnvironmentObject var auth: UserAuth var body: some