0

I try to build a endless scrolling UIScrollView. So far I took the apple sample "StreetScroller". So all I do is setting the contentOffset back when it reaches the end of the scroll view.

Override -layoutSubviews of the UIScrollView:

- (void)layoutSubviews
{
    CGFloat contentWidth = [self contentSize].width;
    CGPoint contentOffset = [self contentOffset];

    CGFloat centerOffsetX = (contentWidth - [self bounds].size.width) / 2.0;
    CGFloat distanceFromCenter = contentOffset.x - centerOffsetX;
    if (ABS(distanceFromCenter) > (contentWidth / 4.0)) {
        contentOffset = CGPointMake(centerOffsetX, contentOffset.y);
        [super setContentOffset:contentOffset];
    }
}

Now on iOS 5 this works like a charm. But on iOS 4.3 it's not working. As soon as I call [super setContentOffset:contentOffset] it stoops scrolling because next time -layoutSubviews get's called the [self contentOffset] does not return the contentOffset that was set.

I know there are a lot a questions about infinite UIScrollViews, but one of these has fixed this problem!

V1ru8
  • 6,139
  • 4
  • 30
  • 46
  • 1
    This seems to be a bug: http://stackoverflow.com/questions/2168598/iphone-uiscrollview-setcontentoffset-weirdness – Patrick Tescher Dec 11 '11 at 04:49
  • You can also try this implementation: [never-ending paging UIScrollView](http://brainbowapps.com/articles/2011/never-ending-paging-uiscrollview.html) – Lorenzo B Dec 15 '11 at 08:14
  • thx for the link but this is a paging solution. That's not an option for me and was never a problem. – V1ru8 Dec 15 '11 at 08:39

1 Answers1

0

Try this One. This Code is Working Properly for me on iOS 4.3

RootViewController.h

@class ViewControllerForDuplicateEndCaps;

@interface RootViewController : UIViewController {

ViewControllerForDuplicateEndCaps *viewControllerForDuplicateEndCaps;
}

@property (nonatomic, retain) ViewControllerForDuplicateEndCaps *viewControllerForDuplicateEndCaps;

- (IBAction)loadScrollViewWithDuplicateEndCaps:(id)sender; 

@end

RootViewController.m

#import "RootViewController.h"
#import "ViewControllerForDuplicateEndCaps.h"
#import "InfiniteScrollViewAppDelegate.h"

@implementation RootViewController

@synthesize viewControllerForDuplicateEndCaps;


- (IBAction)loadScrollViewWithDuplicateEndCaps:(id)sender {
InfiniteScrollViewAppDelegate *delegate = (InfiniteScrollViewAppDelegate*)[[UIApplication sharedApplication] delegate];

if(self.viewControllerForDuplicateEndCaps == nil) {
    ViewControllerForDuplicateEndCaps *temp = [[ViewControllerForDuplicateEndCaps alloc] initWithNibName:@"ViewControllerForDuplicateEndCaps" bundle:nil];
    self.viewControllerForDuplicateEndCaps = temp;
    [temp release];
}

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];

[delegate.navigationController pushViewController:self.viewControllerForDuplicateEndCaps animated:YES];
}

- (void)dealloc {

[scrollView release];
[super dealloc];
 }
@end

ViewControllerForDuplicateEndCaps.h

#import <UIKit/UIKit.h>

@interface ViewControllerForDuplicateEndCaps : UIViewController <UIScrollViewDelegate> {

IBOutlet UIScrollView *scrollView;
}

@property (nonatomic, retain) UIScrollView *scrollView;

- (void)addImageWithName:(NSString*)imageString atPosition:(int)position;


@end

ViewControllerForDuplicateEndCaps.m

#import "ViewControllerForDuplicateEndCaps.h"

@implementation ViewControllerForDuplicateEndCaps

@synthesize scrollView;


 - (void)viewDidLoad {

 [super viewDidLoad];

// add the last image (image4) into the first position
[self addImageWithName:@"image4.jpg" atPosition:0];

// add all of the images to the scroll view
for (int i = 1; i < 5; i++) {
    [self addImageWithName:[NSString stringWithFormat:@"image%i.jpg",i] atPosition:i];
}

// add the first image (image1) into the last position
[self addImageWithName:@"image1.jpg" atPosition:5];

scrollView.contentSize = CGSizeMake(1920, 416);    
[scrollView scrollRectToVisible:CGRectMake(320,0,320,416) animated:NO]; 
}

- (void)addImageWithName:(NSString*)imageString atPosition:(int)position {
// add image to scroll view
UIImage *image = [UIImage imageNamed:imageString];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(position*320, 0, 320, 416);
[scrollView addSubview:imageView];
[imageView release];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {    
NSLog(@"%f",scrollView.contentOffset.x);
// The key is repositioning without animation      
if (scrollView.contentOffset.x == 0) {         
    // user is scrolling to the left from image 1 to image 4         
    // reposition offset to show image 4 that is on the right in the scroll view         
    [scrollView scrollRectToVisible:CGRectMake(1280,0,320,416) animated:NO];     
}    
else if (scrollView.contentOffset.x == 1600) {         
    // user is scrolling to the right from image 4 to image 1        
    // reposition offset to show image 1 that is on the left in the scroll view         
    [scrollView scrollRectToVisible:CGRectMake(320,0,320,416) animated:NO];         
} 
}

- (void)dealloc {

[scrollView release];
[super dealloc];
}
@end
Nishith Shah
  • 523
  • 3
  • 16
  • No sorry but this does not work as expected. It's endless scrolling I agree, but it's not seamless. It stops when arriving at the boundary and only resets after stopping. I set up a small Xcode project with your code. http://dl.dropbox.com/u/15892171/InfiniteScrollView.zip – V1ru8 Dec 14 '11 at 15:18