• Stars
    star
    143
  • Rank 248,678 (Top 6 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 8 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Lightweight, written natively in Swift, circular UIScrollView.

SwiftCarousel

No Maintenance Intended Version Carthage Compatible License Platform Twitter

SwiftCarousel is a lightweight, written natively in Swift, circular UIScrollView.
So what is there more to that than just a circular scroll view? You can spin it like a real carousel!

SwiftCarousel example SwiftCarousel example

Requirements

Swift 2.0, iOS 9

Installation

SwiftCarousel is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "SwiftCarousel"

Then run pod install and it should be πŸ”₯ Also remember to add import SwiftCarousel in your project.

Examples

You can use Examples directory for examples with creating SwiftCarousel using IB or code.

Basic usage using Interface Builder (Storyboard/xibs)

First, create UIView object and assign SwiftCarousel class to it. Then we need to assign some selectable UIViews. It might be UILabels, UIImageViews etc. The last step would be setting correct resizeType parameter which contains:

public enum SwiftCarouselResizeType {
    // WithoutResizing is adding frames as they are.
    // Parameter = spacing between UIViews.
    // !!You need to pass correct frame sizes as items!!
    case WithoutResizing(CGFloat)

    // VisibleItemsPerPage will try to fit the number of items you specify
    // in the whole screen (will resize them of course).
    // Parameter = number of items visible on screen.
    case VisibleItemsPerPage(Int)

    // FloatWithSpacing will use sizeToFit() on your views to correctly place images
    // It is helpful for instance with UILabels (Example1 in Examples folder).
    // Parameter = spacing between UIViews.
    case FloatWithSpacing(CGFloat)
}

Basic setup would look like:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    items = ["Elephants", "Tigers", "Chickens", "Owls", "Rats", "Parrots", "Snakes"]
    itemsViews = items!.map { labelForString($0) }
    carousel.items = itemsViews!
    carousel.resizeType = .VisibleItemsPerPage(3)
    carousel.defaultSelectedIndex = 3 // Select default item at start
    carousel.delegate = self
}

func labelForString(string: String) -> UILabel {
    let text = UILabel()
    text.text = string
    text.textColor = .blackColor()
    text.textAlignment = .Center
    text.font = .systemFontOfSize(24.0)
    text.numberOfLines = 0

    return text
}

Basic usage using pure code

Here we use itemsFactory(itemsCount:facory:) method. This method allows you to setup your carousel using closure rather than static array of views. Why would we want to use that? In case of quite complicated logic. E.g. if you want to have CALayer properties all across the carousel.

let carouselFrame = CGRect(x: view.center.x - 200.0, y: view.center.y - 100.0, width: 400.0, height: 200.0)
carouselView = SwiftCarousel(frame: carouselFrame)
try! carouselView.itemsFactory(itemsCount: 5) { choice in
    let imageView = UIImageView(image: UIImage(named: "puppy\(choice+1)"))
    imageView.frame = CGRect(origin: CGPointZero, size: CGSize(width: 200.0, height: 200.0))

    return imageView
}
carouselView.resizeType = .WithoutResizing(10.0)
carouselView.delegate = self
carouselView.defaultSelectedIndex = 2
view.addSubview(carouselView)

Additional methods, properties & delegate

You can use method selectItem(_:animated:) to programmatically select your item:

carousel.selectItem(1, animated: true)

Or you can set default selected item:

carousel.defaultSelectedIndex = 3

You can disable selecting item by tapping it (its enabled by default):

carousel.selectByTapEnabled = false

You can also get current selected index:

let selectedIndex = carousel.selectedIndex

You can implement SwiftCarouselDelegate protocol:

@objc public protocol SwiftCarouselDelegate {
    optional func didSelectItem(item item: UIView, index: Int, tapped: Bool) -> UIView?
    optional func didDeselectItem(item item: UIView, index: Int) -> UIView?
    optional func didScroll(toOffset offset: CGPoint) -> Void
    optional func willBeginDragging(withOffset offset: CGPoint) -> Void
    optional func didEndDragging(withOffset offset: CGPoint) -> Void
}

