Skip to content

Commit

Permalink
Public interface
Browse files Browse the repository at this point in the history
  • Loading branch information
eneko committed Jul 8, 2020
1 parent bbf6264 commit a89a90a
Showing 1 changed file with 65 additions and 28 deletions.
93 changes: 65 additions & 28 deletions Sources/Bars/Stripes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,73 +7,110 @@

import SwiftUI

struct Stripes: View {
var background: Color = Color.pink.opacity(0.5)
var foreground: Color = Color.pink.opacity(0.8)
var degrees: Double = 0
var barWidth: CGFloat = 20
var barSpacing: CGFloat = 20

var body: some View {
public struct StripesConfig {
var background: Color
var foreground: Color
var degrees: Double
var barWidth: CGFloat
var barSpacing: CGFloat

public init(background: Color = Color.pink.opacity(0.5), foreground: Color = Color.pink.opacity(0.8),
degrees: Double = 30, barWidth: CGFloat = 20, barSpacing: CGFloat = 20) {
self.background = background
self.foreground = foreground
self.degrees = degrees
self.barWidth = barWidth
self.barSpacing = barSpacing
}

public static let `default` = StripesConfig()
}


public struct Stripes: View {
var config: StripesConfig

public init(config: StripesConfig) {
self.config = config
}

public var body: some View {
GeometryReader { geometry in
let longSide = max(geometry.size.width, geometry.size.height)
let itemWidth = barWidth + barSpacing
let itemWidth = config.barWidth + config.barSpacing
let items = Int(2 * longSide / itemWidth)
HStack(spacing: barSpacing) {
HStack(spacing: config.barSpacing) {
ForEach(0..<items) { index in
foreground
.frame(width: barWidth, height: 2*longSide)
config.foreground
.frame(width: config.barWidth, height: 2 * longSide)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.rotationEffect(Angle(degrees: degrees), anchor: .center)
.rotationEffect(Angle(degrees: config.degrees), anchor: .center)
.offset(x: -longSide / 2, y: -longSide / 2)
.background(background)
.background(config.background)
}
.clipped()
.drawingGroup()
}
}

struct Squares: View {
var degrees: Double = 0
var body: some View {
public struct Squares: View {
var configA: StripesConfig
var configB: StripesConfig

public init(config: StripesConfig) {
configA = config
configB = config
configB.degrees = config.degrees - 90
}

public var body: some View {
ZStack {
Stripes(degrees: degrees)
Stripes(degrees: degrees - 90)
Stripes(config: configA)
Stripes(config: configB)
}
}
}

struct Stripes_Previews: PreviewProvider {
static var previews: some View {
Group {
Stripes().frame(width: 200, height: 200)
Stripes(config: .default).frame(width: 200, height: 200)

Squares().frame(width: 200, height: 200)
Squares(config: .default).frame(width: 200, height: 200)

ZStack {
Stripes(background: Color.blue.opacity(0.2), foreground: Color.blue.opacity(0.4), degrees: 45)
Stripes(background: Color.blue.opacity(0.1), foreground: Color.blue.opacity(0.3), degrees: -45)
Stripes(config: StripesConfig(background: Color.blue.opacity(0.2),
foreground: Color.blue.opacity(0.4), degrees: 45))
Stripes(config: StripesConfig(background: Color.blue.opacity(0.1),
foreground: Color.blue.opacity(0.3), degrees: -45))
}
.frame(width: 414, height: 896, alignment: .center)
.background(Color.black)

ZStack {
Stripes(background: Color.red.opacity(0.2), foreground: Color.blue.opacity(0.6), degrees: 45)
Stripes(background: Color.red.opacity(0.2), foreground: Color.white.opacity(0.3), degrees: -45)
Stripes(config: StripesConfig(background: Color.red.opacity(0.2),
foreground: Color.blue.opacity(0.6),
degrees: 45, barWidth: 50, barSpacing: 20))
Stripes(config: StripesConfig(background: Color.red.opacity(0.2),
foreground: Color.white.opacity(0.15),
degrees: -45, barWidth: 50, barSpacing: 20))
}
.frame(width: 896, height: 414, alignment: .center)
.background(Color.black)

ZStack {
Stripes(background: Color.white.opacity(0.7), foreground: Color.blue.opacity(0.6), degrees: 30)
Stripes(config: StripesConfig(background: Color.clear,
foreground: Color.blue.opacity(0.2), degrees: 56))
}
.frame(width: 896, height: 414, alignment: .center)
.background(Color.black)
.background(Color.white)

ZStack {
Stripes(background: Color.red.opacity(0.6), foreground: Color.white.opacity(0.3), degrees: 0)
Stripes(config: StripesConfig(background: Color.green.opacity(0.6),
foreground: Color.white.opacity(0.3), degrees: 0,
barWidth: 50, barSpacing: 50))
}
.frame(width: 896, height: 414, alignment: .center)
.background(Color.black)
Expand Down

0 comments on commit a89a90a

Please sign in to comment.