码迷,mamicode.com
首页 > 其他好文 > 详细

第一次加载应用时的滑动启动页

时间:2015-12-11 14:52:15      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

首先新建一个类:SplashViewController

还是直接贴代码: .h文件

#import <UIKit/UIKit.h>
#import "UIButton+Bootstrap.h"

@interface SplashViewController : UIViewController{
    

}
//页数量
@property (nonatomic,assign)  NSInteger pagecount;
//是否动画
@property (nonatomic, assign) BOOL isanimating;
//滑动页视图
@property (nonatomic, strong) UIScrollView  *pageScroll;
//是否显示按钮
@property (nonatomic,retain)  UIButton* enter_button;

//添加切换页
-(void)addpage:(NSString*)imagename;

//添加进入按钮
-(void)addenterbutton:(NSString*)title imgNor:(NSString*)imgnor imgPress:(NSString*)imgpress;


//显示
- (void)show;

//隐藏
- (void)hide;

//单例
+(SplashViewController*) sharedSplashViewController;

//获取屏幕大小
-(CGRect) getscreenFrame;

@end

.m文件:

#import "SplashViewController.h"
//#import "RailwayClientApplication.h"

@interface SplashViewController ()

@end

@implementation SplashViewController

@synthesize isanimating = _isanimating;
@synthesize pageScroll  = _pageScroll;
@synthesize enter_button;
@synthesize pagecount;


static SplashViewController *instance = nil;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self){
        self.view.frame = CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height);
        self.view.backgroundColor = [UIColor whiteColor];
        self.pagecount=0;
        

        //[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    }
    return self;
}

//添加切换页
-(void)addpage:(NSString*)imagename{
    
    UIImageView      *view;
    view = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.frame.size.width * pagecount), 0, self.view.frame.size.width, Main_Screen_Height)];
    UIImage *img=[UIImage imageNamed:imagename];
    view.image = img;
    view.userInteractionEnabled = YES;
//    UIEdgeInsets insets = UIEdgeInsetsMake(10, 10, 400, 10);
//    img=[img resizableImageWithCapInsets:insets];
//    view.backgroundColor = [UIColor colorWithPatternImage:img];
    
//    view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//    view.contentMode = UIViewContentModeScaleAspectFill;
//    
    
    [self.pageScroll addSubview:view];
    pagecount++;
    self.pageScroll.contentSize = CGSizeMake(self.view.frame.size.width * pagecount, Main_Screen_Height);
}

//添加进入按钮
-(void)addenterbutton:(NSString*)title imgNor:(NSString*)imgnor imgPress:(NSString*)imgpress{
    enter_button= [UIButton buttonWithType:UIButtonTypeSystem];
    self.enter_button.frame=CGRectMake(0.f, 0.f, 175.f, 35.f);
    [self.enter_button setTitle:title forState:UIControlStateNormal];
    [self.enter_button orangerStyle];
//    [enter_button setTitle:title forState:UIControlStateNormal];
//    [enter_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [enter_button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [self.enter_button setCenter:CGPointMake(self.view.center.x, Main_Screen_Height - 70)];
//    [enter_button setBackgroundImage:[UIImage imageNamed:imgnor] forState:UIControlStateNormal];
//    [enter_button setBackgroundImage:[UIImage imageNamed:imgpress] forState:UIControlStateHighlighted];
    [self.enter_button addTarget:self action:@selector(pressEnterButton:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *uiview=[self.pageScroll.subviews objectAtIndex:self.pageScroll.subviews.count-1];
    if(self.enter_button!=nil){
        [self.enter_button addTarget:self action:@selector(pressEnterButton:) forControlEvents:UIControlEventTouchUpInside];
        [uiview addSubview:self.enter_button];
    }
}

//加载视图
- (void)viewDidLoad
{
    [super viewDidLoad];
    CGRect frame=[UIScreen mainScreen].bounds;
    
    if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) {
//        [self.navigationBar setBarTintColor:[UIColor redColor]];
        [self.navigationController.navigationBar setTranslucent:NO];
    }
    _pageScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    self.pageScroll.pagingEnabled = YES;
    [self.view addSubview:self.pageScroll];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}



//单例
+(SplashViewController*) sharedSplashViewController{
    @synchronized(self){
        if(instance==nil){
            instance=[[SplashViewController alloc]init];
        }
    }
    return instance;
}

//显示
- (void)show{
    [self.pageScroll setContentOffset:CGPointMake(0.f, 0.f)];
    [self showPage];
}

//隐藏
- (void)hide{
    [self hidePage];
}

//获取屏幕大小
-(CGRect) getscreenFrame{
    return  CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height);//[UIScreen mainScreen].applicationFrame;
}