Then you need to set the delegate property:

carousel.delegate = self

If you need more, basic usages in Example1 project in directory Examples.

Known limitations

The original views are internally copied to using the copyView method defined in the UIView+SwiftCarousel extension when using the items property. This performs a shallow copy of the view using NSKeyedUnarchiver and NSKeyedArchiver. So, if a custom UIView subclass with references to external objects is used, those references might be nil when didSelectItem and didDeselectItem delegate methods are called. To avoid this situation, the itemsFactory method can be used instead of the items property to setup the carousel.

Contributing

Feel free to make issues/pull requests if you have some questions, ideas for improvement, or maybe bugs you've found. After some contribution I'm giving write access as a thank you πŸŽ‰

Author

Sunshinejr, [email protected], @thesunshinejr

License

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

More Repositories

1

MPParallaxView

Apple TV Parallax effect in Swift.
Swift
1,742
star
2

RxSwiftExamples

Examples and resources for RxSwift.
Swift
966
star
3

flutter-roadmap

Roadmap for Flutter developers in 2020
Dart
432
star
4

jspoon

Annotation based HTML to Java parser + Retrofit converter
Java
319
star
5

Workcation

My Research & Development project I've done at Droids on Roids.
Java
267
star
6

VisionFaceDetection

An example of use a Vision framework for face landmarks detection in iOS 11
Swift
260
star
7

MeasureARKit

An example of measuring app with ARKit in iOS 11
Swift
228
star
8

Casty

Small Android library that provides a simple media player for Chromecast, fully consistent with Google Cast v3
Java
101
star
9

FlipAnimation-Android

Java
94
star
10

ExampleRealmMVP-Android

Java
66
star
11

GlideSharedTransition

Sample app for Shared Element Transition with Glide
Kotlin
46
star
12

FaceTracking

An example app with AVFoundation camera support and face features tracking
Swift
42
star
13

PageTransformerDemo

Simple example of screen transitions with ViewPager and PageTransformer.
Java
36
star
14

PhotosHelper

Photos Framework in Swift, easiest way.
Swift
35
star
15

let-swift-app

Let Swift Developers Meetup app
Swift
34
star
16

Toast-App

Open source App for TOAST Android developers meetup
Kotlin
34
star
17

android-device-cleaner

Tool for cleaning up Android devices used in QA
Kotlin
27
star
18

android-animation-disabler

Gradle plugin for disabling animations in global settings before UI tests and reenabling them afterwards
Kotlin
27
star
19

SwiftCompressor

SwiftCompressor lets you use Compression framework easily
Shell
24
star
20

RxJavaProductionLine

RxJava example project
Java
23
star
21

StarWarsRosieExample

Example of Rosie framework using The Star Wars API
Java
21
star
22

calculator-unit-and-integration-tests

Java
18
star
23

bitrise-step-flutter

Bitrise step for Flutter
Go
18
star
24

Moya-JASON

JASON bindings for Moya.
Swift
17
star
25

SheetsTranslator

App translation generator using Google Sheets API
Swift
17
star
26

FoQA

Container for Quality Assurance utilities to be included in QA/testing variants of Android apps.
Java
16
star
27

ExampleRealm-Android

Java
11
star
28

mockwebserver-path-dispatcher

A helper for dispatching MockWebServer responses
Kotlin
10
star
29

smoge-flutter

Dart
10
star
30

swift-style-guide

Droids on Roids Swift guidelines
8
star
31

SnapchatFiltersView

Snapchat - like swipeable filters for views, photos, videos and live camera preview. Swift.
Shell
8
star
32

StructPersistence

Swift
7
star
33

android-style-guide

Kotlin
7
star
34

BeerApp

