Skip to content

Commit

Permalink
Merge branch 'feature/slice_background_image' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-khashimov committed Jul 22, 2020
2 parents 2160a98 + ce074ab commit 80b5ab4
Show file tree
Hide file tree
Showing 132 changed files with 1,965 additions and 332 deletions.
3 changes: 3 additions & 0 deletions Documentation/Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## CHANGELOG

### 1.1.1
- [Issue #8](https://github.com/sh-khashimov/SwiftFortuneWheel/issues/8): Added background image for Slice object;

### 1.1.0

- Added support for **macOS 10.11** and above;
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ The ultimate spinning wheel control that supports dynamic content and rich custo
|---|---|
| 🏵 | Dynamic content, supports texts, images, and lines |
| 🎯 | Adaptive text size with support multiline, alignment and line break mode |
| 🎇 | Supports background Image for each Slice (sector) |
| 🧮 | Supports vertical and horizontal text orientation |
| 🌈 | Appearance customization |
| 🎨 | Drawn and animated using CoreGraphics, CoreAnimations
| 🔋 | High performance, low memory usage |
| 🎨 | Drawn and animated using CoreGraphics, CoreAnimations |
| 🚀 | Written in Swift |

### Layout Preview
Expand Down
12 changes: 12 additions & 0 deletions Sources/SwiftFortuneWheel/Configuration/SFWConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ public extension SFWConfiguration {
/// Stroke color
public var strokeColor: SFWColor

/// Background image content mode
public var backgroundImageContentMode: ContentMode = .scaleAspectFill

/// Initiates a slice preferences
/// - Parameters:
/// - backgroundColorType: Background color type
Expand Down Expand Up @@ -359,3 +362,12 @@ public extension SFWConfiguration {
}
}
}


public extension SFWConfiguration {
/// Content can be drawn by specified mode
enum ContentMode {
case scaleAspectFill
case bottom
}
}
35 changes: 35 additions & 0 deletions Sources/SwiftFortuneWheel/Extensions/CGRect+AspectFill.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// CGRect+AspectFill.swift
// SwiftFortuneWheel
//
// Created by Sherzod Khashimov on 7/22/20.
// Copyright © 2020 SwiftFortuneWheel. All rights reserved.
//

import Foundation

#if os(macOS)
import AppKit
#else
import UIKit
#endif

extension CGSize {

static func aspectFill(aspectRatio :CGSize, minimumSize: CGSize) -> CGSize {

var minimumSize = minimumSize

let mW = minimumSize.width / aspectRatio.width;
let mH = minimumSize.height / aspectRatio.height;

if( mH > mW ) {
minimumSize.width = minimumSize.height / aspectRatio.height * aspectRatio.width;
}
else if( mW > mH ) {
minimumSize.height = minimumSize.width / aspectRatio.width * aspectRatio.height;
}

return minimumSize;
}
}
9 changes: 8 additions & 1 deletion Sources/SwiftFortuneWheel/Models/Slice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,23 @@ public struct Slice {
/// Contents in vertical align order
public var contents: [ContentType]


/// Background color, `optional`
public var backgroundColor: SFWColor?

/// Background image, `optional`
public var backgroundImage: SFWImage?

/// Initiates a slice object
/// - Parameter contents: Contents in vertical align order
/// - Parameter backgroundColor: Background color, `optional`
/// - Parameter backgroundImage: Background image, `optional`
public init(contents: [ContentType],
backgroundColor: SFWColor? = nil) {
backgroundColor: SFWColor? = nil,
backgroundImage: SFWImage? = nil) {
self.contents = contents
self.backgroundColor = backgroundColor
self.backgroundImage = backgroundImage
}
}

