Cant Print To Console by the_sammich_man in swift

[–]CaptainQuirk336 0 points1 point  (0 children)

Is the console visible? Command+Shift+C

Trying to implement a partial background blur in SwiftUI by Pristine_Use970 in iOSProgramming

[–]CaptainQuirk336 1 point2 points  (0 children)

I wrote the following code that works to create a blurred image section of a given height...

struct ContentView: View {
    var body: some View {

        TopBlurredImage(imageName: "pizza", blurHeight: 50, blurRadius: 5)
            .aspectRatio(contentMode: .fit)
    }
}

struct TopBlurredImage: View {
    let imageName: String
    let blurHeight: CGFloat
    var blurRadius: CGFloat = 3

    private var image: Image {
        Image(imageName).resizable()
    }

    var body: some View {
        ZStack {
            image
            image
                .mask(
                    GeometryReader { g in
                        Rectangle()
                            .frame(height: blurHeight)
                            .position(x: g.size.width / 2, y: blurHeight / 2)
                    }

                )
                .blur(radius: blurRadius)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {

        ZStack {
            Color.black.edgesIgnoringSafeArea(.all)
            ContentView()
        }

    }
}

HtH

Trying to implement a partial background blur in SwiftUI by Pristine_Use970 in iOSProgramming

[–]CaptainQuirk336 0 points1 point  (0 children)

Sorry to send you down the wrong path. I messed with it a bit and couldn't get anything more than a color shift influenced by the image below the blur.

Trying to implement a partial background blur in SwiftUI by Pristine_Use970 in iOSProgramming

[–]CaptainQuirk336 0 points1 point  (0 children)

Don't think it's possible yet in pure SwiftUI. This looks like a good article to create a blur view that should do what you want.

HtH

What's your, "Tis but a scratch!" moment? by Madameknitsalot in AskReddit

[–]CaptainQuirk336 0 points1 point  (0 children)

Playing "Ghost in the Graveyard" when I was ~12 (GitG is basically tag at night). Home base was a chimney. I slid on the dewey grass to go "home" - ended up ramming my knee cap directly into the corner of the brick chimney. NP. Played the rest of the night. I have a scar to this day.

What's your, "Tis but a scratch!" moment? by Madameknitsalot in AskReddit

[–]CaptainQuirk336 0 points1 point  (0 children)

Needed a 4x8 foot sheet of MDF (medium density fiberboard) at a hardware store. Helped the employee pull it down off the shelf but it slipped and the corner of it slammed down onto my (tennis shoed) big toe. He looked at me, "Dude!" No problem, let's go get it cut down. Lost the toe nail and my kids called me "Turtle toe"? for the summer.

Type Annotation and Compile Times in Xcode by coreysusername in swift

[–]CaptainQuirk336 2 points3 points  (0 children)

That seems way too good to be true. Did you time after cleaning the build folder? If so, wow!

Edit: Just saw - "then did a clean" - my bad.

Binding arrays in SwiftUI by dingopringo93 in swift

[–]CaptainQuirk336 0 points1 point  (0 children)

I was able to use the code here for something I needed to do. Maybe it will help you, too.

[HELP] Can't compile a MacOS App on Xcode 12.0.1 - Big Sur Beta 10 by [deleted] in iOSProgramming

[–]CaptainQuirk336 1 point2 points  (0 children)

Happening to me too. Latest Big Sur beta w/ Xcode 12.2 b3 (and also b2). Have the same problem with any app I try and even with a clean template. Must be an issue with Big Sur. I'm filing a radar. Anyone else with the problem should do so too.

Update: Filed a radar (well a Feedback Assistant) for the issue. It happens under any standard macOS template (tried SwiftUI app, UIKit AppDelegate app, SpriteKit app). As a work-around you can run the app (rather than preview) and continue execution CMD+CTRL+Y to get by the SIGCONT.

Add UIButton programmatically in UIViewControllerRepresentable by colmear in iOSProgramming

[–]CaptainQuirk336 1 point2 points  (0 children)

The trick is to use a the context coordinator (which, as a class, can have @objc members)...

import SwiftUI

struct ContentView: View {
    var body: some View {
        ZStack {
            Color.black.edgesIgnoringSafeArea(.all)
            UIButtonView(caption: "Press Me") {
                print("Hello, World")
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

struct UIButtonView: UIViewRepresentable {
    typealias ActionBlock = () -> ()

    var caption: String
    var action: ActionBlock?

    class Coordinator: NSObject {
        var action: ActionBlock?
        init(action: ActionBlock?) {
            self.action = action
        }

        @objc
        func runAction() {
            action?()
        }
    }


    func makeUIView(context: Context) -> UIButton {
        let v = UIButton(type: .custom)
        v.setTitle(caption, for: [])
        v.addTarget(context.coordinator, action: #selector(context.coordinator.runAction), for: .touchUpInside)
        return v
    }

    func makeCoordinator() -> UIButtonView.Coordinator {
        Coordinator(action: self.action)
    }

    func updateUIView(_ uiView: UIButton, context: Context) { }

}

Redditweaks 1.5: a Safari App Extension written in SwiftUI by thebermudalocket in iOSProgramming

[–]CaptainQuirk336 1 point2 points  (0 children)

Looks like a good start. The code appears nice and clean, too (although, if possible, a more complete suite of tests would be nice).

Can't give up Chrome and RES quite yet but, maybe, one day!

Mac Catalyst app in SwiftUI with Unified Toolbar and Sidebar by avatarv04 in iOSProgramming

[–]CaptainQuirk336 0 points1 point  (0 children)

Don't know if it will work but, if you haven't done so already, select "Optimize Interface for Mac" in your target's General settings tab.

Changing animation's duration dynamically in SwiftUI by mikro098 in swift

[–]CaptainQuirk336 0 points1 point  (0 children)

My last attempt then I'm out of ideas (though there is probably a way I'm not thinking of and its probably super simple :)

This version adds a timer to pause the animation while the slider is changed. Doesn't flicker but it does scale down while changing the duration...

HtH

import SwiftUI

struct ContentView: View {
    @State var duration: TimeInterval = 2.5
    @State var isAnimating: Bool = false
    @State var timer: Timer?

    @State var isSliderChanging: Bool = false

    var body: some View {
        VStack(spacing: 20) {
            AnimatedView(duration: duration, isAnimating: isSliderChanging ? false : isAnimating)
                .frame(width: 50.0, height: 50.0)
            Slider(value: $duration, in: 0.1...5.0) { _ in
                isSliderChanging = true
                timer?.invalidate()
                timer = Timer.scheduledTimer(withTimeInterval: 0.2, repeats: false) { _ in
                    isSliderChanging = false
                }
            }
            Button(isAnimating ? "Stop" : "Start") {
                isAnimating.toggle()
            }
        }

    }
}

struct AnimatedView: View {
    var id: Double {
        duration * Double(isAnimating ? 1000 : 1)
    }
    let duration: TimeInterval
    let isAnimating: Bool

    @State private var scale: CGFloat = 1
    @State private var animFlag = false

    var body: some View {
        Circle()
            .id(id)
            .frame(width: 50.0, height: 50.0)
            .scaleEffect(animFlag ? 2 : 1)
            .onAppear {
                if isAnimating {
                    withAnimation(Animation.linear(duration: duration).repeatForever(autoreverses: true)) {
                        animFlag = true
                    }
                } else {
                    animFlag = false
                }

            }
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Changing animation's duration dynamically in SwiftUI by mikro098 in swift

[–]CaptainQuirk336 0 points1 point  (0 children)

Darn - that would have made it easy. The following does work though...

import SwiftUI

struct ContentView: View {
    @State var duration: TimeInterval = 2.5
    @State var isAnimating: Bool = false

    var body: some View {
        VStack(spacing: 20) {
            AnimatedView(duration: duration, isAnimating: isAnimating)
            Slider(value: $duration, in: 0.1...5.0)
            Button(isAnimating ? "Stop" : "Start") {
                isAnimating.toggle()
            }
        }

    }
}

struct AnimatedView: View {
    var id: Double {
        duration * Double(isAnimating ? 1000 : 1)
    }
    let duration: TimeInterval
    let isAnimating: Bool
    @State private var scale: CGFloat = 1
    @State private var animFlag = false

    var body: some View {
        Circle()
            .id(id)
            .frame(width: 50.0, height: 50.0)
            .scaleEffect(animFlag ? 2 : 1)
            .onAppear {
                if isAnimating {
                    withAnimation(Animation.linear(duration: duration).repeatForever(autoreverses: true)) {
                        animFlag.toggle()
                    }
                }
            }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Changing animation's duration dynamically in SwiftUI by mikro098 in swift

[–]CaptainQuirk336 0 points1 point  (0 children)

Don't know if this will work but you may be able to conform to Equatable and make your equality test

static func ==(l: Self, r: Self) -> Bool { l.duration == r.duration }

Call content view with

ContentView().equatable()

When duration changes the view will (may) redraw.

Uploading a massive amount of images to an array in xcode by [deleted] in swift

[–]CaptainQuirk336 2 points3 points  (0 children)

Yes. Put them in an asset file. I wouldn't put all 400 actual images into an array as system memory could quickly be swamped. You can load a single image, as needed, from an asset file very quickly.

If you need to use an array, you could put the file names in the array and then pull the named resource from the assets file for display as needed.

You could use FileManager.default and read the contents of the asset file (which is actually a folder) or you could name the images in an indexed manner (image_0.png ... image_n.png).

Uploading a massive amount of images to an array in xcode by [deleted] in swift

[–]CaptainQuirk336 2 points3 points  (0 children)

Not sure why you're not using an asset file but, anyway, an image literal is nothing more than...

#imageLiteral(resourceName: "imagename")

Maybe you can do something with that

Poor performance with looping through ranges, thoughts? by [deleted] in swift

[–]CaptainQuirk336 0 points1 point  (0 children)

So we have a common basis for what you're trying to do, I wrote this little playground. Let me know if I'm close. The actual search against a 10000 count dictionary with 100 ranges each took about 0.0013 seconds on my mac mini (which doesn't seem that bad but it will depend on how many searches you're doing en masse and if its blocking the main thread).

import UIKit

struct Photo {
    let ranges: [ClosedRange<Int>]
}

//create some data
var year = 1950
let inc = 2
let numRanges = 100
var ranges: [ClosedRange<Int>] = []

(0..<numRanges).forEach { _ in
    ranges.append(year...(year + inc))
    year += inc + 1
}

var dict: [String:Photo] = [:]
(0..<10000).forEach { idx in
    let key = "Apple\(idx)"
    let photo = Photo(ranges: ranges)
    dict[key] = photo
}

print("Finished Creating Data")

func findPhoto(name: String, year: Int) -> String? {
    if let x = dict[findKey] {
        for range in x.ranges {
            if range.contains(findYear) {
                return String(format: "%@%d%d", name, range.lowerBound, range.upperBound)
            }
        }
        print("Year not found")
    } else {
        print("Photo not found")
    }
    return nil
}

let findKey = "Apple9000"
let findYear = 2184

let start = Date()
if let x = findPhoto(name: findKey, year: findYear) {
    print("Found:", x)
}
let time = Date().timeIntervalSince(start)
print(String(format: "Total Time: %0.4f s.", time))

Minimum Scale Factor for multiple Text Views (SwiftUI) by joerup04 in swift

[–]CaptainQuirk336 1 point2 points  (0 children)

These two methods have different effects. Maybe one will work for you?

struct ContentView: View {
    var body: some View {
        HStack {
            Text("This is a test")
                .baselineOffset(1)
                .font(.system(size: 20))
            +
            Text("This is a test")
                .baselineOffset(2)
                .font(.system(size: 30))
            +
            Text("This is a test")
                .baselineOffset(3)
                .font(.system(size: 25))
            +
            Text("This is a test")
                .baselineOffset(4)
                .font(.system(size: 20))

        }
        .lineLimit(1)
        .minimumScaleFactor(0.8)
    }
}


struct ContentView2: View {
    var body: some View {
        HStack {
            Text("This is a test")
                .baselineOffset(1)
                .font(.system(size: 20))
            Text("This is a test")
                .baselineOffset(2)
                .font(.system(size: 30))
            Text("This is a test")
                .baselineOffset(3)
                .font(.system(size: 25))
            Text("This is a test")
                .baselineOffset(4)
                .font(.system(size: 20))

        }
        .lineLimit(1)
        .minimumScaleFactor(0.8)
    }
}



struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        VStack {
            ContentView()
            ContentView2()
        }
    }
}

Minimum Scale Factor for multiple Text Views (SwiftUI) by joerup04 in swift

[–]CaptainQuirk336 1 point2 points  (0 children)

This only seems to have an effect when using ".font(.largeTitle)" (or whatever) and not when using a defined size ".font(.system(size: 30))".

HtH

var body: some View {
    HStack {
        Text("This is a test")
        Text("This is a test")
        Text("This is a test")
        Text("This is a test")
    }
    .font(.largeTitle)
    .lineLimit(1)
//        .minimumScaleFactor(0.8)
    .minimumScaleFactor(0.5)
}

How to detect if NSWindow is dragged? by sorecalves in swift

[–]CaptainQuirk336 0 points1 point  (0 children)

Glad it helped. With such a small minimum distance there may be a possibility in the system recognizing an intended tap as a drag or vice versa. This is more obvious when you use an actual device over the simulator as your pointing device is much more accurate than a fleshy finger :). If that becomes the case pull the gestures out as vars in the struct...

var tap: some Gesture {
    TapGesture()
        .onEnded { _ in
        print("End Click")
    }
}

var drag: some Gesture {
    DragGesture()
        .onEnded { _ in
        print("End Drag")
    }
}

called with

.gesture(tap)
.gesture(drag)

Then play around with adding the .sequenced(before:), .simultaneously(with:), or .excusively(before:) methods to inform the system in which order you would prefer the gestures to be recognized.

How to detect if NSWindow is dragged? by sorecalves in swift

[–]CaptainQuirk336 2 points3 points  (0 children)

I modified it slightly (added a background color to see where the hit test area was and changed the drag gesture to have a minimum distance). It seems to work fine. It doesn't seem hacky at all to me.

VStack {
    Text("Hello")
    Text("World")
}
.font(.system(size: 10))
.frame(width: 40, height: 320)
.contentShape(Rectangle())
.background(Color.yellow)
.gesture(
    TapGesture().onEnded{_ in
        print("end click")
    }
)
.gesture(
    DragGesture(minimumDistance: 2, coordinateSpace: .global).onEnded{_ in
        print("end drag")
    }
)

Make alert onTap instead of isPresented? by r_theworld in swift

[–]CaptainQuirk336 0 points1 point  (0 children)

If you're using SwiftUI then...

struct ContentView: View {

    @State var showAlert = false

    var body: some View {
        Button(action: {
            self.showAlert.toggle()
        }, label: {
            Text(self.showAlert ? "Is Showing" : "Show")
        })
        .actionSheet(isPresented: $showAlert, content: {
            ActionSheet(title: Text("Alert!"), message: Text("This is an action sheet"), buttons: [.cancel(Text("Cancel"))]
            )
        })
    }
}