如何在iPhone上制作无限长的滚动视图?

时间:2011-03-19 01:23:14

标签: iphone objective-c uiscrollview

这是一个想法:

滚动视图有几个子视图,在本例中为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 个答案:

答案 0 :(得分:2)

您需要设置一个在滚动视图时调用的委托方法,然后检查任何多于一个屏幕的视图,使其远离可见,如果是,则将视图3 *屏幕宽度重新定位到另一边。

答案 1 :(得分:0)

这就是你想要的(在iphone 7 plus上)。这是纯粹的代码UI,没有故事板。首先,您需要在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

如果你对我的回答感到满意,请给我一个明星。我需要声誉。