Hello and welcome to all. Today we will explore a swift library TGLParallaxCarousel. By using this library in our swift project we will make a image carousel with 3d effect. So let's start our tutorial.
First of all create a new project in your Xcode.
Now we need to install 'TGLParallaxCarousel pod' in our project, so that we can use pod supporting files to create our carousel. But we can't install pod directly in our project, first we need to create a podfile in our project, so that we can write pod name in that file to install pod.
To create podfile in our project, we use command pod init. So, Open Terminal and navigate to the directory that contains your project by using the cd command like this:
cd /some drive/Folder/your project folder
pod init command in your terminal and press enter. This command creates a podfile.rb file in your project directory.pod init
podfile.rb file. We can do this by two ways first is by terminal and second by editing file manually from finder. We will use the second one method. So go to your project directory using finder and open podfile.rb file in any editor. Now add pod 'TGLParallaxCarousel' in your podfile like and save the file :-# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target '3dCarousel' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for 3dCarousel
pod 'TGLParallaxCarousel'
end
pod install to install dependencies of pod in our project. So head back to our project root directory in terminal and run the command :-pod install
[!] Please close any current Xcode sessions and use `your project name.xcworkspace` for this project from now on.
So close the xcode and open .xcworkspace file to start programming for our project.
main.storyboard and place a UIView object in your view controller and set it as subclass of TGLParallaxCarousel like this :-import UIKit
class ViewController: UIViewController {
@IBOutlet var carouselView: TGLParallaxCarousel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
After making outlet you will get error saying use of unresolved identifier. You are getting this error because we didn't import TGLParallaxCarousel class in our view controller file. So add import TGLParallaxCarousel under import UIKit in your view controller file.
Now we will set some properties and delegates of carousel in our view controller viewDidLoad method. As we are making 3d carousel, so we will set carousel type to three dimensional and also give some margin to carousel items to give a feel of 3d effect. So write this code in your view controller viewDidLoad method :-
override func viewDidLoad() {
super.viewDidLoad()
self.carouselView.delegate = self
self.carouselView.margin = 10
self.carouselView.type = .threeDimensional
}
Now we will use TGLParallaxCarousel delegate methods to set number of items in carousel, item for row at index. There are also other methods with different functionality but i am using only two delegate methods for now to not make tutorial too lengthy. To use delegate methods, first add TGLParallaxCarouselDelegate in your view controller class like this :-
import UIKit
import TGLParallaxCarousel
class ViewController: UIViewController, TGLParallaxCarouselDelegate {
@IBOutlet var carouselView: TGLParallaxCarousel!
override func viewDidLoad() {
super.viewDidLoad()
self.carouselView.delegate = self
self.carouselView.margin = 10
self.carouselView.type = .threeDimensional
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
class ViewController: UIViewController, TGLParallaxCarouselDelegate {
@IBOutlet var carouselView: TGLParallaxCarousel!
override func viewDidLoad() {
super.viewDidLoad()
self.carouselView.delegate = self
self.carouselView.margin = 10
self.carouselView.type = .threeDimensional
}
func numberOfItemsInCarouselView(_ carouselView: TGLParallaxCarousel) -> Int {
return 10
}
func carouselView(_ carouselView: TGLParallaxCarousel, itemForRowAtIndex index: Int) -> TGLParallaxCarouselItem {
let demoView = UIView
demoView.frame = CGRect(x: 0, y: 0, width: 200, height: 180)
demoView.backgroundColor = UIcolor.black
return demoView, number: index)
}
}
What we did in above code is we add 10 items in our carousel in numberOfItemsInCarouselView method and we also set a uiview with some height and width for every item in itemForRowAtIndex.
Thanks for reading the tutorial, if you had any doubt regarding this comment in post. I will help you.