Skip to content

Commit

Permalink
Remove .drawingGroup()
Browse files Browse the repository at this point in the history
  • Loading branch information
eneko committed Jan 24, 2021
1 parent fc1112d commit d533fd4
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 11 deletions.
10 changes: 7 additions & 3 deletions Demo/StripesDemo/Shared/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import Stripes
struct ContentView: View {
var body: some View {
ZStack {
Stripes(config: StripesConfig(background: Color.green.opacity(0.6),
foreground: Color.white.opacity(0.3), degrees: 0,
barWidth: 50, barSpacing: 50))
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))

Text("Hello, world!")
.font(.system(size: 50))
.foregroundColor(.white)
Expand Down
4 changes: 2 additions & 2 deletions Demo/StripesDemo/StripesDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.16;
MACOSX_DEPLOYMENT_TARGET = 11.0;
PRODUCT_BUNDLE_IDENTIFIER = com.enekoalonso.apps.StripesDemo;
PRODUCT_NAME = StripesDemo;
SDKROOT = macosx;
Expand All @@ -421,7 +421,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.16;
MACOSX_DEPLOYMENT_TARGET = 11.0;
PRODUCT_BUNDLE_IDENTIFIER = com.enekoalonso.apps.StripesDemo;
PRODUCT_NAME = StripesDemo;
SDKROOT = macosx;
Expand Down
113 changes: 108 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,61 @@

Beautiful background pattern views for SwiftUI.

## Example Patterns

### Diagonal Bars

![Stripes SwiftUI](Documentation/stripes-swiftui-macos.png)

## Example Patterns
```swift
import SwiftUI
import Stripes

struct ContentView: View {
var body: some View {
ZStack {
Stripes(config: .default)

Text("Hello, world!")
.font(.system(size: 50))
.foregroundColor(.white)
.bold()
}
.background(Color.black)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.edgesIgnoringSafeArea(.all)
}
}
```

### Vertical Bars
![Stripes SwiftUI](Documentation/stripes-swiftui-ipad.png)

### Overlapping Patterns
![Stripes SwiftUI](Documentation/stripes-swiftui-iphone.png)
```swift
import SwiftUI
import Stripes

struct ContentView: View {
var body: some View {
ZStack {
Stripes(config: StripesConfig(background: Color.green.opacity(0.6),
foreground: Color.white.opacity(0.3), degrees: 0,
barWidth: 50, barSpacing: 50))

Text("Hello, world!")
.font(.system(size: 50))
.foregroundColor(.white)
.bold()
}
.background(Color.black)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.edgesIgnoringSafeArea(.all)
}
}
```

## Usage
### Overlapping Patterns
![Stripes SwiftUI](Documentation/stripes-swiftui-iphone.png)

```swift
import SwiftUI
Expand All @@ -22,16 +65,26 @@ import Stripes
struct ContentView: View {
var body: some View {
ZStack {
Stripes(config: .default)
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))

Text("Hello, world!")
.font(.system(size: 50))
.foregroundColor(.white)
.bold()
}
.background(Color.black)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.edgesIgnoringSafeArea(.all)
}
}
```


## Installation

### Preferred: Add package to Xcode project
Expand All @@ -46,3 +99,53 @@ struct ContentView: View {

![Stripes Install](Documentation/stripes-install-2.png)

### Avoid dependencies, copy the code

```swift
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 = config.barWidth + config.barSpacing
let items = Int(2 * longSide / itemWidth)
HStack(spacing: config.barSpacing) {
ForEach(0..<items) { index in
config.foreground
.frame(width: config.barWidth, height: 2 * longSide)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.rotationEffect(Angle(degrees: config.degrees), anchor: .center)
.offset(x: -longSide / 2, y: -longSide / 2)
.background(config.background)
}
.clipped()
}
}
```
1 change: 0 additions & 1 deletion Sources/Stripes/Stripes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public struct Stripes: View {
.background(config.background)
}
.clipped()
.drawingGroup()
}
}

Expand Down

0 comments on commit d533fd4

Please sign in to comment.