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. VC++ 遍历目录

    遍历文件目录,即把一个目录里的文件名都取出来.本文是CFileFind类的使用实例的笔记.下面的程序是从一个目录出发,把这个目录里的所有成员按着层次输出. 代码如下: void TravelFolde ...

  2. echarts报错Cannot read property 'features' of undefined

    引入地图的时候 echarts2报错: Uncaught Error: [MODULE_MISS]"echarts/src/util/mapData/params" is not ...

  3. URLConnection格式与用法

    private void getdialog() { final EditText et = new EditText(this); final String workid = this.workid ...

  4. uWSGI的stats注释,送给需要的人,欢迎指正

    吐槽先,对于uWSGI状态信息没有文档说明这样一个现实,我只想说一句:F*CK YOU!!! 花了2天时间,累得眼珠子疼,针对这鬼畜的stats,借助Total Commander和VS大概撸了一边u ...

  5. mount -t nfs 不能使用

    去年使用一个新的文件系统的时候,发现mount -t nfs ip:/g/ftp ~/mnt -o tcp,nolock 不能使用 一直以为是因为mount 命令更新了,有些用法我不会用,但是刚才发现 ...

  6. [应用]Linux下" >/dev/null 2>&1 "

    转自:http://blog.csdn.net/sunrier/article/details/7695839 这条命令的意思就是在后台执行这个程序,并将错误输出2重定向到标准输出1,然后将标准输出1 ...

  7. nginx的centos和rhel的yum配置安装

    Official Red Hat/CentOS packages To add NGINX yum repository, create a file named /etc/yum.repos.d/n ...

  8. Nginx实战系列之功能篇----后端节点健康检查(转)

    公司前一段对业务线上的nginx做了整理,重点就是对nginx上负载均衡器的后端节点做健康检查.目前,nginx对后端节点健康检查的方式主要有3种,这里列出:   1.ngx_http_proxy_m ...

  9. StarRTC , AndroidThings , 树莓派小车,公网环境,视频遥控(一)准备工作

    原文地址:http://blog.starrtc.com/?p=48 啥也不说,先来个视频看看效果 视频播放器     00:00   00:54     概述为了体现StarRTC的实时音视频传输能 ...

  10. ubuntu下 apache phpmyadmin 的安装和使用

    // Apache //Task: Start Apache 2 Server /启动apache服务 # /etc/init.d/apache2 start //or $ sudo /etc/ini ...