1 #import "ViewController.h"
2
3 @interface ViewController ()<UIScrollViewDelegate>
4 @property(strong,nonatomic)UIScrollView *
ScrollView;
5 @property(strong,nonatomic)UIImageView *
view1;
6
7 @property(strong,nonatomic)UIPageControl *
PageControl;
8 @property(strong,nonatomic)UILabel *
titltLabel;
9
10 @property(strong,nonatomic)NSArray *
contentArray;
11 @property(strong,nonatomic)NSArray *
titleArray;
12
13 @property(strong,nonatomic)NSTimer *
timerView;
14 -(NSArray *
)contentArray;
15 @end
16
17 @implementation ViewController
18
19 -(NSArray *
)contentArray
20 {
21 NSString * path = [[NSBundle mainBundle]pathForResource:
@"photo.plist" ofType:nil];
22 _contentArray =
[NSArray arrayWithContentsOfFile:path];
23 return _contentArray;
24 }
25
26 - (
void)viewDidLoad {
27 [super viewDidLoad];
28
29 self.titleArray = @[
@"美女1",
@"美女2",
@"美女3",
@"美女4"];
30
31 CGFloat width =
self.view.bounds.size.width;
32 self.ScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(
0,
10, width,
313)];
33 self.ScrollView.backgroundColor =
[UIColor groupTableViewBackgroundColor];
34 self.ScrollView.contentOffset = CGPointMake(width,
0);
35 self.ScrollView.contentSize = CGSizeMake(width*
6,
313);
36 self.ScrollView.pagingEnabled =
YES;
37 [self.view addSubview:self.ScrollView];
38 self.ScrollView.
delegate =
self;
39 self.ScrollView.showsHorizontalScrollIndicator =
NO;
40 [self.view addSubview:self.ScrollView];
41
42
43 for (
int i; i <
6; i++
)
44 {
45 NSDictionary * dic =
self.contentArray[i];
46 self.view1 = [[UIImageView alloc]initWithFrame:CGRectMake(width*i,
10, width,
313)];
47 self.view1.image = [UIImage imageNamed:dic[
@"icon"]];
48 [self.ScrollView addSubview:self.view1];
49 }
50
51
52
53 self.titltLabel = [[UILabel alloc]initWithFrame:CGRectMake(width/
2,
280,
80,
60)];
54 self.titltLabel.text = self.titleArray[
0];
55 [self.view addSubview:self.titltLabel];
56
57 self.PageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(width/
2,
300,
50,
20)];
58 self.PageControl.numberOfPages =
4;
59 self.PageControl.currentPage =
0;
60 //设置小白点颜色
61 [self.PageControl setPageIndicatorTintColor:[UIColor groupTableViewBackgroundColor]];
62 [self.PageControl setCurrentPageIndicatorTintColor:[UIColor greenColor]];
63 [self.view addSubview:self.PageControl];
64
65
66 self.timerView = [NSTimer scheduledTimerWithTimeInterval:
3.0 target:self selector:@selector(Autoplayphoto) userInfo:nil repeats:YES];
67
68 }
69 -(
void)Autoplayphoto
70 {
71 CGFloat width =
self.view.bounds.size.width;
72 CGPoint offset =
self.ScrollView.contentOffset;
73
74 if (offset.x ==
5*
width)
75 {
76 offset.x = offset.x -
5*
width;
77 }
else
78 {
79 offset.x = offset.x +
width;
80 }
81
82 self.ScrollView.contentOffset =
offset;
83
84 int page = offset.x/width -
1;
85 if (page >
3) {
86 page =
0;
87 }
88 if (page <
0) {
89 page =
3;
90 }
91 self.PageControl.currentPage =
page;
92
93 self.titltLabel.text =
self.titleArray[page];
94 }
95
96
97 -(
void)scrollViewDidScroll:(UIScrollView *
)scrollView
98 {
99 CGFloat width =
self.view.bounds.size.width;
100 CGFloat offset =
self.ScrollView.contentOffset.x;
101 if (offset >=
5*
width)
102 {
103 self.ScrollView.contentOffset = CGPointMake(width,
0);
104 }
105 if (offset <=
0)
106 {
107 self.ScrollView.contentOffset = CGPointMake(
4*width,
0);
108 }
109 int page = offset/width -
1;
110 if (page >
3) {
111 page =
0;
112 }
113 if (page <
0) {
114 page =
3;
115 }
116 self.PageControl.currentPage =
page;
117 //NSLog(@"page = %d",page);
118 self.titltLabel.text =
self.titleArray[page];
119
120 }
121
122
123 - (
void)didReceiveMemoryWarning {
124 [super didReceiveMemoryWarning];
125 // Dispose of any resources that can be recreated.
126 }
127
128 @end
View Code
此题目是将循环播放的图片放入plist文件中,然后再读取出来显示。
1、plist文件读取方法
@property(strong,nonatomic)NSArray * contentArray;//contentArray数组用于存储plist文件中的内容
-(NSArray *)contentArray; //声明contentArray的getter方法
//实现contentArray的getter方法
-(NSArray *)contentArray
{
NSString * path = [[NSBundle mainBundle]pathForResource:@"photo.plist" ofType:nil]; //寻找plist文件路径,并保存于字符串path中
_contentArray = [NSArray arrayWithContentsOfFile:path]; //将plist文件中的图片内容保存于属性contentArray中
return _contentArray;
}
2、UIScrollController常用属性和常用方法
//设置偏移量
self.ScrollView.contentOffset = CGPointMake(width, 0);
//设置内容大小
self.ScrollView.contentSize = CGSizeMake(width*6, 313);
//此属性是当图片的中轴到达或超过偏移量的一半的地方时图像会自动滑动,否则图片将返回滑动前的位置
self.ScrollView.pagingEnabled = YES;
//关闭水平滑动指示器,相对应的有设置垂直滑动指示器
self.ScrollView.showsHorizontalScrollIndicator = NO;
方法:
//只要图片滑动此方法就会被执行
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
//视图将要(开始)拖拽时该方法被执行
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
//拖拽结束时,该方法被执行
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
//自动滑动结束时,此方法被执行
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
3、UIPageControl
常用属性及方法
self.PageControl.numberOfPages = 4; //设置页的张数
self.PageControl.currentPage = 0; //设置初始页
//设置小白点颜色
[self.PageControl setPageIndicatorTintColor:[UIColor groupTableViewBackgroundColor]];
//设置当前页小点的颜色
[self.PageControl setCurrentPageIndicatorTintColor:[UIColor greenColor]];
4、NSTimer
//暂停计时器
- (void)pauseTimer:(NSTimer *)oTimer
{
[oTimer setFireDate:[NSDate distantFuture]];
}
//重启计时器
- (void)restartTimer:(NSTimer *)oTimer
{
[oTimer setFireDate:[NSDate date]];
}
//延时启动定时器
- (void)restartTimerAfterDelay:(NSTimeInterval)interval timer:(NSTimer *)oTimer
{
[oTimer setFireDate:[NSDate dateWithTimeInterval:interval sinceDate:[NSDate date]]];
}
//停止计时器
- (void)stopTimer:(NSTimer *)oTimer
{
[oTimer invalidate];
}
//此种方式创建的timer已经添加至runloop中
self.timerView = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(Autoplayphoto) userInfo:nil repeats:YES];
//此种方式没有将创建的timer添加至runloop中
timer = [NSTimer timerWithTimeInterval:timerInterval target:self selector:@selector(scrollMethod) userInfo:nil repeats:YES];
//因此需要将timer添加至runloop中
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
转载于:https://www.cnblogs.com/ylg-----/p/4760227.html
相关资源:数据结构—成绩单生成器