使用 UIScrollView 创建新手引导界面

老牛浏览 389评论 0发表于

步骤

  1. 创建一个 UIScrollView 对象,添加 UIScrollViewDelegate 代理

  2. 加入引导图片

  3. 设置 scrollView 内容大小(contentSize),开启分页(scrollView.pagingEnable = YES)

  4. 创建 pageControl,设置分页数(numberOfPages)以及当前页(currentPage)

  5. 添加按钮跳转和代理方法

代码

//
//  NiuViewController.m
//  UIScrollView创建新手引导界面
//
//  Created by ilaoniu on 15-7-9.
//  Copyright (c) 2015年 ilaoniu. All rights reserved.
//

#import "NiuViewController.h"

@interface NiuViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIPageControl *pageControl;
@end

@implementation NiuViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    CGRect ScreenRect = [[UIScreen mainScreen]bounds];
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenRect.size.width, ScreenRect.size.height)];
    scrollView.delegate = self;
    [self.view addSubview:scrollView];

    for (int i=0; i<4; i++) {
        UIImage *backImage = [UIImage imageNamed:@"guide_bg"];
        UIImageView *backImageView = [[UIImageView alloc]initWithImage:backImage];
        backImageView.frame = CGRectMake(320*i, 0, 320, 480);
        [scrollView addSubview:backImageView];

        NSString *string = [NSString stringWithFormat:@"guideView_%i",i+1];
        UIImage *forImage = [UIImage imageNamed:string];
        UIImageView *forImageView = [[UIImageView alloc]initWithImage:forImage];
        forImageView.frame = CGRectMake(320*i, 0, 320, 480);
        [scrollView addSubview:forImageView];
    }

    scrollView.contentSize = CGSizeMake(320*4, 0);
    scrollView.pagingEnabled = YES;

    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(100, 450, 120, 0)];
    self.pageControl.numberOfPages = 4;
    self.pageControl.currentPage = 0;
    [self.view addSubview:self.pageControl];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100+320*3, 420, 120, 40);
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:button];

}

-(void)buttonClicked:(UIButton *)button{
    NSLog(@"新手引导完成,进入应用了!");
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int gap = scrollView.contentOffset.x/320;
    self.pageControl.currentPage = gap;
}

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

@end
点赞
收藏
暂无评论,快来发表评论吧~
私信
老牛@ilaoniu
老牛,俗称哞哞。单纯的九零后理工小青年。喜欢折腾,爱玩,爱音乐,爱游戏,爱电影,爱旅游...
最后活跃于