//偏移屏幕
- (CGRect)offscreenFrame
{
    CGRect frame = [self getscreenFrame];
    switch ((NSInteger)[UIApplication sharedApplication].statusBarOrientation)
    {
        case UIInterfaceOrientationPortrait:
            frame.origin.y = frame.size.height;
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            frame.origin.y = -frame.size.height;
            break;
        case UIInterfaceOrientationLandscapeLeft:
            frame.origin.x = frame.size.width;
            break;
        case UIInterfaceOrientationLandscapeRight:
            frame.origin.x = -frame.size.width;
            break;
    }
    return frame;
}

//停止动画
- (void)stopAnimation
{
    _isanimating = NO;
}

//进入事件
- (void)pressEnterButton:(UIButton *)enterButton
{
    [self hidePage];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isnofirst"];
    
//    [[NSUserDefaults standardUserDefaults] setInteger:2 forKey:@"skin_type"];
//    [[NSUserDefaults standardUserDefaults] setValue:G_APPVERSION forKey:@"platform_type"];
//    [[NSUserDefaults standardUserDefaults] synchronize];
//    [RailwayClientApplication getInstance].appsetings.platform_type=G_APPVERSION;
////    [RailwayClientApplication getInstance].appsetings.skin_type=2;
//    
//    [self dismissModalViewControllerAnimated:YES];

//    [RailwayClientApplication showLoginViewController];
}


//从视图中移除
- (void)subviewHidden
{
    _isanimating = NO;
    [[[SplashViewController sharedSplashViewController] view] removeFromSuperview];
}

//显示页面
- (void)showPage
{
    if (!_isanimating && self.view.superview == nil)
    {
        [SplashViewController sharedSplashViewController].view.frame = [self offscreenFrame];
        //[[RailwayClientApplication getMainWindow] addSubview:[SplashViewController sharedSplashViewController].view];
        [[self getMainWindow] addSubview:[SplashViewController sharedSplashViewController].view];
        _isanimating = YES;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(stopAnimation)];
        [SplashViewController sharedSplashViewController].view.frame = [self getscreenFrame];
        [UIView commitAnimations];
    }
}
//获取主窗口
- (UIWindow *)getMainWindow
{
    UIApplication *app = [UIApplication sharedApplication];
    if ([app.delegate respondsToSelector:@selector(window)])
    {
        return [app.delegate window];
    }
    else
    {
        return [app keyWindow];
    }
}

//隐藏
- (void)hidePage
{
    if (!_isanimating && self.view.superview != nil)
    {
        _isanimating = YES;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.4];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(subviewHidden)];
        [SplashViewController sharedSplashViewController].view.frame = [self offscreenFrame];
        [UIView commitAnimations];
    }
}

@end

在要引用启动页的类中插入一下代码:

//显示快显视图
-(void)showSplashViewController{
    //第一次启动程序的过场动画
    if ( [[NSUserDefaults standardUserDefaults] boolForKey:@"isnofirst"] == NO) {
        //添加切换页面
        [[SplashViewController sharedSplashViewController]addpage:@"ydy_1.jpg"];
        [[SplashViewController sharedSplashViewController]addpage:@"ydy_2.jpg"];
        [[SplashViewController sharedSplashViewController]addpage:@"ydy_3.jpg"];
        [[SplashViewController sharedSplashViewController]addpage:@"ydy_4.jpg"];
        //添加进入按钮
        [[SplashViewController sharedSplashViewController]addenterbutton:@"开始进入" imgNor:@"btn_nor" imgPress:@"btn_press"];
        [[SplashViewController sharedSplashViewController] show];
    }
}

 

第一次加载应用时的滑动启动页

标签:

原文地址:http://www.cnblogs.com/h-tao/p/5038826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!