Carousel View in iOS swift
Solution:
1)First you have to download the carousalView using the below URL.
https://github.com/nicklockwood/iCarousel
2) Open the examples folder and open swift3 Project using xcode.
3) Create folder named iCarousel in your project.
4) IN the swift 3 example project find iCarousel.h and iCarousel.m files..Drag and drop those files to your project inside iCarousel folder.
5) Goto your project storyboard and create one UIView and select the class as iCarousel.Then create the outlet to your view.
1)First you have to download the carousalView using the below URL.
https://github.com/nicklockwood/iCarousel
2) Open the examples folder and open swift3 Project using xcode.
3) Create folder named iCarousel in your project.
4) IN the swift 3 example project find iCarousel.h and iCarousel.m files..Drag and drop those files to your project inside iCarousel folder.
5) Goto your project storyboard and create one UIView and select the class as iCarousel.Then create the outlet to your view.
@IBOutlet weak var carosalView: iCarousel!
class ViewController:
UIViewController,iCarouselDataSource, iCarouselDelegate {
var List: NSMutableArray! = NSMutableArray()
override func viewDidLoad() {
carosalView.type = .coverFlow
}
}
// MARK: - carousalViewDelegate
func numberOfItems(in carousel: iCarousel) -> Int {
return List.count
}
func carouselCurrentItemIndexDidChange(_ carousel: iCarousel) {
print("called",carousel.currentItemIndex)
if carousel.currentItemIndex >= 0 {
dataLoad(index: carousel.currentItemIndex)
}
}
func carousel(_ carousel: iCarousel, didSelectItemAt index: Int) {
dataLoad(index: index)
}
func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
var label: UILabel
var imgView: UIImageView
var itemView: UIView
let loc = List.object(at: index)
//reuse view if available, otherwise create a new view
if let view = view as? UIView {
itemView = view
//get a reference to the label in the recycled view
label = itemView.viewWithTag(1) as! UILabel
imgView = itemView.viewWithTag(2) as! UIImageView
} else {
//don't do anything specific to the index within
//this `if ... else` statement because the view will be
//recycled and used with other index values later
itemView = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 150))
itemView.backgroundColor = UIColor.white
imgView = UIImageView(frame: CGRect(x: 25, y: 25, width: 100, height: 100))
imgView.image = UIImage(named: "yourImage")!
imgView.tag = 2
itemView.addSubview(imgView)
itemView.contentMode = .center
label = UILabel(frame: CGRect(x: imgView.frame.origin.x, y: imgView.frame.origin.y+imgView.frame.size.height, width: 100, height: 20))
label.backgroundColor = .clear
label.textAlignment = .center
label.textColor = oldColor
label.font = label.font.withSize(12)
label.tag = 1
itemView.addSubview(label)
}
//set item label
//remember to always set any properties of your carousel item
//views outside of the `if (view == nil) {...}` check otherwise
//you'll get weird issues with carousel item content appearing
//in the wrong place in the carousel
label.text ="title"
return itemView
}
func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {
if (option == .spacing) {
return value * 1.1
}
return value
}
Comments
Post a Comment