0

I have a Scrollview in my app that shows a few slides, the default behaviour of the slides are when you reach the last one (ie slide 5 for example) is that it doesn't scroll more to the right, or when you go to the first one it doesn't scroll more to the left.

how do I make it infinity loop , meaning when I see the last slide, and I scroll more right, it will just scroll to the first one? (or from the first one left it will scroll to the last one)

here is my scrollViewDidScroll:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let pageIndex = round(scrollView.contentOffset.x/view.frame.width)
        pageControl.currentPage = Int(pageIndex)

        let maximumHorizontalOffset: CGFloat = scrollView.contentSize.width - scrollView.frame.width
        let currentHorizontalOffset: CGFloat = scrollView.contentOffset.x

        // vertical
        let maximumVerticalOffset: CGFloat = scrollView.contentSize.height - scrollView.frame.height
        let currentVerticalOffset: CGFloat = scrollView.contentOffset.y

        let percentageHorizontalOffset: CGFloat = currentHorizontalOffset / maximumHorizontalOffset
        let percentageVerticalOffset: CGFloat = currentVerticalOffset / maximumVerticalOffset


        /*
         * below code changes the background color of view on paging the scrollview
         */
        //        self.scrollView(scrollView, didScrollToPercentageOffset: percentageHorizontalOffset)


        /*
         * below code scales the imageview on paging the scrollview
         */
        let percentOffset: CGPoint = CGPoint(x: percentageHorizontalOffset, y: percentageVerticalOffset)
        print ("present offset: ", percentOffset.x)
        if(percentOffset.x > 0 && percentOffset.x <= 0.25) {

            slides[0].mainPic.transform = CGAffineTransform(scaleX: (0.25-percentOffset.x)/0.25, y: (0.25-percentOffset.x)/0.25)
            slides[1].mainPic.transform = CGAffineTransform(scaleX: percentOffset.x/0.25, y: percentOffset.x/0.25)

        } else if(percentOffset.x > 0.25 && percentOffset.x <= 0.50) {
            slides[1].mainPic.transform = CGAffineTransform(scaleX: (0.50-percentOffset.x)/0.25, y: (0.50-percentOffset.x)/0.25)
            slides[2].mainPic.transform = CGAffineTransform(scaleX: percentOffset.x/0.50, y: percentOffset.x/0.50)

        } else if(percentOffset.x > 0.50 && percentOffset.x <= 0.75) {
            slides[2].mainPic.transform = CGAffineTransform(scaleX: (0.75-percentOffset.x)/0.25, y: (0.75-percentOffset.x)/0.25)
            slides[3].mainPic.transform = CGAffineTransform(scaleX: percentOffset.x/0.75, y: percentOffset.x/0.75)

        } else if(percentOffset.x > 0.75 && percentOffset.x <= 1) {
            slides[3].mainPic.transform = CGAffineTransform(scaleX: (1-percentOffset.x)/0.25, y: (1-percentOffset.x)/0.25)
            slides[4].mainPic.transform = CGAffineTransform(scaleX: percentOffset.x, y: percentOffset.x)

        } 
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Chief Madog
  • 1,738
  • 4
  • 28
  • 55
  • check if you can use https://github.com/nicklockwood/iCarousel. Instead of writing your own logic to achieve. – Mohammad Sadiq Jan 09 '19 at 09:00
  • 1
    Here is the soultion https://stackoverflow.com/questions/16788657/ios-scrollview-infinite-paging-duplicate-end-caps – iEngineer Jan 09 '19 at 09:01
  • Possible duplicate of [iOS: ScrollView infinite paging - Duplicate end caps](https://stackoverflow.com/questions/16788657/ios-scrollview-infinite-paging-duplicate-end-caps) – Sateesh Yemireddi Jan 09 '19 at 10:02
  • @ChiefMadog I need more information for that. What is slides ? is in an NSObject ? and you showing separate UIView for each slide or UIImageView ? – iEngineer Jan 09 '19 at 12:14
  • i'm showing a seperate UIView for each of them – Chief Madog Jan 09 '19 at 12:27
  • @separate it's not becouse that post is just the basic funcionality of ending in the last slide, and i want it to continue scrolling left just return to the first one when finish – Chief Madog Jan 09 '19 at 12:27

1 Answers1

0

Not sure about your current implementation but your solution will be something like, I'm assuming your slides array contains UIView for each slide. Change the code according to your current implementation.

(void)scrollViewDidScroll:(UIScrollView *)scrollView {

    if(if(scrollView.contentOffset.x < 0)) {
        CGPoint newOffset = CGPointMake(scrollView.bounds.size.width+scrollView.contentOffset.x, scrollView.contentOffset.y);
        [scrollView setContentOffset:newOffset];
        [self rotateViewsRight];
    }
    else if(scrollView.contentOffset.x > scrollView.bounds.size.width*2) {
        CGPoint newOffset = CGPointMake(scrollView.contentOffset.x-scrollView.bounds.size.width, scrollView.contentOffset.y);
        [scrollView setContentOffset:newOffset];
        [self rotateViewsLeft];
    }
}

-(void)rotateViewsRight {
    UIView *endView = [slides lastObject];
    [slides removeLastObject];
    [slides insertObject:endView atIndex:0];
    [self setContentViewFrames];

}

-(void)rotateViewsLeft {
    UIView *endView = slides[0];
    [slides removeObjectAtIndex:0];
    [slides addObject:endView];
    [self setContentViewFrames];

}

-(void) setContentViewFrames {
    for(int i = 0; i < 5; i++) {
        UIView * view = slides[i];
        [view setFrame:CGRectMake(self.view.bounds.size.width*i, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    }
}
iEngineer
  • 1,319
  • 1
  • 11
  • 27
  • this is an obj C code, and i said i'm using swift, also in the comment for you, in any way i wrote it in swift, and it doesn't work it has same funcionality of ending at the last slide – Chief Madog Jan 09 '19 at 12:25