I need infinite autoscrolling effect in .linear type in iCarousel and i already achieve autoscrolling in .cylinder type but i can't achieve this effect in .linear type.
Here is my code of achieve autoscrolling in .cylinder type
carousel.type = .linear
carousel.autoscroll = -0.4;
carousel.reloadData()
Yes, this does not work carousel.type = .linear
in the case of linear
so you have to make an own timer for scrolling just like that:
self.timer = NSTimer.scheduledTimerWithTimeInterval(6, target: self, selector: #selector(self.handleTimer), userInfo: nil, repeats: true)
func handleTimer(){
if itemsScroll.count != 0{
if itemsScroll.count-1 == index{
index = 0
}
else {
index += 1
}
}
let x = CGFloat(index)
if index == 0 {
carousel.scrollToOffset(x, duration: 0)
}
else {
carousel.scrollToOffset(x, duration: 2)
}
}
The index
is used to get the current data for the carousel data source.
Thanks @salman for an answer with the help of Salman's answer I got a solution for an infinite solution without jerking issue when carousel type is linear
Please follow the steps given below.
1. Define timer for handle scrolling
_ = Timer.scheduledTimer(timeInterval: 4, target: self, selector: #selector(self.handleTimer), userInfo: nil, repeats: true)
2. Write the delegate method of the carousel and handle wrap type with the help of wrap we solve jerk issue.
func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {
switch option {
case .wrap:
return 1
default:
return value
}
}
3. Method to handle scrolling
func handleTimer() {
var newIndex = self.carousel.currentItemIndex + 1
if newIndex > self.carousel.numberOfItems {
newIndex = 0
}
carousel.scrollToItem(at: newIndex, duration: 0.5)
}
来源:https://stackoverflow.com/questions/44950769/autoscrolling-infinite-effect-in-linear-type-of-icarousel-in-swift