Skip to content

GGJJack/SwiftUIWheelPicker

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
February 11, 2022 10:47
img
February 11, 2022 10:47
September 16, 2021 16:39
September 16, 2021 16:39
January 26, 2022 09:33
September 16, 2021 16:39

SwiftUIWheelPicker

Version License Platform

Horizontal wheel picker for SwiftUI

Requirements

iOS 13.0+

Installation

CocoaPods

pod 'SwiftUIWheelPicker'

Import

import SwiftUIWheelPicker

Getting Started

Basic use

Basic Use

@State var indexBasic: Int = 5
@State var items: [Int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

...

SwiftUIWheelPicker($indexBasic, items: $items) { value in
    GeometryReader { reader in
        Text("\(value)")
            .frame(width: reader.size.width, height: reader.size.height, alignment: .center)
    }
}
.frame(width: geometry.size.width - 40, height: 40, alignment: .center)
.padding(.top, 10)

With Visible Count

With Visible Count

SwiftUIWheelPicker($indexRatio, items: $items) { value in
    GeometryReader { reader in
        Text("\(value)")
            .frame(width: reader.size.width, height: reader.size.height, alignment: .center)
    }
}
.width(.Ratio(0.1))
public enum WidthOption {
    case VisibleCount(Int) // How many items to show on the display
    case Fixed(CGFloat) // to a fixed width
    case Ratio(CGFloat) // From the width of the view to the ratio
}

With Alpha

With Alpha

SwiftUIWheelPicker($indexAlpha, items: $items) { value in
    GeometryReader { reader in
        Text("\(value)")
            .frame(width: reader.size.width, height: reader.size.height, alignment: .center)
    }
}
.scrollAlpha(0.1)

With Scale

With Scale

SwiftUIWheelPicker($indexScale, items: $items) { value in
    GeometryReader { reader in
        Text("\(value)")
            .frame(width: reader.size.width, height: reader.size.height, alignment: .center)
    }
}
.scrollScale(0.4)

With Gradient

With Scale

SwiftUIWheelPicker($indexGradient, items: $items) { value in
    GeometryReader { reader in
        Text("\(value)")
            .frame(width: reader.size.width, height: reader.size.height, alignment: .center)
    }
}
.edgeLeft(AnyView(
    LinearGradient(gradient: Gradient(colors: [Color.gray.opacity(0.4), Color.white.opacity(0)]), startPoint: .leading, endPoint: .trailing)
), width: .Ratio(0.2))
.edgeRight(AnyView(
    LinearGradient(gradient: Gradient(colors: [Color.white.opacity(0), Color.gray.opacity(0.4)]), startPoint: .leading, endPoint: .trailing)
), width: .Ratio(0.2))

Center indicator

Center indicator

SwiftUIWheelPicker($indexIndicator, items: $items) { value in
    GeometryReader { reader in
        Text("\(value)")
            .frame(width: reader.size.width, height: reader.size.height, alignment: .center)
    }
}
.centerView(AnyView(
    HStack(alignment: .center, spacing: 0) {
        Divider()
            .frame(width: 1)
            .background(Color.gray)
            .padding(EdgeInsets(top: 6, leading: 0, bottom: 6, trailing: 0))
            .opacity(0.4)
        Spacer()
        Divider()
            .frame(width: 1)
            .background(Color.gray)
            .padding(EdgeInsets(top: 6, leading: 0, bottom: 6, trailing: 0))
            .opacity(0.4)
    }
), width: .Fixed(40))

Selected Value

Selected Value

@State var indexValue: Int = 5
@State var items: [Int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Text("Selected Value : \(indexValue)").font(Font.title)
SwiftUIWheelPicker($indexValue, items: $items) { value in
    GeometryReader { reader in
        Text("\(value)")
            .frame(width: reader.size.width, height: reader.size.height, alignment: .center)
            .background(value % 2 == 0 ? Color.blue.opacity(0.4) : Color.green.opacity(0.4))
    }
}

Scroll To

Scroll To

HStack(alignment: .bottom, spacing: 20) {
    Text("Scroll To ").font(Font.title)
    Spacer()
    Button("First") {
        indexScrollTo = 0
    }
    Button("Center") {
        indexScrollTo = items.count / 2
    }
    Button("Last") {
        indexScrollTo = items.count - 1
    }
}
SwiftUIWheelPicker($indexScrollTo, items: $items) { value in
    GeometryReader { reader in
        Text("\(value)")
            .frame(width: reader.size.width, height: reader.size.height, alignment: .center)
            .background(value % 2 == 0 ? Color.blue.opacity(0.4) : Color.green.opacity(0.4))
    }
}

Author

ggaljjak, ggaljjak.choi@gmail.com

License

SwiftUIWheelPicker is available under the MIT license. See the LICENSE file for more info.