SwiftFortuneWheel Documentation

repository·master·Indexed 19 days ago

https://github.com/sh-khashimov/swiftfortunewheel

A high-performance spinning wheel control for iOS, macOS, and tvOS. It features dynamic content support (text, images, lines), rich slice customization, collision effects, and haptic/sound feedback. The library provides a comprehensive Rotation API for controlling animations and an SFWConfiguration system for managing visual preferences of the wheel, pin indicator, and spin button.

Tokens
6.3K
Snippets
17
Records
32
Agent score
63%

What's inside SwiftFortuneWheel

  1. How SFWConfiguration.ColorType works

    master

    The colorType property (of type SFWConfiguration.ColorType) allows you to define how colors are applied to slices.

    • Even/Odd Pattern: Use evenOddColors(evenColor:oddColor:) to alternate between two colors.
    • Solid Color: To use a single solid color, use the customPatternColors(colors:defaultColor:) case, passing nil for the colors parameter and your desired color for defaultColor.
    // Example of setting a solid color
    let mySolidColor = UIColor.red
    let colorType = SFWConfiguration.ColorType.customPatternColors(colors: nil, defaultColor: mySolidColor)
  2. Understand the SwiftFortuneWheel component hierarchy

    master

    The visual structure of the wheel is composed of several layers and configurable components:

    1. WheelView: The main container (configured via SFWConfiguration.WheelPreferences).
    2. SFWConfiguration.AnchorImage: An optional image anchored to the wheel.
    3. SpinButton: The trigger button (configured via SFWConfiguration.SpinButtonPreferences).
    4. PinImageView: The indicator/pin (configured via SFWConfiguration.PinImageViewPreferences).
    5. Slice: Individual segments (configured via SlicePreferences).
    6. Slice.ContentType: The actual content inside a slice:
      • Slice.ContentType.image or assetImage (configured via ImagePreferences).
      • Slice.ContentType.text (configured via TextPreferences).
  3. Populate the wheel using Slice and Slice contents

    master

    To add content to the SwiftFortuneWheel, you must create a list of Slice objects and assign it to the wheel instance. Each Slice represents a segment of the wheel and can contain multiple content items and an optional backgroundColor.

    Content items are rendered in the order they are added to the list (from top to bottom). Each content item allows for individual customization, such as vertical/horizontal offsets, size, and font.

  4. Configure center collision sound effects and haptics

    master

    You can play a sound whenever the center of a slice moves during rotation. To enable this, you must provide an AudioFile to the centerCollisionSound property and set centerCollisionDetectionOn to true. You can also optionally enable haptic feedback using impactFeedbackOn (available on iOS 10+ only).

    @IBOutlet weak var fortuneWheel: SwiftFortuneWheel!
    // after SwiftFortuneWheel init and configuration…
    
    // add Click.mp3 to your project, create AudioFile, and set to centerCollisionSound
    fortuneWheel.centerCollisionSound = AudioFile(filename: "Click", extensionName: "mp3")
    
    // optionally, turn on the haptic feedback for each impact
    fortuneWheel.impactFeedbackOn = true
    
    // turn on the center collision detection
    fortuneWheel.centerCollisionDetectionOn = true
  5. Migrate from version 0.x.x to 1.0.0

    master

    When upgrading from any 0.x.x version to 1.0.0, apply the following breaking changes:

    • Configuration Renaming: The configuration class SwiftFortuneWheelConfiguration has been renamed to SFWConfiguration.
    • Optional Preferences: Inside SFWConfiguration, the properties pinPreferences and spinButtonPreferences are now optional.
    • Wheel Initialization: When initializing WheelPreferences, you must now explicitly specify the startPosition property.
    • Slice Content Type Changes:
      • The Slice.ContentType case image(String, ImagePreferences) has been renamed to assetImage(String, ImagePreferences).
      • A new case image(UIImage, ImagePreferences) has been added to Slice.ContentType to support direct UIImage usage.
  6. Configure edge collision sound effects and haptics

    master

    You can play a sound whenever the edge of a slice moves during rotation. To enable this, you must provide an AudioFile to the edgeCollisionSound property and set edgeCollisionDetectionOn to true. You can also optionally enable haptic feedback using impactFeedbackOn (available on iOS 10+ only).

    @IBOutlet weak var fortuneWheel: SwiftFortuneWheel!
    // after SwiftFortuneWheel init and configuration…
    
    // add Click.mp3 to your project, create AudioFile, and set to edgeCollisionSound
    fortuneWheel.edgeCollisionSound = AudioFile(filename: "Click", extensionName: "mp3")
    
    // optionally, turn on the haptic feedback for each impact
    fortuneWheel.impactFeedbackOn = true
    
    // turn on the edge collision detection
    fortuneWheel.edgeCollisionDetectionOn = true
  7. Integrate SwiftFortuneWheel into your project

    master

    To use SwiftFortuneWheel, import the module and add a SwiftFortuneWheel view to your ViewController via Storyboard or XIB.

    Storyboard Setup

    1. Add a UIView to your ViewController's view hierarchy.
    2. In the Identity Inspector, change the class and module to SwiftFortuneWheel.
    3. Connect the view to your ViewController using an @IBOutlet.

    Important: Ensure your SwiftFortuneWheel view maintains a 1:1 aspect ratio to render correctly.

    import SwiftFortuneWheel
    
    /// Fortune Wheel
    @IBOutlet weak var fortuneWheel: SwiftFortuneWheel!
  8. Install SwiftFortuneWheel via Swift Package Manager

    master

    If you are using Xcode 11 or higher, you can add SwiftFortuneWheel as a dependency directly through Xcode's built-in Swift Package Manager.

    1. Go to File / Swift Packages / Add Package Dependency...
    2. Enter the repository URL: https://github.com/sh-khashimov/SwiftFortuneWheel.git
    3. Follow the Xcode instructions to complete the installation.

    To remove the dependency, select your project in the navigator, open the Swift Packages tab (located next to Build Settings), and remove the package from the list.

    https://github.com/sh-khashimov/SwiftFortuneWheel.git
  9. Migrate from 1.0.x to 1.1.x: Update preference initializations

    master

    When upgrading from version 1.0.x to 1.1.x, the initialization process for several preference objects has changed. You can no longer pass all configuration parameters during the initial call. Instead, you must initialize the object with a specific subset of parameters and then update any remaining properties after initialization.

    To fix errors caused by this change, update your code according to the following requirements for version 1.1.x:

    • TextPreferences: Initialize only with textColorType, font, and verticalOffset. Update other parameters post-initialization.
    • ImagePreferences: Initialize only with preferedSize and verticalOffset. Update other parameters post-initialization.
    • WheelPreferences: Initialize only with circlePreferences, slicePreferences, and startPosition. Update other parameters post-initialization.
    • SpinButtonPreferences: Initialize only with size, horizontalOffset, and verticalOffset. Update other parameters post-initialization.
    • PinImageViewPreferences: Initialize only with size, position, horizontalOffset, and verticalOffset. Update other parameters post-initialization.
    • AnchorImage: Initialize only with size, imageName, and verticalOffset. Update other parameters post-initialization.