开发者

How to make an infinitely long scroll view on iPhone?

Here is the idea:

The scroll view has several sub views, in this example, A, B, C:

[A][B][C]

Once the user scrolls to C, the view can'开发者_Python百科t scroll anymore, but I want to loop back to A, like this:

[C][A][B]

When the user keeps scrolling to the right, the view keeps filling previous views in at the end. When the user scrolls to left the view should also have similar behavior, in order to make the user think that this view is infinitely long:

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

How can I implement this in actual code? Thank you.


you need to set a delegate method that gets called when the view is scrolled, and then check any view that is more than one screens worth of pixels away from being visible, if it is then reposition the view 3*screen width to the other side.


This is what you want( on iphone 7 plus). This is pure code UI, no storyboard.First you need add sub code in your 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;

}

Second add sub code in 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

If you are satisfied with my answer give me a star.I need reputation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