Simple introduction to data binding library.
Java
7
star
35

add-to-google-wallet

Dart
6
star
36

bitrise-step-sonarqube-scanner

Step for running SonarQube Scanner CLI
Shell
6
star
37

ChartsDemo

Charts demo
Swift
5
star
38

multi-window-drop-adjacent

Java
5
star
39

bitrise-step-trigger-bitrise-workflow

Bitrise step for triggering workflow on bitrise.io with specified parameters
Go
5
star
40

Habit

An easy-to-use library to make repeated notifications simpler
Shell
4
star
41

bitrise-step-openstf-connect

Bitrise step for Open STF devices connection
Go
4
star
42

auth0_rs256_jwt_verifier

Ruby gem for verifying Auth0 API JWT access tokens signed using RS256 algorithm.
Ruby
4
star
43

modeltesting

Sample project for blog post
Java
4
star
44

swift-vs-objc

Swift vs Obj-C performance comparision
Objective-C
4
star
45

MazeSpriteKit

Maze Game with SpriteKit demo
Swift
4
star
46

android-font-binder

Annotation-based typeface binding to TextViews
Kotlin
3
star
47

fluttrise

Unofficial Bitrise.io client made with Flutter.
Dart
3
star
48

multi-window-catch

Java
3
star
49

SwiftyRouter

SwiftyRouter makes it easy to deal with network connection in Swift
Swift
3
star
50

GCMSender

Push notifications sender for Google Cloud Messaging
Swift
3
star
51

PlanningPoker-Android

Java
2
star
52

bitrise_set_jira_features

Feature list as jira links to insert in release notes
Shell
2
star
53

TracepathAndroid

C
2
star
54

toast7-hackme

Hackme application for Toast #7
Java
2
star
55

bitrise_step_build_info_jira_comment

Shell
2
star
56

android-gradle-aosp-aapt-plugin

Gradle plugin which sets aaptConfig.additional parameters based on environment variables defined by AOSP build system
Groovy
2
star
57

EstimoteBeaconsDemo

The simplest of the simplest examples
Java
2
star
58

constraint-layout-charts

Java
2
star
59

MemoryLeaksExample

Java
2
star
60

android-runtime-permission-tock-too

Example of Time Of Check To Time Of Use race condition on Android
Kotlin
2
star
61

3DTouchExample

Example of all kinds of 3D Touch support written in Swift 3.0
Swift
2
star
62

compose-guidelines

2
star
63

TwitterLogIn

Swift
1
star
64

TouchID_swift

Swift
1
star
65

homebrew-tools

Ruby
1
star
66

B-VIPER-module-template

Swift
1
star
67

flutter-events

Flutter events app
Dart
1
star
68

DroidsMap

Kotlin
1
star
69

RealmToDo

To-do app with Realm
Swift
1
star
70

DORSidebar

Purely Swift sidebar menu
Swift
1
star
71

Unit-and-Integration-Tests-for-Android

Java
1
star
72

DORDoneHUD

Done animation in Objective-C
Objective-C
1
star
73

HoloGraphLibrary

Fork of HoloGraphLibrary by Daniel Nadeau https://bitbucket.org/danielnadeau/holographlibrary
Java
1
star
74

bitrise-step-publish-spoon-results

Bitrise step for publishing Spoon tests results on Github pages
Shell
1
star
75

bitrise-step-chuck-norris

Bitrise step for adding jokes about Chuck Norris to build log
Go
1
star
76

snakeyaml

snakeyaml fork with FIELD bean access
Java
1
star
77

android-aop-demo

AOP demo app
Java
1
star
78

bitrise-step-update-jira-issue-field

Bitrise step for updating JIRA issue fields
Go
1
star
79

DORUIKitDynamics

Sample UIKitDynamics, presented by one of ours iOS Developers
Swift
1
star
80

HipChatGerritHooks

Gerrit hooks sending notifications to HipChat
Python
1
star