2011-03-19 73 views
4

这里的理念是:如何在iPhone上制作无限长的滚动视图?

滚动视图具有几个子图,在这个例子中,A,B,C:

[A][B][C] 

一旦用户滚动到C,该视图不能再滚动,但我想循环回A,像这样:

[C][A][B] 

当用户一直向右滚动,认为不断填充在年底之前的看法。当用户滚动 到左的观点也应该有类似的行为, 为了使用户认为这种观点是无限长:

[A][B][C][A][B][C][A][B][C][A][B][C][A][B][C][A][B][C]..... 

我怎样才能在实际的代码实现这一点?谢谢。

回答

2

你需要设置一个委托方法,当视图滚动时被调用,然后检查任何多于一个屏幕值的像素远离可见的视图,如果它然后重新定位视图3 *屏幕宽度到另一边。

0

这就是你想要的(在iphone 7 plus上)。这是纯代码用户界面,没有故事板。首先,您需要在AppDelegate.m中添加子代码。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
// Override point for customization after application launch. 
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
[self.window makeKeyAndVisible]; 
ViewController *vc = [ViewController alloc]; 
self.window.rootViewController = vc; 
self.window.backgroundColor = [UIColor redColor]; 

return YES; 

}

其次在ViewController.m

@interface ViewController()<UIScrollViewDelegate> 
@property (nonatomic, strong) UIScrollView *readCannelScrollView; 
@property (nonatomic, strong) UIImageView *pageOneView; 
@property (nonatomic, strong) UIImageView *pageTwoView; 
@property (nonatomic, strong) UIImageView *pageThreeView; 
@end 

@implementation ViewController 
- (void)dealloc 
{ 
    _readCannelScrollView = nil; 
    _pageOneView = nil; 
    _pageTwoView = nil; 
    _pageThreeView = nil; 
} 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    //容器的属性设置 
    self.readCannelScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; 
    [self.view addSubview:self.readCannelScrollView]; 
    CGSize size = self.readCannelScrollView.contentSize; 
    size.width = 414*3; 
    self.readCannelScrollView.contentSize = size; 
    self.readCannelScrollView.pagingEnabled = YES; 
    self.readCannelScrollView.showsHorizontalScrollIndicator = NO; 
    self.readCannelScrollView.delegate = self; 
    self.readCannelScrollView.contentOffset = CGPointMake(0, 0); 
    //end 
    //添加页面1 
    self.pageOneView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 414, self.view.bounds.size.height)]; 
    self.pageOneView.backgroundColor = [UIColor lightGrayColor]; 
    self.pageOneView.image = [UIImage imageNamed:@"1"]; 
// self.pageOneView.font = [UIFont systemFontOfSize:80]; 
// self.pageOneView.textAlignment = NSTextAlignmentCenter; 
    [self.readCannelScrollView addSubview:self.pageOneView]; 
    //添加页面2 
    self.pageTwoView = [[UIImageView alloc] initWithFrame:CGRectMake(828, 0, 414, self.view.bounds.size.height)]; 
    self.pageTwoView.backgroundColor = [UIColor greenColor]; 
    self.pageTwoView.image = [UIImage imageNamed:@"2"]; 
// self.pageTwoView.font = [UIFont systemFontOfSize:80]; 
// self.pageTwoView.textAlignment = NSTextAlignmentCenter; 
    [self.readCannelScrollView addSubview:self.pageTwoView]; 
    //添加页面3 
    self.pageThreeView = [[UIImageView alloc] initWithFrame:CGRectMake(828, 0, 414, self.view.bounds.size.height)]; 
    self.pageThreeView.backgroundColor = [UIColor grayColor]; 
    self.pageThreeView.image = [UIImage imageNamed:@"3"]; 
// self.pageThreeView.font = [UIFont systemFontOfSize:80]; 
// self.pageThreeView.textAlignment = NSTextAlignmentCenter; 
    [self.readCannelScrollView addSubview:self.pageThreeView]; 
} 
- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 
#pragma mark - 
#pragma mark - scroll delegate 
- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{ 
    NSLog(@"scrollView.contentOffset.x=%f",scrollView.contentOffset.x); 
    CGFloat pageWidth = scrollView.frame.size.width; 
    int currentPage = floor((scrollView.contentOffset.x-pageWidth/2)/pageWidth)+1; 
    if (currentPage == 0) 
    { 
     UIImageView *tmpTxtView = self.pageThreeView; 
     self.pageThreeView = self.pageTwoView; 
     self.pageTwoView = self.pageOneView; 
     self.pageOneView = tmpTxtView; 
    } 
    if (currentPage == 2) 
    { 
     //换指针 
     UIImageView *tmpTxtView = self.pageOneView; 
     self.pageOneView = self.pageTwoView; 
     self.pageTwoView = self.pageThreeView; 
     self.pageThreeView = tmpTxtView; 
    } 
    //恢复原位 
    self.pageOneView.frame = (CGRect){0,0,self.pageOneView.frame.size}; 
    self.pageTwoView.frame = (CGRect){414,0,self.pageTwoView.frame.size}; 
    self.pageThreeView.frame = (CGRect){828,0,self.pageThreeView.frame.size}; 
    self.readCannelScrollView.contentOffset = CGPointMake(414, 0); 
} 
@end 

添加子代码如果满意我的回答给我一个star.I需要的声誉。