//相对位置,学会使用-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的使用
最新推荐文章于 2024-09-09 11:27:55 发布