当前位置:首页 » 编程语言

【新浪微博项目】09--设置启动的时候版本新特性

2015-04-10 17:28 本站整理 浏览(122)

1.启动的时候设置版本新特性的根控制器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    NSString *key = @"CFBundleVersion";
    
    // 取出沙盒中存储的上次使用软件的版本号
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *lastVersion = [defaults stringForKey:key];
    
    // 获得当前软件的版本号
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
    
    if ([currentVersion isEqualToString:lastVersion]) {
        // 显示状态栏
        application.statusBarHidden = NO;
        
        self.window.rootViewController = [[MRRootTabBarController alloc] init];
    } else { // 新版本
        self.window.rootViewController = [[MRNewFeatureController alloc] init];
        // 存储新版本
        [defaults setObject:currentVersion forKey:key];
        [defaults synchronize];
    }
    [self.window makeKeyAndVisible];
    return YES;
}

2.自定义版本新特性的Controller

#import "MRNewFeatureController.h"
#import "UIImage+MJ.h"
#import "Header.h"
#import "MRRootTabBarController.h"
#define NEWFEATURE_IMAGE_NUMBER 3
@interface MRNewFeatureController ()<UIScrollViewDelegate>
@property (nonatomic, weak) UIPageControl *pageControl;
@end

@implementation MRNewFeatureController

- (void)viewDidLoad {
    [super viewDidLoad];
    //1.添加ScrollView
    [self setNewFeatureScrollView];
    // 2.添加pageControl
    [self setupPageControl];
    // Do any additional setup after loading the view.
}
//1.添加ScrollView
-(void)setNewFeatureScrollView
{
    //0.添加一个背景
    UIImageView *imageBackView = [[UIImageView alloc]init];
    [imageBackView setImage:[UIImage imageWithName:@"new_feature_background"]];
    //如何不设置frame属性就无法显示View
    [imageBackView setFrame:self.view.bounds];
    [self.view addSubview:imageBackView];
	
    //1.添加一个ScrollView
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.frame = self.view.bounds;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
	
    //2.UIScrollView添加图片
    CGFloat imageViewW = scrollView.bounds.size.width;
    CGFloat imageViewH = scrollView.bounds.size.height;
    
    for (int i = 0 ; i < NEWFEATURE_IMAGE_NUMBER; ++i) {
        UIImageView *imageView = [[UIImageView alloc]init];
        //设置图片
        NSString *strImage = [NSString stringWithFormat:@"new_feature_%d",i+1];
        imageView.image = [UIImage imageWithName:strImage];
        //设置frame
        CGFloat imageViewX = i * imageViewW;
        imageView.frame = CGRectMake(imageViewX, 0, imageViewW, imageViewH);
        
        [scrollView addSubview:imageView];
        
        // 在最后一个图片上面添加按钮
        if (i == NEWFEATURE_IMAGE_NUMBER - 1) {
            [self setupLastImageView:imageView];
        }
    }
    //3.设置滚动的内容尺寸
    scrollView.contentSize = CGSizeMake(NEWFEATURE_IMAGE_NUMBER*imageViewW, imageViewH);
    //不显示水平滚动条
    scrollView.showsHorizontalScrollIndicator = NO;
    //支持分页属性
    scrollView.pagingEnabled = YES;
    //到边缘的时候无法缓冲
    //scrollView.bounces = NO;

}
// 2.添加pageControl
- (void)setupPageControl
{
    // 1.添加
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = NEWFEATURE_IMAGE_NUMBER;
    CGFloat centerX = self.view.frame.size.width * 0.5;
    CGFloat centerY = self.view.frame.size.height - 30;
    pageControl.center = CGPointMake(centerX, centerY);
    pageControl.bounds = CGRectMake(0, 0, 100, 30);
    pageControl.userInteractionEnabled = NO;
    [self.view addSubview:pageControl];
    self.pageControl = pageControl;
    
    // 2.设置圆点的颜色
    pageControl.currentPageIndicatorTintColor = MRColor(253, 98, 42);
    pageControl.pageIndicatorTintColor = MRColor(189, 189, 189);
}
/**
 *  添加内容到最后一个图片
 */
- (void)setupLastImageView:(UIImageView *)imageView
{
    // 0.让imageView能跟用户交互
    imageView.userInteractionEnabled = YES;
    
    // 1.添加开始按钮
    UIButton *startButton = [[UIButton alloc] init];
    [startButton setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [startButton setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    
    // 2.设置frame
    CGFloat centerX = imageView.frame.size.width * 0.5;
    CGFloat centerY = imageView.frame.size.height * 0.6;
    startButton.center = CGPointMake(centerX, centerY);
    startButton.bounds = (CGRect){CGPointZero, startButton.currentBackgroundImage.size};
    
    // 3.设置文字
    [startButton setTitle:@"开始微博" forState:UIControlStateNormal];
    [startButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [startButton addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:startButton];
    
    // 4.添加checkbox
    UIButton *checkbox = [[UIButton alloc] init];
    checkbox.selected = YES;
    [checkbox setTitle:@"分享给大家" forState:UIControlStateNormal];
    [checkbox setImage:[UIImage imageWithName:@"new_feature_share_false"] forState:UIControlStateNormal];
    [checkbox setImage:[UIImage imageWithName:@"new_feature_share_true"] forState:UIControlStateSelected];
    checkbox.bounds = CGRectMake(0, 0, 200, 50);
    CGFloat checkboxCenterX = centerX;
    CGFloat checkboxCenterY = imageView.frame.size.height * 0.5;
    checkbox.center = CGPointMake(checkboxCenterX, checkboxCenterY);
    [checkbox setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    checkbox.titleLabel.font = [UIFont systemFontOfSize:15];
    [checkbox addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside];
    //按钮里面文字四周留下的距离
    //    checkbox.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    //按钮里面图片四周留下的距离
    checkbox.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);
    //按钮里面的内容距离按钮四周的长度
    //    checkbox.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    [imageView addSubview:checkbox];
}

/**
 *  开始微博
 */
- (void)start
{
    // 显示状态栏
    [UIApplication sharedApplication].statusBarHidden = NO;
    // 切换窗口的根控制器
    self.view.window.rootViewController = [[MRRootTabBarController alloc] init];
}

- (void)checkboxClick:(UIButton *)checkbox
{
    checkbox.selected = !checkbox.isSelected;
}

/**
 *  只要UIScrollView滚动了,就会调用
 *
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 1.取出水平方向上滚动的距离
    CGFloat offsetX = scrollView.contentOffset.x;
    
    // 2.求出页码
    double pageDouble = offsetX / scrollView.frame.size.width;
    int pageInt = (int)(pageDouble + 0.5);//四舍五入
    self.pageControl.currentPage = pageInt;
}

@end

3.结果图