前言,关于进入App进入欢迎页面还是直接进入主页工具类的实现,在文章(2)中已经完成了工具类的实现,这里继续深入的写一下,如何把欢迎页的3~4页的滑动,最后一页点击开始进入的那种效果。具体效果先暂时脑补一下,稍后奉上。。。。、
正文:
上一文章说到,根据判断版本来选择初始化不同的视图控制器,当判断出版本有变化时候,创建这个NewFeatureViewController,具体如下。
//
// NewFeatureViewController.m
// kaixinwaP
//
// Created by wangyao on 15/11/24.
// Copyright © 2015年 wangyao. All rights reserved.
//
#import "NewFeatureViewController.h"
#import "QKTabBarController.h"
#define QKNewfeatureImageCount 3
@interface NewFeatureViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIPageControl * pageControl;
@end
@implementation NewFeatureViewController
- (void)viewDidLoad {
[super viewDidLoad];
[UIApplication sharedApplication].statusBarHidden = YES;
[self setUpScrollView];
// [self setUpLastImageView:<#(UIImageView *)#>]
[self setUpPageControl];
// Do any additional setup after loading the view.
}
//创建滑动视图
- (void)setUpScrollView
{
// 1.添加UIScrollView
UIScrollView * scrollView = [UIScrollView new];
scrollView.frame = self.view.bounds;
scrollView.delegate = self;
scrollView.contentSize = CGSizeMake(P_WIDTH*QKNewfeatureImageCount,0 );
// scrollView.directionalLockEnabled = YES;
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.bounces = NO;
[self.view addSubview:scrollView];
// 2.添加图片
CGFloat imageW = scrollView.width;
CGFloat imageH = scrollView.height;
for (int i = 0; i<QKNewfeatureImageCount; i++) {
// 创建UIImageView
UIImageView * imageView = [UIImageView new];
NSString * name = [NSString stringWithFormat:@"%d",i+1];
imageView.image = [UIImage imageNamed:name];
[scrollView addSubview:imageView];
// 设置frame
imageView.y = 0 ;
imageView.x = i * imageW;
imageView.width = imageW;
imageView.height = imageH;
// 给最后一个imageView添加按钮
if (i ==QKNewfeatureImageCount -1)
{
[self setUpLastImageView:imageView];
}
}
}
//创建书页控制器
- (void)setUpPageControl
{
// 1.添加
UIPageControl * pageControl = [UIPageControl new];
pageControl.numberOfPages = QKNewfeatureImageCount;
pageControl.centerX = self.view.width * 0.5;
pageControl.centerY = self.view.height - 70;
[self.view addSubview:pageControl];
// 2.设置圆点的颜色
pageControl.currentPageIndicatorTintColor = QKColor(106, 179, 60);//当前页的小圆点颜色
pageControl.pageIndicatorTintColor =QKColor(166, 166, 166);
self.pageControl = pageControl;
}
- (void)setUpLastImageView:(UIImageView *)imageView
{
imageView.userInteractionEnabled = YES;
// 1.添加开始按钮
[self setUpStartButton:imageView];
}
- (void)setUpStartButton:(UIImageView *)imageView
{
// 1.添加开始按钮
UIButton * startButton = [UIButton new];
[imageView addSubview:startButton];
// 2.设置背景图片
[startButton setBackgroundImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
[startButton setBackgroundImage:[UIImage imageNamed:@"2"] forState:UIControlStateHighlighted];
// 3.设置frame
startButton.size = CGSizeMake(30, 30);
startButton.centerX = self.view.width* 0.5;
startButton.y = self.view.height - startButton.height -20;
[startButton setTitle:@"开始" forState:UIControlStateNormal];
[startButton addTarget:self action:@selector(startApp) forControlEvents:UIControlEventTouchUpInside];
}
//开始应用
- (void)startApp
{
[UIApplication sharedApplication].statusBarHidden = NO;
QKTabBarController * VC = [QKTabBarController new];
UIWindow * window = [UIApplication sharedApplication].keyWindow;
window.rootViewController = VC;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//获得页码
CGFloat doublePage = scrollView.contentOffset.x /scrollView.width;
int intPage = (int)(doublePage + 0.5);
self.pageControl.currentPage = intPage;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
总结:
一.创建ScrollView并会调用里面的代理时,不要忘记<UIScrollViewDelegate>,并且在需要设置代理的scrollView上设置 scrollView.delegate = self;
二.
1.scrollView.contentSize = CGSizeMake(P_WIDTH*QKNewfeatureImageCount,0 );
2.scrollView.pagingEnabled = YES;
3.scrollView.showsHorizontalScrollIndicator = NO;
4.scrollView.bounces = NO;
[self.view addSubview:scrollView];
上面这段设置是很重要的,既要记住,也要会用,1.contentSize指的是可滑动的区域的那个范围,也就是相当于它的实际内容全部大小,而frame则是显示的那个大小,如果设置某一个方向上的可移动坐标为0,即表示这个scrollView在这个方向上面是不可以滑动的,同时,在有数值的那个位置上,就能够滑动多远的距离;
2.scrollView.pagingEnable = YES ; 意思是滑动的这个按照一页一页的那种模式,没有半页,超过半页就进入下一页,没超过就回到之前页;
3.scollView.showsHorizontalScrollIndicator = NO; 这条属性设置完,就能够取消滑动的同时有个指示条存在,对于欢迎页这个一定是要设置成为NO的,当然了,默认它是YES;
4.scrollView.bounces = NO,这条属性,一旦设置成NO,在最左边继续向左面滑或者最右边向右滑,都不会再有反应,而相反设置成YES则会在这种情况下,出现那种类似弹簧的效果,能够继续像极限的方向上划出一个距离。
三.
// 1.添加
1. UIPageControl * pageControl = [UIPageControl new];
2.pageControl.numberOfPages = QKNewfeatureImageCount;
3.pageControl.centerX = self.view.width * 0.5;
4pageControl.centerY = self.view.height - 70;
[self.view addSubview:pageControl];
// 2.设置圆点的颜色
5.pageControl.currentPageIndicatorTintColor = QKColor(106, 179, 60);//当前页的小圆点颜色
6.pageControl.pageIndicatorTintColor =QKColor(166, 166, 166);
7.self.pageControl = pageControl;
1.初始化一个页面指示器
2.设置页面控制器,即小圆点的个数;
3,4设置这个控制器的位置。
5.设置当前圆点的颜色。
6.设置其余小点的颜色。
7.将对象赋值给属性。
四.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//获得页码
1. CGFloat doublePage = scrollView.contentOffset.x /scrollView.width;
2.int intPage = (int)(doublePage + 0.5);
3.self.pageControl.currentPage = intPage;
}
这个代理是当scrollVIew被滑动的时候能够触发,执行内部的语句
1.目的是求出个数,因为软件自己不知道有多少页,怎么算一页,所以用 contentOffset 这个叫做滑动偏移量的东西除以一个的宽度,表示几页。
2.int intpage =(int)(doublePage+0.5);作用是四舍五入,加0.5后取整
3.设置pagecontrol的当前页码,将intPage的数值赋予给current page,实现了滑动后,根据page的变化,页面指示器页跟着变化。
一句话:听着杰伦的歌,总结着最近学完的知识,我想说真的是一种享受,喝着咖啡Coding,听着Music,写着博客,程序员的小幸福都在这里。