Auto CarouselView for SwiftUI.
- iOS 13.0+
- Swift 5.3+
Open Xcode
, go to File -> Swift Packages -> Add Package Dependency
and enter https://github.com/y-okudera/AutoCarouselView
-
Basic
AutoCarouselView parameters have default values. You can simply pass in the views and the current page.
import AutoCarouselView
import SwiftUI
struct ContentView: View {
@State private var currentPage = 0
var body: some View {
GeometryReader { geometry in
AutoCarouselView(
[scaledImage(UIImage(named: "fox")!),
scaledImage(UIImage(named: "iceland")!),
scaledImage(UIImage(named: "tree")!)
], currentPage: $currentPage
)
.frame(width: UIScreen.main.bounds.width, height: geometry.size.height / 3)
}
}
func scaledImage(_ image: UIImage) -> some View {
GeometryReader { geometry in
Image(uiImage: image)
.resizable()
.scaledToFill()
.frame(width: geometry.size.width, height: geometry.size.height)
.clipped()
}
}
}
-
Customize
You can set the appearance of the PageControl by specifying parameters in the AutoCarouselView initializer.
import AutoCarouselView
import SwiftUI
struct ContentView: View {
@State private var currentPage = 0
var body: some View {
GeometryReader { geometry in
AutoCarouselView(
[scaledImage(UIImage(named: "fox")!),
scaledImage(UIImage(named: "iceland")!),
scaledImage(UIImage(named: "tree")!)],
currentPage: $currentPage,
interval: 3.0,
isHiddenPageDot: false,
currentPageDotSize: .init(width: 10, height: 10),
currentPageDotFillColor: .red,
currentPageDotStrokeColor: .blue,
otherPageDotSize: .init(width: 7, height: 7),
otherPageDotFillColor: .gray,
otherPageDotStrokeColor: .white)
.frame(width: UIScreen.main.bounds.width, height: geometry.size.height / 3)
}
}
func scaledImage(_ image: UIImage) -> some View {
GeometryReader { geometry in
Image(uiImage: image)
.resizable()
.scaledToFill()
.frame(width: geometry.size.width, height: geometry.size.height)
.clipped()
}
}
}
See Demo project.
Yuki Okudera(@y-okudera), E-Mail
Feel free to dive in! Open an issue or submit PRs.
AutoCarouselView is available under the MIT license. See the LICENSE file for more info.