Expand Down
40 changes: 33 additions & 7 deletions Sources/SwiftFortuneWheel/Utils/Drawing/SliceDrawing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ extension SliceDrawing {
// Draws slice path and background
self.drawPath(in: context,
backgroundColor: slice.backgroundColor,
backgroundImage: slice.backgroundImage,
start: start,
and: end,
rotation: rotation,
Expand Down Expand Up @@ -125,7 +126,7 @@ extension SliceDrawing {
/// - end: end degree
/// - rotation: rotation degree
/// - index: index
private func drawPath(in context: CGContext, backgroundColor: SFWColor?, start: CGFloat, and end: CGFloat, rotation:CGFloat, index: Int) {
private func drawPath(in context: CGContext, backgroundColor: SFWColor?, backgroundImage: SFWImage?, start: CGFloat, and end: CGFloat, rotation:CGFloat, index: Int) {

context.saveGState()
context.rotate(by: (rotation + contextPositionCorrectionOffsetDegree) * CGFloat.pi/180)
Expand Down Expand Up @@ -155,12 +156,14 @@ extension SliceDrawing {
context.addPath(path)
context.drawPath(using: .fill)

// let path = UIBezierPath()
// let center = CGPoint(x: 0, y: 0)
// path.move(to: center)
// path.addArc(withCenter: center, radius: radius, startAngle: Calc.torad(start), endAngle: Calc.torad(end), clockwise: true)
// pathBackgroundColor?.setFill()
// path.fill()
if let backgroundImage = backgroundImage {
context.saveGState()
context.addPath(path)
context.clip()
context.rotate(by: -contextPositionCorrectionOffsetDegree * CGFloat.pi/180)
self.draw(backgroundImage: backgroundImage, in: context)
context.restoreGState()
}

if rotation != end {
let startPoint = CGPoint(x: (radius * (cos((end)*(CGFloat.pi/180)))), y: (radius * (sin((start)*(CGFloat.pi/180)))))
Expand All @@ -184,6 +187,29 @@ extension SliceDrawing {
context.restoreGState()
}

/// Draws background image for slice
/// - Parameters:
/// - backgroundImage: Background Image
/// - context: Context
private func draw(backgroundImage: SFWImage, in context: CGContext) {

let aspectFillSize = CGSize.aspectFill(aspectRatio: backgroundImage.size, minimumSize: CGSize(width: radius, height: circularSegmentHeight))

let position = CGPoint(x: -aspectFillSize.width / 2, y: -aspectFillSize.height)
let rectangle = CGRect(x: position.x, y: position.y, width: aspectFillSize.width, height: aspectFillSize.height)

switch preferences?.slicePreferences.backgroundImageContentMode {
case .some(.bottom):
#if os(macOS)
backgroundImage.draw(at: position, from: NSRect(x: 0, y: 0, width: rectangle.width, height: rectangle.height), operation: .copy, fraction: 1)
#else
backgroundImage.draw(at: position)
#endif
default:
backgroundImage.draw(in: rectangle)
}
}

/// Prepare to draw text
/// - Parameters:
/// - text: text
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftFortuneWheel/macOSPort.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ extension NSImage {

return image
}

func draw(at position: NSPoint) {
self.draw(at: position, from: NSRect(x: 0, y: 0, width: self.size.width, height: self.size.height), operation: .copy, fraction: 1)
}
}
#endif

Expand Down
4 changes: 4 additions & 0 deletions SwiftFortuneWheel.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
2722559924BCA4A400950563 /* NSView+AnchorPoint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2722559824BCA4A400950563 /* NSView+AnchorPoint.swift */; };
2722559B24BCD7F400950563 /* NoClippingLayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2722559A24BCD7F300950563 /* NoClippingLayer.swift */; };
277A6EBE249A9B450066C807 /* LinePreferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = 277A6EBD249A9B450066C807 /* LinePreferences.swift */; };
277C4C2B24C85D25007580F4 /* CGRect+AspectFill.swift in Sources */ = {isa = PBXBuildFile; fileRef = 277C4C2A24C85D25007580F4 /* CGRect+AspectFill.swift */; };
2787E56E24A728F900533AD4 /* String+Width.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2787E56D24A728F900533AD4 /* String+Width.swift */; };
2787E57024A7292F00533AD4 /* String+Lines.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2787E56F24A7292F00533AD4 /* String+Lines.swift */; };
27F4807924AEE406005886F6 /* TextDrawing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27F4807824AEE406005886F6 /* TextDrawing.swift */; };
Expand Down Expand Up @@ -83,6 +84,7 @@
2722559A24BCD7F300950563 /* NoClippingLayer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoClippingLayer.swift; sourceTree = "<group>"; };
272B62C524A46FED00F19E41 /* MyPlayground.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = MyPlayground.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
277A6EBD249A9B450066C807 /* LinePreferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinePreferences.swift; sourceTree = "<group>"; };
277C4C2A24C85D25007580F4 /* CGRect+AspectFill.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CGRect+AspectFill.swift"; sourceTree = "<group>"; };
2787E56D24A728F900533AD4 /* String+Width.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Width.swift"; sourceTree = "<group>"; };
2787E56F24A7292F00533AD4 /* String+Lines.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Lines.swift"; sourceTree = "<group>"; };
27F4807824AEE406005886F6 /* TextDrawing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextDrawing.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -202,6 +204,7 @@
27105668248D4C93006C0181 /* CGRest+AspectFit.swift */,
27105669248D4C93006C0181 /* Array+Default.swift */,
2722559824BCA4A400950563 /* NSView+AnchorPoint.swift */,
277C4C2A24C85D25007580F4 /* CGRect+AspectFill.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -415,6 +418,7 @@
277A6EBE249A9B450066C807 /* LinePreferences.swift in Sources */,
27105681248D4C93006C0181 /* SwiftFortuneWheel.swift in Sources */,
27105671248D4C93006C0181 /* ImagePreferences.swift in Sources */,
277C4C2B24C85D25007580F4 /* CGRect+AspectFill.swift in Sources */,
27105675248D4C93006C0181 /* SpinningAnimatable.swift in Sources */,
27F7D5BF24A5F6B0004BEE19 /* String+Dots.swift in Sources */,
27105684248D4C93006C0181 /* WheelLayer.swift in Sources */,
Expand Down
7 changes: 5 additions & 2 deletions docs/Classes.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<a title="Classes Reference"></a>
<header>
<div class="content-wrapper">
<p><a href="index.html">SwiftFortuneWheel 1.1.0 Docs</a> (80% documented)</p>
<p><a href="index.html">SwiftFortuneWheel 1.1.0 Docs</a> (79% documented)</p>
</div>
</header>
<div class="content-wrapper">
Expand Down Expand Up @@ -194,6 +194,9 @@
<li class="nav-group-task">
<a href="Structs/SFWConfiguration/AnchorImage.html">– AnchorImage</a>
</li>
<li class="nav-group-task">
<a href="Structs/SFWConfiguration/ContentMode.html">– ContentMode</a>
</li>
<li class="nav-group-task">
<a href="Structs/Slice.html">Slice</a>
</li>
Expand Down Expand Up @@ -508,7 +511,7 @@ <h4>Declaration</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2020 <a class="link" href="https://github.com/sh-khashimov/SwiftFortuneWheel.git" target="_blank" rel="external">Sherzod Khashimov</a>. All rights reserved. (Last updated: 2020-07-17)</p>
<p>&copy; 2020 <a class="link" href="https://github.com/sh-khashimov/SwiftFortuneWheel.git" target="_blank" rel="external">Sherzod Khashimov</a>. All rights reserved. (Last updated: 2020-07-22)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.13.4</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
Expand Down
7 changes: 5 additions & 2 deletions docs/Classes/NoClippingLayer.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<a title="NoClippingLayer Class Reference"></a>
<header>
<div class="content-wrapper">
<p><a href="../index.html">SwiftFortuneWheel 1.1.0 Docs</a> (80% documented)</p>
<p><a href="../index.html">SwiftFortuneWheel 1.1.0 Docs</a> (79% documented)</p>
</div>
</header>
<div class="content-wrapper">
Expand Down Expand Up @@ -194,6 +194,9 @@
<li class="nav-group-task">
<a href="../Structs/SFWConfiguration/AnchorImage.html">– AnchorImage</a>
</li>
<li class="nav-group-task">
<a href="../Structs/SFWConfiguration/ContentMode.html">– ContentMode</a>
</li>
<li class="nav-group-task">
<a href="../Structs/Slice.html">Slice</a>
</li>
Expand Down Expand Up @@ -311,7 +314,7 @@ <h4>Declaration</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2020 <a class="link" href="https://github.com/sh-khashimov/SwiftFortuneWheel.git" target="_blank" rel="external">Sherzod Khashimov</a>. All rights reserved. (Last updated: 2020-07-17)</p>
<p>&copy; 2020 <a class="link" href="https://github.com/sh-khashimov/SwiftFortuneWheel.git" target="_blank" rel="external">Sherzod Khashimov</a>. All rights reserved. (Last updated: 2020-07-22)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.13.4</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
Expand Down
7 changes: 5 additions & 2 deletions docs/Classes/PinImageView.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<a title="PinImageView Class Reference"></a>
<header>
<div class="content-wrapper">
<p><a href="../index.html">SwiftFortuneWheel 1.1.0 Docs</a> (80% documented)</p>
<p><a href="../index.html">SwiftFortuneWheel 1.1.0 Docs</a> (79% documented)</p>
</div>
</header>
<div class="content-wrapper">
Expand Down Expand Up @@ -194,6 +194,9 @@
<li class="nav-group-task">
<a href="../Structs/SFWConfiguration/AnchorImage.html">– AnchorImage</a>
</li>
<li class="nav-group-task">
<a href="../Structs/SFWConfiguration/ContentMode.html">– ContentMode</a>
</li>
<li class="nav-group-task">
<a href="../Structs/Slice.html">Slice</a>
</li>
Expand Down Expand Up @@ -422,7 +425,7 @@ <h4>Parameters</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2020 <a class="link" href="https://github.com/sh-khashimov/SwiftFortuneWheel.git" target="_blank" rel="external">Sherzod Khashimov</a>. All rights reserved. (Last updated: 2020-07-17)</p>
<p>&copy; 2020 <a class="link" href="https://github.com/sh-khashimov/SwiftFortuneWheel.git" target="_blank" rel="external">Sherzod Khashimov</a>. All rights reserved. (Last updated: 2020-07-22)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.13.4</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
Expand Down
7 changes: 5 additions & 2 deletions docs/Classes/SpinButton.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<a title="SpinButton Class Reference"></a>
<header>
<div class="content-wrapper">
<p><a href="../index.html">SwiftFortuneWheel 1.1.0 Docs</a> (80% documented)</p>
<p><a href="../index.html">SwiftFortuneWheel 1.1.0 Docs</a> (79% documented)</p>
</div>
</header>
<div class="content-wrapper">
Expand Down Expand Up @@ -194,6 +194,9 @@
<li class="nav-group-task">
<a href="../Structs/SFWConfiguration/AnchorImage.html">– AnchorImage</a>
</li>
<li class="nav-group-task">
<a href="../Structs/SFWConfiguration/ContentMode.html">– ContentMode</a>
</li>
<li class="nav-group-task">
<a href="../Structs/Slice.html">Slice</a>
</li>
Expand Down Expand Up @@ -505,7 +508,7 @@ <h4>Parameters</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2020 <a class="link" href="https://github.com/sh-khashimov/SwiftFortuneWheel.git" target="_blank" rel="external">Sherzod Khashimov</a>. All rights reserved. (Last updated: 2020-07-17)</p>
<p>&copy; 2020 <a class="link" href="https://github.com/sh-khashimov/SwiftFortuneWheel.git" target="_blank" rel="external">Sherzod Khashimov</a>. All rights reserved. (Last updated: 2020-07-22)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.13.4</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
Expand Down
Loading

0 comments on commit 80b5ab4

Please sign in to comment.