码迷,mamicode.com
首页 > 其他好文 > 详细

UI-ScrollView的使用

时间:2015-08-04 11:10:58      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:ui   objective-c   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
技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

UI-ScrollView的使用

标签:ui   objective-c   scrollview   界面搭建   界面跳转   

原文地址:http://blog.csdn.net/u012701023/article/details/47273385

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!