代码改变世界

ios中开始页面做法

2013-07-02 20:43  甘超波  阅读(1370)  评论(0编辑  收藏  举报

 

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    
    id key = (id)kCFBundleVersionKey;
    
    // 检测是否第一次使用这个版本
    
    // 新浪微博-Info.plist
    NSDictionary *info = [NSBundle mainBundle].infoDictionary;
    // 获取当前软件的版本号
    NSString *currentVersion = [info objectForKey:key];
    
    // 从沙盒中取出版本号
    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    NSString *saveVersion = [defaults objectForKey:key];
    
    // 不是第一次使用这个版本
    if ([currentVersion isEqualToString:saveVersion]) {
        
        //MJLog(@"不是第一次使用这个版本");
        // 显示状态栏
        application.statusBarHidden = NO;
        
        IndexController *index = [[[IndexController alloc] init] autorelease];
        self.window.rootViewController = index;
        
    } else { // 第一次使用这个版本
        // 更新沙盒中的版本号
        [defaults setObject:currentVersion forKey:key];
        // 同步到沙盒中
        [defaults synchronize];
        
        //MJLog(@"第一次使用这个版本");
        
        // 显示版本新特性控制器
        NewFeatureController *new = [[[NewFeatureController alloc] init] autorelease];
        self.window.rootViewController = new;
    }

    [self.window makeKeyAndVisible];
    return YES;
}

 

 

 

#define kNewFeatureListName @"new_feature"
#define kNewFeatureListExt @"plist"

#import "NewFeatureController.h"
#import "IndexController.h"
#import <QuartzCore/QuartzCore.h>

@interface NewFeatureController ()
@end

@implementation NewFeatureController
#pragma mark - 按钮点击
- (void)start {
    UIApplication *app = [UIApplication sharedApplication];
    // 显示状态栏
    app.statusBarHidden = NO;
    
    IndexController *index = [[IndexController alloc] init];
    
    // 设置窗口的根控制器
    app.keyWindow.rootViewController = index;
    
    [index release];
    
    // 执行动画
    CATransition *anim = [CATransition animation];
    anim.duration = 0.4;
    anim.type = kCATransitionPush;
    anim.subtype = kCATransitionFromRight;
    [app.keyWindow.layer addAnimation:anim forKey:nil];
}

#pragma mark - 生命周期方法
- (void)loadView {
    // 不需要调用super的loadView
    
    // self.view默认是nil的,一旦发现view是nil,就会调用loadView方法大创建view
    // 这里会造成死循环
    //self.view = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    
    // 控制器的view最适合的frame
    CGRect appFrame = [UIScreen mainScreen].applicationFrame;
    
    // 建议少用autorelease
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:appFrame];
    self.view = scrollView;
    [scrollView release];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 获取new_feature.plist的路径
    NSString *path = [[NSBundle mainBundle] pathForResource:kNewFeatureListName ofType:kNewFeatureListExt];
    // 加载new_feature.plist
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    int count = array.count;
    
    // 设置scrollView的内容宽度
    UIScrollView *scrollView = (UIScrollView *)self.view;
    scrollView.contentSize = CGSizeMake(count * scrollView.bounds.size.width, 0);
    
    // 设置分页
    scrollView.pagingEnabled = YES;
    // 隐藏水平的滚动条
    scrollView.showsHorizontalScrollIndicator = NO;
    // 去除弹簧效果
    scrollView.bounces = NO;
    
    for (int index = 0; index< count; index++) {
        // 获取图片名称
        NSString *imgName = [array objectAtIndex:index];
        // 加载图片
        UIImage *image = [UIImage imageNamed:imgName];
        
        // 创建UIImageView
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        
        // 设置ImageView的x
        CGRect frame =  imageView.frame;
        frame.origin.x = index * image.size.width;
        imageView.frame = frame;
        
        [self.view addSubview:imageView];
        [imageView release];
        
        // 添加"开始微博"按钮
        if (index == count - 1) {
            // 让UIImageView跟用户进行交互(接收触摸事件)
            imageView.userInteractionEnabled = YES;
            
            // 创建按钮
            UIButton *btn = [[UIButton alloc] init];
            btn.bounds = CGRectMake(0, 0, 100, 35);
            btn.center = CGPointMake(image.size.width * 0.5f, image.size.height - 100);
            
            // 设置默认和高亮背景
            [btn setNormalBg:@"new_features_startbutton_background.png" andHighlighted:@"new_features_startbutton_background_highlighted.png"];
            
            // 设置按钮文字
            NSString *title = NSLocalizedString(@"开始微博", nil);
            [btn setTitle:title forState:UIControlStateNormal];
            
            // 设置按钮文字颜色
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            
            // 设置按钮文字大小
            btn.titleLabel.font = [UIFont systemFontOfSize:16];
            
            [imageView addSubview:btn];
            
            // 监听按钮
            [btn addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
            
            [btn release];
        }
    }
}

@end