想让我的UIScrollView无限(比如日历中的日期)

时间:2014-05-28 08:13:48

标签: ios objective-c ios7 uiscrollview calendar

我有一个像这样的UIScrollView:

self.view.backgroundColor = [UIColor redColor];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++) {
    CGFloat xOrigin = i * self.view.frame.size.width;
    UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
    awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
    [scroll addSubview:awesomeView];
}
[scroll setShowsHorizontalScrollIndicator:NO];

scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];

这是我想要的正确手势,对于一组有限的自定义子视图很好,但是如何让scollview变得无限?

我希望子视图为日期,左右滑动会显示下一个或上一个日期。

1 个答案:

答案 0 :(得分:0)

使用UIScrollViewDelegate方法-(void)scrollViewDidScroll:(UIScrollView*)scrollView并调整您的视图位置和在那里偏移的scrollviews内容,或者查看Apple关于如何通过继承{{1}创建无限水平ScrollView的示例}:https://developer.apple.com/library/ios/samplecode/StreetScroller/