iOS引導頁

hither發表於2017-12-13

在我們專案中經常會用到引導頁,引導頁主要功能就是向使用者展示你的產品。

這是我寫的一個例子的效果圖(圖片是隨便找的):

iOS引導頁

在AppDelegate.m中:
我們需要兩個Viewcongtroller來實現;
myViewController是我的引導頁面檢視控制器
MainViewController是我們滑動完引導頁 點選按鈕以後進入的主頁面。

#import "AppDelegate.h"
#import "myViewController.h"
#import "MainViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSUserDefaults *useDef = [NSUserDefaults standardUserDefaults];
    // 使用 NSUserDefaults 讀取使用者資料
    if (![useDef boolForKey:@"notFirst"]) {
        // 如果是第一次進入引導頁
        _window.rootViewController = [[myViewController alloc] init];
    }
    else{
        // 否則直接進入應用
        _window.rootViewController = [[MainViewController alloc] init];
    }
    return YES;
}

複製程式碼
myViewController.m中:
#import "myViewController.h"
#import "MainViewController.h"

#define WIDTH (NSInteger)self.view.bounds.size.width
#define HEIGHT (NSInteger)self.view.bounds.size.height

@interface myViewController ()<UIScrollViewDelegate>
{
    // 建立頁碼控制器
    UIPageControl *pageControl;
    // 判斷是否是第一次進入應用
    BOOL flag;
}
@end

@implementation myViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    for (int i=0; i<3; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"Y%d.jpg",i+1]];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
        // 在最後一頁建立按鈕
        if (i == 2) {
            // 必須設定使用者互動 否則按鍵無法操作
            imageView.userInteractionEnabled = YES;
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(WIDTH / 3, HEIGHT * 7 / 8, WIDTH / 3, HEIGHT / 16);
            [button setTitle:@"點選進入" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.layer.borderWidth = 2;
            button.layer.cornerRadius = 5;
            button.clipsToBounds = YES;
            button.layer.borderColor = [UIColor whiteColor].CGColor;
            [button addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];
            [imageView addSubview:button];
        }
        imageView.image = image;
        [myScrollView addSubview:imageView];
    }
    myScrollView.bounces = NO;
    myScrollView.pagingEnabled = YES;
    myScrollView.showsHorizontalScrollIndicator = NO;
    myScrollView.contentSize = CGSizeMake(WIDTH * 3, HEIGHT);
    myScrollView.delegate = self;
    [self.view addSubview:myScrollView];
    
    pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 3, HEIGHT * 15 / 16, WIDTH / 3, HEIGHT / 16)];
    // 設定頁數
    pageControl.numberOfPages = 3;
    // 設定頁碼的點的顏色
    pageControl.pageIndicatorTintColor = [UIColor yellowColor];
    // 設定當前頁碼的點顏色
    pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    
    [self.view addSubview:pageControl];
}

#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    // 計算當前在第幾頁
    pageControl.currentPage = (NSInteger)(scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width);
}

// 點選按鈕儲存資料並切換根檢視控制器
- (void) go:(UIButton *)sender{
    flag = YES;
    NSUserDefaults *useDef = [NSUserDefaults standardUserDefaults];
    // 儲存使用者資料
    [useDef setBool:flag forKey:@"notFirst"];
    [useDef synchronize];
    // 切換根檢視控制器
    self.view.window.rootViewController = [[MainViewController alloc] init];
}
複製程式碼

相關文章