iOS_ 用户引导界面代码

本文介绍了一种iOS应用中实现用户引导视图的方法。通过UIScrollView实现页面切换,并结合UIPageControl显示页面指示器,同时利用手势识别实现特定操作。文章详细展示了如何设置UIScrollView的属性以实现平滑翻页效果,以及如何添加UIPageControl来指示当前页面。
摘要由CSDN通过智能技术生成

我们在首次安装一个软件时都有用户引导,这里就做了一个用户引导的demo

#import "DemoViewController.h"
#import "MainViewController.h"
#define kScreenWidth   [[UIScreen mainScreen]bounds].size.width
#define  kScreenHeigth  [[UIScreen mainScreen]bounds].size.height
#define kImageCount   4
#define kImageName  @"information"
#define kScrollViewTag 101
#define kPageControlTag 102

@interface DemoViewController () <UIScrollViewDelegate>

@end

@implementation DemoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //布局子视图
    [self layoutSubviews];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if ([self isViewLoaded] && !self.view.window) {
        self.view = nil;
    }
}

- (void)layoutSubviews {
    [self setupScrollView];//
    [self setUpPageControl];//创建页面控制

}

- (void)setupScrollView {

    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeigth)];
    //整屏滑动
    scroll.pagingEnabled = YES;
    //关闭滚动指示条 (水平方向)
    scroll.showsHorizontalScrollIndicator = NO;
    //设置容量大小
    scroll.contentSize = CGSizeMake(kScreenWidth *kImageCount, kScreenHeigth);//这个属性必须设定,不然不会滑动
    scroll.tag = kScrollViewTag;
    //设置代理
    scroll.delegate = self;
    [self.view addSubview:scroll];
    [scroll release];

    for (int i = 0; i < kImageCount; i ++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth * i, 0, kScreenWidth, kScreenHeigth)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@%02d", kImageName, i + 1]];
        if (i == kImageCount - 1) {
            //如果是最后一张,添加轻拍手势
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleIntoMain:)];
            //打开imageView的用户交互
            imageView.userInteractionEnabled = YES;
            [imageView addGestureRecognizer:tap];
            [tap release];
        }
        [scroll addSubview:imageView];
        [imageView release];
    }


}

- (void)setUpPageControl {
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((kScreenWidth - 200)/2, kScreenHeigth - 50, 200, 30)];
    pageControl.numberOfPages = kImageCount;
    pageControl.tag = kPageControlTag;
    pageControl.pageIndicatorTintColor = [UIColor grayColor];
    [self.view addSubview:pageControl];
    [pageControl release];

    }
//实现跳转
-(void)handleIntoMain:(UITapGestureRecognizer *)tap {
    //当点击最后一张时,将内容存储到NSUserDefaults中,单例模式
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:YES forKey:KFirstMark];//给一个key值,返回BOOL值,如果为NO的话每次进入都会进行引导
    //立即同步 (立即执行存储任务)
    [defaults synchronize];
//2. 进入程序主页面
    MainViewController *mainVC = [[MainViewController alloc] init];
    //更改window的根视图控制器为主页面视图控制器
    //单例模式,只初始化一次,在整个程序运行期间始终存在 是一个类方法
    //[UIApplication sharedApplication]  得到一个对象
    [UIApplication sharedApplication].keyWindow.rootViewController = mainVC;
    [mainVC release];

}

#pragma mark -- UIScrolViewDelegate--

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//获取scrollView 的偏移量
    NSInteger index = scrollView.contentOffset.x/kScreenWidth;
//获取pageControl
    UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:kPageControlTag];
    //设置pageControl的当前页
    pageControl.currentPage = index;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值