Autoscrolling infinite effect in .linear type of iCarousel in swift

泄露秘密 提交于 2019-12-02 03:00:41

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.

SmarterSusheel

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)
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!