3

advanced-swiftui-animations.swift

 1 year ago
source link: https://gist.github.com/swiftui-lab/e5901123101ffad6d39020cc7a810798
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

advanced-swiftui-animations.swift · GitHub

Instantly share code, notes, and snippets.

Truly excellent stuff here. One fix, for the colorMixer functions, the number of components from a CGColor depends on the color space. If you pass in a gray UIColor, like .white or .black for example, as a CGColor it only has two components because it's not in an RGB color space and you get an out of bounds exemption trying to access cc1[2]. Something like this will fix it:

let rgbSpace = CGColorSpaceCreateDeviceRGB()
guard let cc1 = c1.cgColor.converted(to: rgbSpace, intent: .defaultIntent, options: nil)?.components else { return Color(c1) }

Just a note, I have seen that the position of the counter border & background are incorrect when following the example above. The following resolves the issue.

struct CounterBorder: View  {

        @Binding var height: CGFloat

        var body: some View {
            GeometryReader { proxy in
                RoundedRectangle(cornerRadius: 5.0).stroke(lineWidth: 5).foregroundColor(Color.blue).frame(width: 80, height: proxy.size.height / 5.0 + 30)
                    .position(x: proxy.size.width / 2, y: proxy.size.height / 2)
            }
        }
    }

    struct CounterBackground: View {

        @Binding var height: CGFloat

        var body: some View {
            GeometryReader { proxy in
                RoundedRectangle(cornerRadius: 5.0).fill(Color.black).frame(width: 80, height: proxy.size.height / 5.0 + 30)
                    .position(x: proxy.size.width / 2, y: proxy.size.height / 2)
            }
        }
    }

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK