- (void)viewDidLoad {
[super viewDidLoad];
NSArray *colors = @[[UIColor lightGrayColor],[UIColor whiteColor],[UIColor grayColor]];
for (int i = 0; i < 3; i++) {
UIScrollView *scrollView = [[UIScrollView alloc] init];
if (i == 2) {
scrollView.contentSize = CGSizeMake(320*18, 160);
} else {
scrollView.contentSize = CGSizeMake(320*3, 160);
}
scrollView.frame = CGRectMake(0, i*160, 320, 160);
scrollView.backgroundColor = [colors objectAtIndex:i];
scrollView.bounces = NO;
scrollView.showsHorizontalScrollIndicator = YES;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.pagingEnabled = YES;
scrollView.tag = i+1;
scrollView.delegate = self;
[self.view addSubview:scrollView];
if (i == 0) {
NSArray *array = @[@"1只鸡",@"2只鸡",@"3只鸡"];
for (int j = 0; j < 3; j++) {
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(320*j, 0, 320, 160);
label.text = array[j];
label.tag = 100+j;
label.textAlignment = NSTextAlignmentCenter;
[scrollView addSubview:label];
}
}
if (i == 1) {
NSArray *array = @[@"1只羊",@"2只羊",@"3只羊"];
for (int j = 0; j < 3; j++) {
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(320*j, 0, 320, 160);
label.text = array[j];
label.tag = 1000+j;
label.textAlignment = NSTextAlignmentCenter;
[scrollView addSubview:label];
}
}
if (i == 2) {
for (int j = 0; j < 18; j++) {
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(320*j, 0, 320, 160);
label.text = [NSString stringWithFormat:@"%d只腿",j+1];
label.textAlignment = NSTextAlignmentCenter;
[scrollView addSubview:label];
}
}
}
}
static int count1 = 1,count2 = 1;
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
int index = scrollView.contentOffset.x/320;
if (scrollView.tag == 1) {
count1 = index+1;
} else if (scrollView.tag == 2) {
count2 = index+1;
}
UIScrollView *scrollView3 = (UIScrollView *)[self.view viewWithTag:3];
scrollView3. contentOffset = CGPointMake((count1*2+count2*4-1)*320, 0);
}
三个滚动视图连用
最新推荐文章于 2023-03-27 14:44:42 发布