Search code examples
swiftxcodeicarousel

Autoscrolling infinite effect in .linear type of iCarousel in swift


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()

Solution

  • 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)
        }