swift和oc逻辑上都是一样的,只是写法不一样,可以使用一个view,也可以使用一个viewController,两种都可以的,使用view注意初始化的时候给他一个frame,vc的话,直接在本控制器里面写控制即可!

1,创建一个vc,然后在里面初始化一个scrollview,升值contentsize为3 * 页面宽度,然后添加图片,最后可以实现相应的代理方法,判断最后是点击进入主页,还是滑动

具体代码如下:

import UIKit

class hDisplayViewController: UIViewController,UIScrollViewDelegate {

    //页面数量
var numOfPages = override func viewDidLoad() {
super.viewDidLoad()
let frame = self.view.bounds
//scrollView的初始化
let scrollView = UIScrollView()
scrollView.frame = self.view.bounds
scrollView.delegate = self
//为了能让内容横向滚动,设置横向内容宽度为3个页面的宽度总和
scrollView.contentSize = CGSize(width:frame.size.width * CGFloat(numOfPages),
height:frame.size.height)
print("\(frame.size.width*CGFloat(numOfPages)),\(frame.size.height)")
scrollView.isPagingEnabled = true
scrollView.showsHorizontalScrollIndicator = false
scrollView.showsVerticalScrollIndicator = false
scrollView.scrollsToTop = false
for i in ..<numOfPages{
let imgfile = "bg\(Int(i+1)).png"
print(imgfile)
let image = UIImage(named:"\(imgfile)")
let imgView = UIImageView(image: image)
imgView.frame = CGRect(x:frame.size.width*CGFloat(i), y:CGFloat(),
width:frame.size.width, height:frame.size.height)
scrollView.addSubview(imgView)
}
scrollView.contentOffset = CGPoint.zero
self.view.addSubview(scrollView)
} func scrollViewDidScroll(_ scrollView: UIScrollView) {
print("scrolled:\(scrollView.contentOffset)")
let twidth = CGFloat(numOfPages-) * self.view.bounds.size.width
//如果在最后一个页面继续滑动的话就会跳转到主页面
if scrollView.contentOffset.x > twidth {
// let mainStoryboard = UIStoryboard(name:"Main", bundle:nil)
// let viewController = mainStoryboard.instantiateInitialViewController()
// self.present(MainVC(), animated: true, completion: nil)
let rootVC = UIApplication.shared.delegate as! AppDelegate
            rootVC.window?.rootViewController = MainVC()
}
}
}

2,在appdelegate里面写如下代码,判断是否第一次安装,

  //这里判断是否第一次启动APP
if (!(UserDefaults.standard.bool(forKey: "everLaunched"))) {
UserDefaults.standard.set(true, forKey:"everLaunched")
let guideViewController = hDisplayViewController()
self.window!.rootViewController=guideViewController;
print("guideview launched!")
}

如下图:

这样就完成了!

下面分享一个oc版的:

.h

@interface hDisplayView : UIView

.m :注意实现代理方法,判断偏移量或者其他的都行,因为是view,也可以添加手机或者按钮(建议frame是全屏幕大小的),然后实现响应事件隐藏就行了!

#import "hDisplayView.h"

@interface hDisplayView ()<UIScrollViewDelegate>
{
UIScrollView *_bigScrollView;
NSMutableArray *_imageArray;
UIPageControl *_pageControl;
} @end @implementation hDisplayView -(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_imageArray = [@[@"闪屏1.png",@"闪屏2.png", @"闪屏3.png",@"闪屏4.png"]mutableCopy]; // _imageArray = [NSMutableArray arrayWithObjects:@"闪屏1.png",@"闪屏2.png", @"闪屏3.png",@"闪屏4.png", nil]; UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(, , MainScreen_width, MainScreen_height)]; scrollView.contentSize = CGSizeMake((_imageArray.count + )*MainScreen_width, MainScreen_height);
//设置反野效果,不允许反弹,不显示水平滑动条,设置代理为自己
scrollView.pagingEnabled = YES;//设置分页
scrollView.bounces = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.delegate = self;
[self addSubview:scrollView];
_bigScrollView = scrollView; for (int i = ; i < _imageArray.count; i++) {
UIImageView *imageView = [[UIImageView alloc]init];
imageView.frame = CGRectMake(i * MainScreen_width, , MainScreen_width, MainScreen_height);
UIImage *image = [UIImage imageNamed:_imageArray[i]];
imageView.image = image; [scrollView addSubview:imageView];
} UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(MainScreen_width/, MainScreen_height - , , )];
pageControl.numberOfPages = _imageArray.count;
pageControl.backgroundColor = [UIColor clearColor];
[self addSubview:pageControl]; _pageControl = pageControl; //添加手势
UITapGestureRecognizer *singleRecognizer;
singleRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTapFrom)];
singleRecognizer.numberOfTapsRequired = ;
[scrollView addGestureRecognizer:singleRecognizer]; } return self;
} -(void)handleSingleTapFrom
{
if (_pageControl.currentPage == ) { self.hidden = YES; }
} -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
if (scrollView == _bigScrollView) { CGPoint offSet = scrollView.contentOffset; _pageControl.currentPage = offSet.x/(self.bounds.size.width);//计算当前的页码
[scrollView setContentOffset:CGPointMake(self.bounds.size.width * (_pageControl.currentPage), scrollView.contentOffset.y) animated:YES]; } if (scrollView.contentOffset.x == (_imageArray.count) *MainScreen_width) { self.hidden = YES; } }

调用:

