Swift可向上滑移出界面的欢迎页简单封装
使用:
-(WelcomView*)welcomeView{
if (!_welcomeView) {
_welcomeView = [[NSBundle mainBundle] loadNibNamed:@"WelcomView" owner:nil options:nil].firstObject;
_welcomeView.frame = self.window.frame;
}
return _welcomeView;
} ............
ImgModel *im = [[ImgModel alloc]init];
[im setValuesForKeysWithDictionary:obj];
self.welcomeView.imgArray = @[im];
//MARK:- Timer
extension Timer{
/**
*计时器暂停
*/
func timer_pause(){
self.fireDate = Date.distantFuture
} /**
*计时器继续
*/
func timer_continue(){
self.fireDate = Date.distantPast
}
}
import UIKit
//图片信息模型,根据需求修改
class ImgModel: NSObject {
var PicPath = ""
var PicDescription = ""
var AppSubjectPictureID = ""
var InfoLinkUrl = ""
var ShowSeconds = "" override func setValue(_ value: Any?, forUndefinedKey key: String) {
#if DEBUG
print("ImgModel出现了未定义的字段:\(key)-->\(String(describing: value))")
#endif
}
} class WelcomView: UIView ,UIScrollViewDelegate{
//空间关联
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var pageController: UIPageControl!
@IBOutlet weak var titleLB: UILabel!
@IBOutlet weak var btnBgview: UIView!
@IBOutlet weak var timeLB: UILabel!
@IBOutlet weak var intoMainBtn: UIButton! var stopIndex = 0 //滚动视图停止时候停在的位置
var beginPoint = CGPoint()
var beginPoint1 = CGPoint()
var second = 0
var beginSecond = 0
var timer:Timer?
//图片资源数组
var imgArray = [ImgModel]() {
didSet{
scrollView.tag = 1000
scrollView.contentSize = CGSize(width: CGFloat(imgArray.count)*self.frame.width, height: self.frame.height)
pageController.numberOfPages = imgArray.count
scrollView.isPagingEnabled = true
scrollView.bounces = true
scrollView.showsVerticalScrollIndicator = false
scrollView.showsHorizontalScrollIndicator = false
scrollView.delegate = self
for (index,ele) in imgArray.enumerated() {
let sv = UIScrollView(frame: CGRect(x: CGFloat(index)*self.frame.width, y: 0, width: self.frame.width, height: self.frame.height))
sv.tag = 1000 + (index + 1)
sv.delegate = self
sv.bounces = false
sv.showsVerticalScrollIndicator = false
sv.showsHorizontalScrollIndicator = false
sv.decelerationRate = 0.5
sv.contentSize = CGSize(width: self.frame.width, height: self.frame.height*2)
let imgView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height))
sv.addSubview(imgView)
scrollView.addSubview(sv)
imgView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action:#selector(tapAction))
imgView.addGestureRecognizer(tap)
second = Int(ele.ShowSeconds)!
beginSecond = second
DispatchQueue.main.async {
imgView.sd_setImage(with: URL(string:ele.PicPath), placeholderImage: UIImage(named: "正在加载"), options: .lowPriority, completed: { (img:UIImage?, erro:Error?, type:SDImageCacheType, imgUrl:URL?) in
if erro != nil{
self.removeFromSuperview()
self.showHint((erro?.localizedDescription)!)
}else{
appDelegate.window.addSubview(self)
}
})
}
}
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerNumChange(t:)), userInfo: nil, repeats: true)
//self.perform(#selector(removeSelf), with: nil, afterDelay: TimeInterval(second))
self.bringSubview(toFront: self.btnBgview)
}
} func timerNumChange(t:Timer){
second -= 1
if second >= 0 {
timeLB.text = "\(second)s"
}else{
self.removeSelf()
timer?.invalidate()
}
} @IBAction func btnAction(_ sender: UIButton) {
self.removeFromSuperview()
} //将自己移出界面
func removeSelf(){
UIView.animate(withDuration: 0.5, animations: {
self.center.y = -sheight/2
}) { (done:Bool) in
if done {
self.removeFromSuperview()
}
}
} override func draw(_ rect: CGRect) {
// Drawing code
super.draw(rect)
} override func awakeFromNib() {
super.awakeFromNib()
btnBgview.layer.cornerRadius = 25/2
btnBgview.layer.masksToBounds = true
} //图片点击事件
func tapAction(){
print("点击了第\(stopIndex)张")
} //MARK:ScrollView delegate
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
if scrollView.tag > 1000 {
beginPoint = self.center
beginPoint1 = scrollView.contentOffset
timer?.timer_pause()
//NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(removeSelf), object: nil)
}
} func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.tag > 1000 {
self.center.y = beginPoint.y - scrollView.contentOffset.y - beginPoint1.y
for imgView in scrollView.subviews {
if imgView.isKind(of: UIImageView.self) {
imgView.center = CGPoint(x: appDelegate.window.center.x, y: scrollView.center.y + scrollView.contentOffset.y - beginPoint1.y)
break
}
}
}
} func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if scrollView.tag == 1000 {
let offset = scrollView.contentOffset.x
let swidth = UIScreen.main.bounds.size.width
stopIndex = Int(offset / swidth)
pageController.currentPage = stopIndex }else{
scrollView.isScrollEnabled = true
scrollViewDidEndAction(scrollView)
}
} func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if decelerate{
if scrollView.tag > 1000 {
scrollView.isScrollEnabled = false
}
}else{
if scrollView.tag > 1000 {
scrollViewDidEndAction(scrollView)
}
}
} //MARK:滚动结束(界面速度为0)
func scrollViewDidEndAction(_ scrollView: UIScrollView){ if self.center.y < 0 {
UIView.animate(withDuration: 0.5, animations: {
self.center = CGPoint(x: self.center.x, y: appDelegate.window.center.y * -2)
}) { (done:Bool) in
if done {
self.removeFromSuperview()
}
}
}else{
UIView.animate(withDuration: 0.3, delay: 0.1, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.1, options: .curveLinear, animations: {
self.center = appDelegate.window.center
}) { (done:Bool) in
if done {
scrollView.contentOffset = self.beginPoint1
for imgView in scrollView.subviews {
if imgView.isKind(of: UIImageView.self) {
imgView.center = CGPoint(x: appDelegate.window.center.x, y: scrollView.center.y)
break
}
}
self.second = self.beginSecond + 1
self.timer?.timer_continue()
}
}
}
}
}
XIB效果图
Swift可向上滑移出界面的欢迎页简单封装的更多相关文章
- swift开发之--简单封装Alamofire请求类以及简单使用SnapKit
以前在swift3的时候,写过类似的,那个时候还没有很成熟的网络请求类库,在这里,还是衷心感谢大神们的付出! 具体效果如下,先上图: 点击按钮的时候,请求数据,数据结构如下: { ; reason = ...
- iOS开发Swift篇(02) NSThread线程相关简单说明
iOS开发Swift篇(02) NSThread线程相关简单说明 一 说明 1)关于多线程部分的理论知识和OC实现,在之前的博文中已经写明,所以这里不再说明. 2)该文仅仅简单讲解NSThread在s ...
- iOS开发笔记(Swift)-通用App安装引导页的实现
之前一直做的项目都是基于OC开发的,最近开始尝试使用Swift语言来重写整个项目. 本篇文章主要是讲述如何使用Swift来实现常见的通用App安装引导界面. 效果预览: 实现思路: 主要是采用了UIS ...
- 页面的Tab选项卡 简单实例
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...
- 【Swift】 GET&POST请求 网络缓存的简单处理
GET & POST 的对比 源码:https://github.com/SpongeBob-GitHub/Get-Post.git 1. URL - GET 所有的参数都包含在 URL 中 ...
- vuex存取token,http简单封装、模拟登入权限校验操作、路由懒加载的几种方式、vue单页设置title
1.config index.js下面的跨域代理设置: proxyTable: { '/api': { target: 'http://xxxx', //要访问的后端接口 changeOrigin: ...
- Swift - 简单封装一个工具类模板
创建模板类(封装一个类) 例1:新建一个名字叫做 Product 的类 Product.swift File 的内容 class Product { var name: String var desc ...
- swift:用UITabBarController、UINavigationController、模态窗口简单的搭建一个QQ界面
搭建一个QQ界面其实是一个很简单的实现,需要几种切换视图的控制器组合一起使用,即导航控制器.标签栏控制器.模态窗口.其中,将标签栏控制器设置为window的rootViewController,因为Q ...
- iOS开发——UI篇Swift篇&玩转UItableView(四)自定义&封装
UItableView自定义&封装 一:Model class AppsModel: NSObject { //定义模型的三个属性 var imageName:String! //图片名称 v ...
随机推荐
- dbus 消息和消息总线实例讲解-二
转自:http://www.fmddlmyy.cn/text53.html 2.3.2.ListActivatableNames和服务器的自动启动 运行: $ dbus-send --system - ...
- BZOJ 3932 [CQOI2015]任务查询系统 - 差分 + 主席树
Solution 差分就好了, 在$s_i$ 的点+1, $e_i + 1$ 的点 - 1. 查询的时候注意$l == r$ 要返回 $k * b[l]$ ,而不是$sum[node] $因为当前位置 ...
- Java Http协议处理类
public class HttpRequest { public static String doGet(String url,String params) throws Exception{ re ...
- 事务 TRANSACTION
事务是数据库中一个但单独的执行单元(Unit),他通常由高级数据库操作语言(如SQL)或编程语言(如C++.Java)编写的用户程序的执行所引起.当在数据库中更改数据成功时,在事务中更改的数据便会提交 ...
- Python之路(第二十一篇) re模块
一.re模块 正则表达式本身是一种小型的.高度专业化的编程语言,正则表达式就是字符串的匹配规则,在多数编程语言里都有相应的支持,python里对应的模块是re,正则表达式模式被编译成一系列的字节码,然 ...
- Null value was assigned to a property of primitive type setter of cn.itcast.oa.domain.Forum.topicCount
[引用http://m.blog.csdn.net/blog/u013998070/41087351] Null value was assigned to a property of primiti ...
- 在ListView中添加EditText丢失光标问题解决
<ListView android:id="@android:id/list" android:layout_height="fill_parent& ...
- 【机器学习】感知机学习算法(PLA)
感知机问题学习算法引入:信用卡问题 根据已知数据(不同标准的人的信用评级)训练后得出一个能不能给新客户发放信用卡的评定结果 解决该问题的核心思想扔为之前所讲到的梯度下降算法,对于更多条件的类似问题,首 ...
- 日志审计系统、事件日志审计、syslog审计
日志审计系统.事件日志审计.syslog审计 任何IT机构中的Windows机器每天都会生成巨量日志数据.这些日志包含可帮助您的有用信息: · 获取位于各个Windows事件日志严重性级别的所有网络活 ...
- 数据分析处理库pandas及可视化库Matplotlib
一.读取文件 1)读取文件内容 import pandas info = pandas.read_csv('1.csv',encoding='gbk') # 获取文件信息 print(info) pr ...