Autoscrolling Infinite Effect in .Linear Type of Icarousel in Swift

Autoscrolling infinite effect in .linear type of iCarousel in swift

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

Repeating iCarousel's scrolling

What solved the problem:

var timer: Timer!

self.timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.handleTimer), userInfo: nil, repeats: true)

func handleTimer(){
self.carousel.scroll(byNumberOfItems: 1, duration: 0.3)
}

Making infinite loop using iCarousel

Yup, this is easy to do in iCarousel:

- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value {
switch (option) {
case iCarouselOptionWrap:
return YES;
}

return value;
}

Insert in a tableView set to Autoresize cause a scroll issue

I think you can cache height value for every cell in cellForRowAtIndex method and in HeightForRowAtIndex set the corresponding height for that cell.

You can just do this way :

var cellCached = Dictionary<Int,AnyObject>()

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
table.rowHeight = yourCell.yourLabel.frame.origin.y + yourCell.yourLabel.frame.height + 10

cellCached[indexPath.row] = yourTable.rowHeight
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

if(tableView == yourTable)
{
if(cellCached[indexPath.row] != nil)
{
return cellCached[indexPath.row] as! CGFloat
}
else
{
return 200
}
}
else
{
return UITableViewAutomaticDimension
}

}


Related Topics



Leave a reply



Submit