一、新特性界面简介:
iOS中几乎所有的软件在我们第一次安装后打开或者版本跟新时都会再启动时显示几幅图片来介绍新版本的特性。那么,今天就来介绍一下如何实现这种新特性的效果。
二、实现效果如下:
三、是否显示新特性界面的思路和实现。
a)取出Info.plist文件中得版本号<span style="font-weight: normal;"><span style="font-size:12px;"><span style="white-space:pre"> </span>NSString *versionKey = (NSString *)kCFBundleVersionKey;
//说明:在CoreFoundation框架的CFBundle.h头文件中定义了一些宏,是Info.plist文件(字典)中保存的属性的key值。
NSString *version = [NSBundle mainBundle].infoDictionary[versionKey];
//说明:NSBundle类中有一个实力方法读取Info.plist文件,保存到一个字典中。</span></span>
<span style="font-weight: normal;"><span style="font-size:12px;"><span style="white-space:pre"> </span>NSString *versionKey = (NSString *)kCFBundleVersionKey;
//说明:在CoreFoundation框架的CFBundle.h头文件中定义了一些宏,是Info.plist文件(字典)中保存的属性的key值。
NSString *version = [NSBundle mainBundle].infoDictionary[versionKey];
//说明:NSBundle类中有一个实力方法读取Info.plist文件,保存到一个字典中。</span></span>
b)从沙盒中取出记录的版本号
<span style="font-size:12px;"><span style="white-space:pre"> </span>NSString *saveVersion = [[NSUserDefaults standardUserDefaults] objectForKey:versionKey];
<span style="white-space:pre"> </span>//说明:从偏好设置中取出保存的版本号。第一次使用应用程序取出为空,如何保存请参照d)步骤。</span>
c)两个版本号是否相同,不相同则显示新特性界面,相同则跳入主界面。
<span style="font-size:12px;"><span style="white-space: pre;"> </span>if ([version isEqualToString:saveVersion]) {
// 并非第一次使用此版本,跳入主界面
self.window.rootViewController = [[MainViewController alloc] init];
} else {
// 第一次使用此版本,跳入新特性界面
d)将版本信息写入沙盒。
[[NSUserDefaults standardUserDefaults] setObject:version forKey:versionKey];
[[NSUserDefaults standardUserDefaults] synchronize];
//新特性显示
self.window.rootViewController = [[NewFeatureViewController alloc] init];
}</span>
四、新特性控制器的设计思路和实现。
1、上图所展示的页面中可以左右滑动,主界面是UIScrollView,下面有一个显示当前页码的为UIPageControl。 <span style="font-size:12px;">#define kCount 4
@interface NewfeatureController () <UIScrollViewDelegate>
{
UIPageControl *_page;
UIScrollView *_scroll;
}
@end
@implementation NewfeatureController
#pragma mark 自定义view
- (void)loadView
{
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage fullscrennImage:@"new_feature_background.png"];
imageView.frame = [UIScreen mainScreen].applicationFrame;
// 设置控件可以和用户交互(这样才能接受触摸事件)。
imageView.userInteractionEnabled = YES;
self.view = imageView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 1.添加UIScrollView
[self addScrollView];
// 2.添加图片
[self addScrollImages];
// 3.添加UIPageControl
[self addPageControl];
}
#pragma mark - UI界面初始化
#pragma mark 添加滚动视图
- (void)addScrollView
{
UIScrollView *scroll = [[UIScrollView alloc] init];
scroll.frame = self.view.bounds;
scroll.showsHorizontalScrollIndicator = NO; // 隐藏水平滚动条
CGSize size = scroll.frame.size;
scroll.contentSize = CGSizeMake(size.width * kCount, 0); // 内容尺寸
scroll.pagingEnabled = YES; // 分页模式
scroll.delegate = self;
[self.view addSubview:scroll];
_scroll = scroll;
}
#pragma mark 添加滚动显示的图片
- (void)addScrollImages
{
CGSize size = _scroll.frame.size;
for (int i = 0; i<kCount; i++) {
// 1.显示图片
NSString *name = [NSString stringWithFormat:@"new_feature_%d.png", i + 1];
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:name];
// 2.设置frame
imageView.frame = CGRectMake(i * size.width, 0, size.width, size.height);
[_scroll addSubview:imageView];
}
}
#pragma mark 添加分页指示器
- (void)addPageControl
{
CGSize size = self.view.frame.size;
UIPageControl *page = [[UIPageControl alloc] init];
page.center = CGPointMake(size.width * 0.5, size.height * 0.95);
page.numberOfPages = kCount;
page.currentPageIndicatorTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"new_feature_pagecontrol_checked_point.png"]];
page.pageIndicatorTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"new_feature_pagecontrol_point.png"]];
page.bounds = CGRectMake(0, 0, 150, 0); [self.view addSubview:page]; _page = page;
}
#pragma mark - 滚动代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
_page.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width;
}</span>
<span style="font-size:12px;">#define kCount 4
@interface NewfeatureController () <UIScrollViewDelegate>
{
UIPageControl *_page;
UIScrollView *_scroll;
}
@end
@implementation NewfeatureController
#pragma mark 自定义view
- (void)loadView
{
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage fullscrennImage:@"new_feature_background.png"];
imageView.frame = [UIScreen mainScreen].applicationFrame;
// 设置控件可以和用户交互(这样才能接受触摸事件)。
imageView.userInteractionEnabled = YES;
self.view = imageView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 1.添加UIScrollView
[self addScrollView];
// 2.添加图片
[self addScrollImages];
// 3.添加UIPageControl
[self addPageControl];
}
#pragma mark - UI界面初始化
#pragma mark 添加滚动视图
- (void)addScrollView
{
UIScrollView *scroll = [[UIScrollView alloc] init];
scroll.frame = self.view.bounds;
scroll.showsHorizontalScrollIndicator = NO; // 隐藏水平滚动条
CGSize size = scroll.frame.size;
scroll.contentSize = CGSizeMake(size.width * kCount, 0); // 内容尺寸
scroll.pagingEnabled = YES; // 分页模式
scroll.delegate = self;
[self.view addSubview:scroll];
_scroll = scroll;
}
#pragma mark 添加滚动显示的图片
- (void)addScrollImages
{
CGSize size = _scroll.frame.size;
for (int i = 0; i<kCount; i++) {
// 1.显示图片
NSString *name = [NSString stringWithFormat:@"new_feature_%d.png", i + 1];
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:name];
// 2.设置frame
imageView.frame = CGRectMake(i * size.width, 0, size.width, size.height);
[_scroll addSubview:imageView];
}
}
#pragma mark 添加分页指示器
- (void)addPageControl
{
CGSize size = self.view.frame.size;
UIPageControl *page = [[UIPageControl alloc] init];
page.center = CGPointMake(size.width * 0.5, size.height * 0.95);
page.numberOfPages = kCount;
page.currentPageIndicatorTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"new_feature_pagecontrol_checked_point.png"]];
page.pageIndicatorTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"new_feature_pagecontrol_point.png"]];
page.bounds = CGRectMake(0, 0, 150, 0); [self.view addSubview:page]; _page = page;
}
#pragma mark - 滚动代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
_page.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width;
}</span>