forked from cubewang/NewsReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuideViewController.m
117 lines (87 loc) · 3.32 KB
/
GuideViewController.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
// GuideViewController.m
// PageScrollSample
//
#import "GuideViewController.h"
#define SPACE_WIDTH 20
#define CONTENT_SIZEHEIGHT 480
#define CONTENT_NUM 2
#define CONTENT_SIZEWEIGHT 320
@implementation GuideViewController
@synthesize scrollView ;
@synthesize isChangeAction;
@synthesize pageControl;
- (void)dealloc
{
[scrollView release];
[pageControl release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.frame = [[UIScreen mainScreen] applicationFrame];
self.view.backgroundColor = GUIDE_VIEW_COLOR;
CGRect frame = CGRectMake(0, 0, CONTENT_SIZEWEIGHT, 480);
self.scrollView = [[[UIScrollView alloc] initWithFrame:frame] autorelease];
self.scrollView.clipsToBounds = YES;
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.delegate = self;
[self.view addSubview:self.scrollView];
NSInteger contentWidth = self.scrollView.frame.size.width - SPACE_WIDTH;
for (int i = 0; i < CONTENT_NUM; ++i) {
CGRect frame = {(contentWidth + SPACE_WIDTH) * i, 0, CONTENT_SIZEWEIGHT, CONTENT_SIZEHEIGHT};
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"guide%d", i] ofType:@"png"];
imageView.image = [[[UIImage alloc] initWithContentsOfFile:path] autorelease];
[self.scrollView addSubview:imageView];
[imageView release];
}
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(140, 400, 60, 30)]; //创建UIPageControl,位置在屏幕最下方
self.pageControl.numberOfPages = CONTENT_NUM;
self.pageControl.currentPage = 0;
[self.pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pageControl]; //将UIPageControl添加到主界面上。
[self.pageControl release];
self.scrollView.contentSize = CGSizeMake((contentWidth + SPACE_WIDTH) * CONTENT_NUM, CONTENT_SIZEHEIGHT);
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.scrollView = nil;
self.pageControl = nil;
}
- (void)loginAction {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:1.0];
[UIView commitAnimations];
[self.view removeFromSuperview];
}
- (void)back {
[self dismissModalViewControllerAnimated:YES];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)ascrollView
{
//更新UIPageControl的当前页
CGPoint offset = ascrollView.contentOffset;
CGRect bounds = ascrollView.frame;
[pageControl setCurrentPage:offset.x / bounds.size.width];
if (pageControl.currentPage == 1) {
if (isChangeAction) {
[self back];
}
else
[self loginAction];
}
}
- (void)pageTurn:(UIPageControl*)sender
{
//令UIScrollView做出相应的滑动显示
CGSize viewSize = scrollView.frame.size;
CGRect rect = CGRectMake(sender.currentPage * viewSize.width, 0, viewSize.width, viewSize.height);
[scrollView scrollRectToVisible:rect animated:YES];
}
@end