我们在第一次打开App的时候,通常不是直接进入App主界面,而是会有一个能左右滑动、介绍App功能的界面。我是用NSUserDefaults + UIScrollview实现。
新建一个类,继承UIView,假设名为Guide。在initWithFrame方法里:
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:frame];
scrollView.backgroundColor = [UIColor whiteColor];
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.contentSize = CGSizeMake(320*4, screenHeight);
scrollView.pagingEnabled = YES;
for (int i=0; i<4; i++) {
UIImageView* imageView = [[UIImageView alloc initWithFrame:CGRectMake(i*320, 0, 320, screenHeight)];
imageView.contentMode = UIViewContentModeScaleAspectFill;
NSString *filePath = [[NSBundle mainBundle] pathForResource:
[NSString stringWithFormat:@"FileName"
ofType:@"FileType"];
imageView.image = [UIImage imageWithContentsOfFile:filePath];
[scrollView addSubview:imageView];
if (i == 3) {
UIButton* start = [UIButton buttonWithType:UIButtonTypeCustom];
start.frame = CGRectMake(0, 0, 100, 44);
start.layer.cornerRadius = 5;
start.layer.borderWidth = 0.5;
[start setCenter:CGPointMake(1120, iPhone5?450:400)];
[start setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[start addTarget:self action:@selector(closeView) forControlEvents:UIControlEventTouchUpInside];
[start setTitle:@"Start" forState:UIControlStateNormal];
[scrollView addSubview:start];
}
这样,就有了一个有4张图片的引导页。
怎么去判断是不是第一次登陆呢,需要用到NSUserDefaults类。
在didFinishLaunchingWithOptions:函数中可以这样判断:
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
if([userDefaults objectForKey:@"FirstLoad"] == nil) {
[userDefaults setBool:NO forKey:@"FirstLoad"];
//显示引导页
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。