iOS7 UIScrollView

####iOS7 Programming Cookbook 第一章学习笔记 UIScrollView

#####Creating Scrollable Content with UIScrollView

#####ViewController.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@interface ViewController () <UIScrollViewDelegate>
//声明 私有变量 UIScrollView UIImageView
@property (nonatomic, strong) UIScrollView *myScrollView;
@property (nonatomic, strong) UIImageView *myImageView;
@end
- (UIImageView *) newImageViewWithImage:(UIImage *)paramImage
frame:(CGRect)paramFrame{

UIImageView *result = [[UIImageView alloc] initWithFrame:paramFrame];
//保持长宽比的大小,任何剩余视图区域边界透明
result.contentMode = UIViewContentModeScaleAspectFit;
result.image = paramImage;
return result;

}

- (void)viewDidLoad{
[super viewDidLoad];

//创建image对象
UIImage *iPhone = [UIImage imageNamed:@"iPhone"];
UIImage *iPad = [UIImage imageNamed:@"iPad"];
UIImage *macBookAir = [UIImage imageNamed:@"MacBookAir"];

//设置scrollview坐标和尺寸
CGRect scrollViewRect = self.view.bounds;
//创建scrollView
self.myScrollView = [[UIScrollView alloc] initWithFrame:scrollViewRect];
//是否启动分页滚动视图
self.myScrollView.pagingEnabled = YES;
//内容视图大小
self.myScrollView.contentSize = CGSizeMake(scrollViewRect.size.width * 3.0f,
scrollViewRect.size.height);
//添加到view
[self.view addSubview:self.myScrollView];

//设置image坐标和尺寸
CGRect imageViewRect = self.view.bounds;

//创建imageview
UIImageView *iPhoneImageView = [self newImageViewWithImage:iPhone frame:imageViewRect];
//添加image到scrollview
[self.myScrollView addSubview:iPhoneImageView];

//去到下一个视图 移动x坐标的位置
imageViewRect.origin.x += imageViewRect.size.width;
UIImageView *iPadImageView = [self newImageViewWithImage:iPad frame:imageViewRect];
[self.myScrollView addSubview:iPadImageView];

//去到下一个视图 移动x坐标的位置
imageViewRect.origin.x += imageViewRect.size.width;
//创建macBookAirImageView添加到scrollview
UIImageView *macBookAirImageView = [self newImageViewWithImage:macBookAir frame:imageViewRect];
[self.myScrollView addSubview:macBookAirImageView];

}

Reference

坚持原创技术分享,您的支持将鼓励我继续创作!
0%