IOS 欢迎页(UIScrollView,UIPageControl)

本文介绍了app欢迎页的简单实现。只有第一次运行程序时才说会出现,其余时间不会出现。下面是效果图。

代码如下:(如有不明白的可以评论我,我会详细讲解)

//
//  ViewController.m
//  CX IOS欢迎页
//
//  Created by ma c on 16/3/18.
//  Copyright © 2016年 xubaoaichiyu. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>

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

@end

@implementation ViewController
#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
   //判断是否是第一次运行程序
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
//    [userDefaults removeObjectForKey:@"FirstLoad"];
    if ([userDefaults objectForKey:@"FirstLoad"] == nil) {
        [userDefaults setBool:NO forKey:@"FirstLoad"];
        [self loadScrollView];
    }
    
    
    
}
#pragma mark - deleDate

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    CGPoint point = self.scrollView.contentOffset;
    
    NSInteger current = point.x / self.view.frame.size.width;
    
    self.pageControl.currentPage = current;
    
}

#pragma mark - function
-(void)loadScrollView{
    
    CGRect rect = [UIScreen mainScreen].bounds;
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:rect];
    //分页
    self.scrollView.pagingEnabled = YES;
    
    self.scrollView.delegate = self;
    
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    self.scrollView.showsVerticalScrollIndicator = NO;
    //scrollView滚动区域大小
    self.scrollView.contentSize = CGSizeMake(rect.size.width * 4, rect.size.height);
    
    for (NSInteger i = 0; i < 4; i ++) {
        
        UIImageView * imageView = [[UIImageView alloc]init];
        
        imageView.frame = CGRectMake(i * rect.size.width, 0, rect.size.width, rect.size.height);
        //为了区别imageView而又简单操作,建立随机颜色。
        imageView.backgroundColor = [UIColor colorWithRed:arc4random() % 255 / 255.f green:arc4random() % 255 / 255.f blue:arc4random() % 255 / 255.f alpha:1];
        [self.scrollView addSubview:imageView];
        
        //添加开启软件
        UIButton * removeBUtton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        removeBUtton.frame = CGRectMake(150 + 3 * rect.size.width, rect.size.height * 0.7, rect.size.width - 300, 30);
        
        [removeBUtton setTitle:@"开启旅行" forState:UIControlStateNormal];
        
        [removeBUtton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        removeBUtton.backgroundColor = [UIColor orangeColor];
        
        [removeBUtton addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
        
        [self.scrollView addSubview:removeBUtton];
    }
    
    [self.view addSubview:self.scrollView];
    
    [self loadPageControl];
    
}

-(void)start{
    
    [self.pageControl removeFromSuperview];
    
    [self.scrollView removeFromSuperview];
    
}
-(void)loadPageControl{
    
    CGRect rect = [UIScreen mainScreen].bounds;
    
    self.pageControl = [[UIPageControl alloc]init];
    
    self.pageControl.numberOfPages = 4;
    
    CGSize pageControlSize = [self.pageControl sizeForNumberOfPages:4];
    
    self.pageControl.frame = CGRectMake((rect.size.width - pageControlSize.width) / 2, rect.size.height * 0.8, pageControlSize.width, pageControlSize.height);
    
    self.pageControl.currentPage = 0;
    
    self.pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
    
    self.pageControl.pageIndicatorTintColor = [UIColor grayColor];
    
    [self.view addSubview:self.pageControl];
    
    [self.view bringSubviewToFront: self.pageControl];
    
}
@end

 

posted @ 2016-03-18 21:30  旭宝爱吃鱼  阅读(1922)  评论(0编辑  收藏  举报