UI-ScrollView的使用

//相对位置,学会使用-scrollView.contentSize = CGSizeMake(320*3,160);
#import "ViewController.h"

@interface ViewController ()
{
    UIScrollView *_scrollView1;
    UIScrollView *_scrollView2;
    UIScrollView *_scrollView3;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _scrollView1 = [[UIScrollView alloc]init];
    _scrollView1.frame = CGRectMake(0, 0, 320, 160);
    _scrollView1.bounces = NO;
    _scrollView1.pagingEnabled = YES;
    _scrollView1.delegate = self;
    _scrollView1.tag = 2;
    _scrollView1.contentSize = CGSizeMake(320*3, 160);
    [self.view addSubview:_scrollView1];
    for (int i = 0; i < 3; i++) {
        UILabel * label = [[UILabel alloc]init];
        label.frame = CGRectMake(320*i, 0, 320, 160);
        label.backgroundColor = [UIColor redColor];
        label.text = [NSString stringWithFormat:@"%d 只鸡",i+1];
        label.font = [UIFont boldSystemFontOfSize:30];
        label.textAlignment = NSTextAlignmentCenter;
        [_scrollView1 addSubview:label];
    }
    
    _scrollView2 = [[UIScrollView alloc]init];
    _scrollView2.frame = CGRectMake(0, 160, 320, 160);
    _scrollView2.bounces = NO;
    _scrollView2.pagingEnabled = YES;
    _scrollView2.contentSize = CGSizeMake(320*3, 160);
    _scrollView2.delegate = self;
    _scrollView2.tag = 4;
    [self.view addSubview:_scrollView2];
    for (int i =0; i<3; i++) {
        UILabel * label = [[UILabel alloc]init];
        label.frame = CGRectMake(320*i, 0, 320, 160);//这里是相对于scrollView设定的
        label.backgroundColor = [UIColor blueColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = [NSString stringWithFormat:@"%d只羊",i+1];
        label.font = [UIFont boldSystemFontOfSize:30];
        [_scrollView2 addSubview:label];
    }
    
    _scrollView3 = [[UIScrollView alloc]init];
    _scrollView3.frame = CGRectMake(0, 320, 320, 160);
    _scrollView3.bounces = NO;
    _scrollView3.delegate = self;
    _scrollView3.pagingEnabled = YES;
    _scrollView3.contentSize = CGSizeMake(320*18, 160);
    [self.view addSubview:_scrollView3];
    for (int i = 0; i < 18; i++) {
        UILabel *label = [[UILabel alloc]init];
        label.frame = CGRectMake(320*i, 0, 320, 160);
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor = [UIColor cyanColor];
        label.text = [NSString stringWithFormat:@"%d只腿",i+1];
        label.font = [UIFont boldSystemFontOfSize:30];
        [_scrollView3 addSubview:label];
    }
    
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int sum = (_scrollView1.contentOffset.x/320+1)*_scrollView1.tag + (_scrollView2.contentOffset.x/320+1)*_scrollView2.tag;
    [_scrollView3 setContentOffset:CGPointMake((sum-1)*320,0)] ;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值