风言枫语  
//
//  GuideViewController.h
//  Guide
//
//  Created by twb on 13-9-17.
//  Copyright (c) 2013年 twb. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface GuideViewController : UIViewController <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSMutableArray *images;

+ (GuideViewController *)shareInstance;
+ (void)show;
+ (void)hide;

@end

 

//
//  GuideViewController.m
//  Guide
//
//  Created by twb on 13-9-17.
//  Copyright (c) 2013年 twb. All rights reserved.
//

#import "GuideViewController.h"

@interface GuideViewController ()

@property (nonatomic, assign) BOOL animating;

@end

@implementation GuideViewController

+ (GuideViewController *)shareInstance
{
    @synchronized(self)
    {
        static GuideViewController *sharedGuide = nil;
        if (sharedGuide == nil)
        {
            sharedGuide = [[self alloc] initWithNibName:@"GuideViewController" bundle:nil];
        }
        return sharedGuide;
    }
}

+ (void)show
{
    [[GuideViewController shareInstance].scrollView setContentOffset:CGPointMake(0.0f, 0.0f)];
	[[GuideViewController shareInstance] showGuide];
}

+ (void)hide
{
	[[GuideViewController shareInstance] hideGuide];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = kClearColor;
    
    // Do any additional setup after loading the view from its nib.
    [self setupContent];
    [self setupScrollView];
    [self setupPageControl];
    [self setupScrollViewContent];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload
{
    [self setScrollView:nil];
    [self setPageControl:nil];
    [super viewDidUnload];
}

#pragma mark - setup part.

- (void)setupContent
{
    // These images retrieve from server. it is empty for just now.
    self.images = [NSMutableArray arrayWithArray:@[@"Default.png", @"Default.png", @"Default.png", @"Default.png"]];
}

- (void)setupScrollView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kScreenWidth, kScreenHeight - kScreenStatusBarHeight)];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.backgroundColor = kClearColor;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
//    self.scrollView.backgroundColor = kOrangeColor;
    self.scrollView.contentSize = CGSizeMake(kScreenWidth * self.images.count, kScreenHeight - kScreenStatusBarHeight);
    [self.view addSubview:self.scrollView];
}

- (void)setupPageControl
{
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0.0f, self.scrollView.frame.size.height - 30.0f, kScreenWidth, 30.0f)];
    self.pageControl.hidesForSinglePage = YES;
    self.pageControl.numberOfPages = self.images.count;
    [self.pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.pageControl];
}

- (void)setupScrollViewContent
{
    for (NSInteger i = 0; i < self.images.count; i++)
    {
        UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(i * kScreenWidth, 0.0f, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
        iv.userInteractionEnabled = YES;
        iv.image = kImageNamed(self.images[i]);
        if (i % 2)
        {
            iv.backgroundColor = kLightGrayColor;
        }
        else
        {
            iv.backgroundColor = kGrayColor;
        }
        [self.scrollView addSubview:iv];
        
        if (i == self.images.count - 1)
        {
            // the end page.
            [iv addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(enter:)]];
        }
    }
}

#pragma mark - Common part.

- (CGRect)onScreenFrame
{
	return [UIScreen mainScreen].applicationFrame;
}

- (CGRect)offScreenFrame
{
	CGRect frame = [self onScreenFrame];
	switch ([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;
}

- (UIWindow *)mainWindow
{
    UIApplication *app = [UIApplication sharedApplication];
    if ([app.delegate respondsToSelector:@selector(window)])
    {
        return [app.delegate window];
    }
    else
    {
        return [app keyWindow];
    }
}

#pragma mark - Event part.

- (void)enter:(UITapGestureRecognizer *)sender
{
    [self hideGuide];
    self.dataManager.defaults.firstLaunch = YES;
}

- (void)showGuide
{
    if (!self.animating)
    {
        [GuideViewController shareInstance].view.frame = [self offScreenFrame];
        [[self mainWindow] addSubview:[GuideViewController shareInstance].view];
        self.animating = YES;
        [UIView animateWithDuration:0.25f animations:^{
            [GuideViewController shareInstance].view.frame = [self onScreenFrame];
        } completion:^(BOOL finished) {
            self.animating = NO;
        }];
    }
}

- (void)hideGuide
{
    if (!self.animating)
    {
        self.animating = YES;
        [UIView animateWithDuration:0.40f animations:^{
#if 0
            [GuideViewController shareInstance].view.frame = [self offScreenFrame];
#else
            [GuideViewController shareInstance].view.transform = CGAffineTransformMakeScale(1.25f, 1.25f);
            [GuideViewController shareInstance].view.alpha = 0.0f;
#endif
        } completion:^(BOOL finished) {
            [[GuideViewController shareInstance].view removeFromSuperview];
            self.animating = NO;
        }];
    }
}

- (void)pageChanged:(UIPageControl *)sender
{
    NSInteger page = sender.currentPage;
    [self.scrollView setContentOffset:CGPointMake(page * kScreenWidth, 0.0f) animated:YES];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat dx = scrollView.contentOffset.x;
    NSInteger page = dx / kScreenWidth;
    
    self.pageControl.currentPage = page;
}

@end


如何使用?

 

 "AppDelegate.m"文件中,

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    
    // ...
    
    // Show Guide?
    if (!self.dataManager.defaults.firstLaunch)
    {
        [GuideViewController show];
    }
    
    return YES;
}


 


 

posted on 2013-09-17 22:22  风言枫语  阅读(262)  评论(0编辑  收藏  举报