三个滚动视图连用

- (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);
    
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值