swiftui

What gesture can execute code only while user placing their finger on the screen in SwiftUI?

ぃ、小莉子 提交于 2021-01-27 19:22:31
问题 What gesture can execute code only while user placing their finger on the screen? Running certain code should not be Effect of the Cause(gesture), what I want is running certain code while user holding their finger on the screen, and if the user took their finger off the code stops running For example, //some view .unknownGesture{ // running this code only while user placing their finger on the screen } 回答1: Here is possible solution .gesture( DragGesture(minimumDistance: 0) .onChanged() {

NavigationLink inside LazyVGrid cycles all entries on back, SwiftUI

只愿长相守 提交于 2021-01-27 19:13:39
问题 I have an image grid. Each image on tap should push a view on the NavigationView with the image details. The navigation link works as intended, but when I press the back button it opens the next image and so on until it has cycled all the images. What is going on? This is the View : struct ImageGrid: View { @ObservedObject var part: Part @State private var showingImagePicker = false @State private var inputImage: UIImage? var body: some View { Button("add images"){ self.showingImagePicker =

How do I create a Multi Line Text Field in SwiftUI for MacOS?

柔情痞子 提交于 2021-01-27 19:04:26
问题 I am writing my first MacOS app using Swift. I need to create a simple editable text box which allows multiple paragraphs. In HTML, this would be a textarea . I gather that iOS 14 will include TextEditor , but that’s not now, and I don’t know whether that will be in MacOS anyway. Many solutions I have seen presume iOS and UIKit. How do I do this with SwiftUI and MacOS? Update Someone has suggested that the question is similar to this one: How do I create a multiline TextField in SwiftUI? I

Button in ContentView that zooms to current location on MapBox map?

北城余情 提交于 2021-01-27 18:32:47
问题 So....in my coordinator--which conforms to the mapbox delegate protocol, I can just do: mapView.setCenter(mapView.userLocation!.coordinate, zoomLevel: 13, animated: true) and this function works fine in the coordinator or when it is called in the mapView class. The only problem is I don't know how to pass this mapView instance around (specifically back into ContentView where I want to have a button that does the same thing). I also have a LocationManager struct but I don't know how much use

SwiftUI on macOS - handle single-click and double-click at the same time

人盡茶涼 提交于 2021-01-27 18:10:05
问题 Consider this view in SwiftUI: struct MyView: View { var body: some View { Rectangle() .fill(Color.blue) .frame(width: 200, height: 200) .onTapGesture { print("single clicked") } } } Now we're handling single-click. Say you wanna handle double-click too, but with a separate callback. You have 2 options: Add double click handler after single click - this doesn't work at all Add double click handler before single click handler, this kinda works: struct MyView: View { var body: some View {

SwiftUI on macOS - handle single-click and double-click at the same time

泄露秘密 提交于 2021-01-27 18:02:15
问题 Consider this view in SwiftUI: struct MyView: View { var body: some View { Rectangle() .fill(Color.blue) .frame(width: 200, height: 200) .onTapGesture { print("single clicked") } } } Now we're handling single-click. Say you wanna handle double-click too, but with a separate callback. You have 2 options: Add double click handler after single click - this doesn't work at all Add double click handler before single click handler, this kinda works: struct MyView: View { var body: some View {

In SwiftUI List View refresh triggered whenever underlying datasource of list is updated from a view far away in hierarchy

霸气de小男生 提交于 2021-01-27 17:28:54
问题 I am trying to write a "Single View App" in SwiftUI. The main design is very simple. I have a list of items (say Expense) which I am displaying in main view in NavigationView -> List. List View Source Code import SwiftUI struct AmountBasedModifier : ViewModifier{ var amount: Int func body(content: Content) -> some View { if amount <= 10{ return content.foregroundColor(Color.green) } else if amount <= 100{ return content.foregroundColor(Color.blue) } else { return content.foregroundColor(Color

In SwiftUI List View refresh triggered whenever underlying datasource of list is updated from a view far away in hierarchy

走远了吗. 提交于 2021-01-27 17:01:36
问题 I am trying to write a "Single View App" in SwiftUI. The main design is very simple. I have a list of items (say Expense) which I am displaying in main view in NavigationView -> List. List View Source Code import SwiftUI struct AmountBasedModifier : ViewModifier{ var amount: Int func body(content: Content) -> some View { if amount <= 10{ return content.foregroundColor(Color.green) } else if amount <= 100{ return content.foregroundColor(Color.blue) } else { return content.foregroundColor(Color

Mapping a Topbar Menu Item in SwiftUI app to opening a specific bundled PDF?

∥☆過路亽.° 提交于 2021-01-27 14:11:57
问题 SwiftUI, macOS: I'm trying to get a menu item to open "default PDF viewer of your choice", with a specific bundled PDF. Here's what I have thus far: import SwiftUI import WebKit import PDFKit func Guide1(_ sender: Any) { if let pdfURL = Bundle.main.url(forResource: "Guide1", withExtension: "pdf"){ if NSWorkspace.shared.open(pdfURL) { } } } func Guide2(_sender: Any) { if let pdfURL = Bundle.main.url(forResource: "Guide2", withExtension: "pdf"){ if NSWorkspace.shared.open(pdfURL) { } } } Now,

In swiftUI is it possible to add a new view onto the screen with a button?

别来无恙 提交于 2021-01-27 13:23:51
问题 Every time I Tap the button I would like a new cardView to appear. I am wondering if this is possible in swiftUI and if so what action I would need to do it. It would be even better if I am able to pass some parameters in to the cardView struct but any help would be great! struct ContentView: View { var body: some View { ZStack { VStack { TextButton(action: {print("Button tapped")}, text: "new card") CardView() } } } } struct CardView: View { var body: some View { ZStack { Rectangle() .fill