2011-12-07 26 views
0

我尝试构建无尽的滚动UIScrollView。到目前为止,我拿了苹果样本“StreetScroller”。因此,我所做的只是在contentOffset到达滚动视图的末尾时将其设置回来。无限UIScrollView无法在iOS 4.3上工作

覆盖的的UIScrollView的-layoutSubviews

- (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]; 
    } 
} 

现在在iOS 5这就像一个魅力。但在iOS 4.3上不起作用。只要我拨打[super setContentOffset:contentOffset],它就会弯腰滚动,因为下一次-layoutSubviews get的名称为[self contentOffset]不会返回已设置的contentOffset

我知道有很多关于无限UIScrollViews的问题,但其中一个解决了这个问题!

+1

这似乎是一个bug:http://stackoverflow.com/questions/2168598/iphone-uiscrollview-setcontentoffset-weirdness –

+0

你也可以尝试这个实现:[永无止境的分页UIScrollView](http:// brainbowapps.com/articles/2011/never-ending-paging-uiscrollview.html) –

+0

thx为链接,但这是一个寻呼解决方案。这对我来说不是一种选择,从来不是问题。 – V1ru8

回答

0

试试这个。此代码工作正常,我在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 
+0

没有抱歉,但没有按预期工作。这是无尽的滚动我同意,但它不是无缝的。到达边界时停止,只在停止后重置。我用你的代码设置了一个小型的Xcode项目。 http://dl.dropbox.com/u/15892171/InfiniteScrollView.zip – V1ru8

相关问题