if (![[NSUserDefaults standardUserDefaults] boolForKey:@"everLaunched"]) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
}
else{
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
} if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {
// 这里判断是否第一次 hDisplayView *hvc = [[hDisplayView alloc]initWithFrame:CGRectMake(, , MainScreen_width, MainScreen_height)]; [self.window.rootViewController.view addSubview:hvc]; [UIView animateWithDuration:0.25 animations:^{
hvc.frame = CGRectMake(, , MainScreen_width, MainScreen_height); }]; }

然后就可以了!

比较简单,只在此记录下!

swift--添加新手引导页的更多相关文章

  1. iOS新手引导页的实现,源码。

    /*.在Main.storyboard中找到,ScrollView和PageControl并添加到ViewController中. .在ScrollView中添加ImageView,新手引导页有几个图 ...

  2. 指令汇B新闻客户端开发(一) 新手引导页开发

    首先做开发的时候应该有一个闪屏页面和新手引导页, 我相信闪屏页面大家应该都会了,那么先看到新手引导页了. 我们可以看到这其实是一个ViewPager,我们也可以看到这是3个引导页,那么首先来看一下布局 ...

  3. iOS 新手引导页图片适配及其尺寸大全

    早期新手引导页只需要几张图片就可以解决了,随着屏幕尺寸的的越来越多,新手引导页的尺寸适配起来越来越麻烦,否则就会出现尺寸不匹配,图片被拉伸的情况 目前应该是有2种方法来解决这个问题 方法1: 根据每款 ...

  4. EasyUI创建异步树形菜单和动态添加标签页tab

    创建异步树形菜单 创建树形菜单的ul标签 <ul class="easyui-tree" id="treeMenu"> </ul> 写j ...

  5. iOSAPP添加启动页

    如果你在开发过程中出现屏幕显示内容比例不正常或者显示不全的问题,你发现不是代码或者约束的问题,那么很可能是启动页没有添加或者添加不全的原因,下面配一张问题图片上下黑屏 添加启动页步骤如下图 (1) ( ...

  6. C# 如何添加Excel页眉页脚(图片、文字、奇偶页不同)

    简介 我们可以通过代码编程来对Excel工作表实现很多操作,在下面的示例中,将介绍如何来添加Excel页眉.页脚.在页眉处,我们可以添加文字,如公司名称.页码.工作表名.日期等,也可以添加图片,如LO ...

  7. C#添加PDF页眉——添加文本、图片到页眉

    页眉常用于显示文档的附加信息,我们可以在页眉中插入文本或者图形,例如,页码.日期.公司徽标.文档标题.文件名或作者名等等.那么我们如何以编程的方式添加页眉呢?今天,这篇文章向大家分享如何使用了免费组件 ...

  8. Swift - 添加纯净的Alamofire

    Swift - 添加纯净的Alamofire 如果你有代码洁癖,不能容忍任何多余的东西,请继续往下看.  . 下载Alamofire (https://github.com/Alamofire/Ala ...

  9. 用 Markdown 写作(一)——添加文章页内导航

    Markdown 可以用更简化的标记来写文章,基本的语法可以参考Markdown 语法说明 (简体中文版). 我平时很少按照论文的写法去写博客,说来忏愧,因为很少写技术性的文章,最近看到百度百科和很多 ...

  10. ES6面向对象 动态添加标签页

    HTML <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml&quo ...

随机推荐

  1. 新手福利:Apache Spark入门攻略

    [编者按]时至今日,Spark已成为大数据领域最火的一个开源项目,具备高性能.易于使用等特性.然而作为一个年轻的开源项目,其使用上存在的挑战亦不可为不大,这里为大家分享SciSpike软件架构师Ash ...

  2. JavaScript Array 对象方法

    data.sort(function(a,b){return a.time>b.time?1:-1}); http://www.w3school.com.cn/jsref/jsref_obj_a ...

  3. linux tail -f 和 tail -F的区别 && tail 的断点续传

    bash-1中启动如下进程while [ "true" ] ; do date >> test.log; sleep 1 ; done; bash-2中,tail -f ...

  4. LeetCode: Text Justification 解题报告

    Text Justification Given an array of words and a length L, format the text such that each line has e ...

  5. centos7搭建python3并和python2共存

    注意事项:1.非root帐号加上sudo2.centos7自带Python 2.7.5是不能卸载的,很多系统级软件依赖这个 安装依赖# yum -y groupinstall "Develo ...

  6. Clock函数用法

    clock()是C/C++中的计时函数,而与其相关的数据类型是clock_t.在MSDN中,查得对clock函数定义如下: clock_t clock(void) ; 这个函数返回从“开启这个程序进程 ...

  7. 【Unity/Kinect】Kinect一些常用的API

    先开好这个坑,之后用到就补充,方便回顾. 获取用户相对Kinect传感器设备的位置坐标.(在Kinect坐标系中的位置) public Vector3 GetUserPosition(Int64 us ...

  8. Java并发(四)多线程开销

    从单线程应用转变为多线程应用并不只是带来好处.这种转变也会带来一定得开销得.并不是所有时候都要把你的应用编程多线程的.你应该明白这样做确实会带来好处,而且这种好处要比开销大.如果你不确定的话,要试着去 ...

  9. 载波帧听Carrier Sense

    在发送数据之前,以太网会“帧听”线缆,判断是否有其他数据传输. 如果通信介质上无载波,即没有被占用,则可以利用通信介质进行传送. 网卡芯片PHY具有上述功能. 数据链路层相关技术 共享介质网络 从通信 ...

  10. spring boot 自学笔记(四) Redis集成—Jedis

    上一篇笔记Reddis集成,操作Redis使用的是RedisTemplate,但实际中还是有一大部分人习惯使用JedisPool和Jedis来操作Redis, 下面使用Jedis集成示例. 修改Red ...