swift bannerview 广告轮播图
class BannerView: UIView,UIScrollViewDelegate{
//图⽚⽔平放置到scrollView上
private var scrollView:UIScrollView = UIScrollView()
//⼩圆点标识
private var pageControl:UIPageControl = UIPageControl()
private var imageViews:Array = Array<UIImageView>()
//图⽚集合
private var images:Array<String> = []
private var type:ImageType?
private var width:CGFloat =
private var height:CGFloat =
private var currIndex =
private var clickBlock :(Int)->Void = {index in}
private var timer:Timer?
// 默认⾃动播放 设置为false只能⼿动滑动
var isAuto = true
// 轮播间隔时间 默认6秒可以⾃⼰修改
var interval:Double =
private var startOffsetX:CGFloat =
override func layoutSubviews() {
super.layoutSubviews()
}
public func setImages(images:Array<String>,type:ImageType
= .Image,imageClickBlock:@escaping (Int) -> Void) {
self.type = type
self.images = images
self.clickBlock = imageClickBlock
self.initLayout()
}
private func initLayout(){
if(self.images.count == ){
return
}
width = self.bounds.width
height = self.bounds.height
scrollView.frame = self.bounds
scrollView.contentSize = CGSize(width:width * CGFloat(images.count +
),height:height)
scrollView.contentOffset = CGPoint(x:width,y:)
scrollView.isUserInteractionEnabled = true
scrollView.isPagingEnabled = true
scrollView.showsHorizontalScrollIndicator = false
scrollView.delegate = self
self.addSubview(scrollView)
var image = UIImageView()
image.frame = CGRect(x:,y:,width:width,height:height)
image.contentMode = .scaleToFill
image.isUserInteractionEnabled = true
setImage(image: image, index: images.count - )
scrollView.addSubview(image)
for i in ... images.count{
let image = UIImageView()
image.frame = CGRect(x:width *
CGFloat(i),y:,width:width,height:height)
image.contentMode = .scaleToFill
image.isUserInteractionEnabled = true
scrollView.addSubview(image)
setImage(image: image, index: i - )
addTapGesWithImage(image: image)
}
image = UIImageView()
image.frame = CGRect(x:width * CGFloat(images.count +
),y:,width:width,height:height)
image.contentMode = .scaleToFill
image.isUserInteractionEnabled = true
scrollView.addSubview(image)
setImage(image: image, index: )
pageControl.center = CGPoint(x:width/,y:height - CGFloat())
pageControl.isEnabled = true
pageControl.numberOfPages = images.count
pageControl.currentPageIndicatorTintColor = UIColor.green
pageControl.pageIndicatorTintColor = UIColor.gray
pageControl.isUserInteractionEnabled = false
self.addSubview(pageControl)
if(isAuto){
openTimer()
}
setCurrent(currIndex: )
}
private func setImage(image:UIImageView,index:Int){
if(type == .Image){
image.image = UIImage.init(named:images[index])
}else{
image.setMyImage(url: images[index])
}
}
func setCurrent(currIndex:Int) {
if(currIndex < ){
self.currIndex = images.count -
}else{
self.currIndex = currIndex
}
pageControl.currentPage = self.currIndex
scrollView.setContentOffset(CGPoint(x:width * CGFloat(self.currIndex +
),y:), animated: false)
}
//给图⽚添加点击⼿势
private func addTapGesWithImage(image:UIImageView) {
let tap = UITapGestureRecognizer(target: self, action: #selector(tap(_:)))
image.isUserInteractionEnabled = true //让控件可以触发交互事件
image.contentMode = .scaleToFill
// image.clipsToBounds = true //超出⽗控件的部分不显示
image.addGestureRecognizer(tap)
}
//点击图⽚,调⽤block
@objc func tap(_ ges:UITapGestureRecognizer) {
clickBlock((ges.view?.tag)!)
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
startOffsetX = scrollView.contentOffset.x
closeTimer()
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate
decelerate: Bool) {
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if(scrollView.contentOffset.x > startOffsetX){
currIndex = (currIndex + ) % images.count
}else{
currIndex = (currIndex - ) % images.count
}
setCurrent(currIndex: currIndex)
openTimer()
}
func openTimer(){
if(isAuto){
closeTimer()
timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector:
#selector(startAutoScroll), userInfo: nil, repeats: true)
}
}
func closeTimer(){
if(timer != nil){
timer?.invalidate()
timer = nil
}
}
@objc func startAutoScroll(){
if(isDisplayInScreen()){
setCurrent(currIndex: (currIndex + ) % images.count)
}
}
func isDisplayInScreen() -> Bool{
if(self.window == nil){
return false
}
return true
}
}
enum ImageType{
case Image //本地图⽚
case URL //URL
}
swift bannerview 广告轮播图的更多相关文章
- iOS回顾笔记(05) -- 手把手教你封装一个广告轮播图框架
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...
- JavaScripts广告轮播图以及定时弹出和定时隐藏广告
轮播图: 函数绑定在body标签内 采用3张图,1.jpg 2.jpg 3.jpg 利用定时任务执行设置图片属性 src 利用for循环可以完成3秒一次 一替换. 定时弹出广告: 由于bod ...
- android中广告轮播图总结
功能点:无限轮播.指示点跟随.点击响应.实现思路: 1.指示点跟随,指示点通过代码动态添加,数量由图片数量决定. 在viewpager的页面改变监听中,设置点的状态选择器enable,当前页时,set ...
- jquery 广告轮播图
轮播图 /*轮播图基本功能: * 1图片切换 * 1.1图片在中间显示 * 1.2图片淡入淡出 * 2左右各有一个按钮 * 2.1点击左按钮,图片切换上一张 * 2.2点击右按钮,图片切换下一张 * ...
- swift 广告轮播图
import UIKit import Kingfisher class BannerView: UIView,UIScrollViewDelegate{ enum ImageType{ case I ...
- android-auto-scroll-view-pager (无限广告轮播图)
github 地址: https://github.com/Trinea/android-auto-scroll-view-pager Gradle: compile ('cn.trinea.andr ...
- Android广告轮播图实现
先看效果 第一步,布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmln ...
- Android开发之ViewPager实现轮播图(轮播广告)效果的自定义View
最近开发中需要做一个类似京东首页那样的广告轮播效果,于是采用ViewPager自己自定义了一个轮播图效果的View. 主要原理就是利用定时任务器定时切换ViewPager的页面. 效果图如下: 主页面 ...
- 从零开始,搭建博客系统MVC5+EF6搭建框架(5),博客详情页、留言、轮播图管理、右侧统计博文
一.博客系统进度回顾 上一遍博客介绍到,系统已经实现到了发布以及前台布局展示,接下来就是实现一些,详情页,留言.轮播图管理.右侧博文统计信息实现. 二.博客系统详情页实现 2.1先来看看详情页展示的效 ...
随机推荐
- jvm 性能监控与linux常用命令
linux日常的运维 .Java开发 管理监控命令 ,较为浅显,欢迎提意见,我将在后面持续补充.
- C++编程学习(二) 数据
博主已经有一些基础了,所以写的东西可能是容易错的,或者以前没记住的,或者是对理解知识点有帮助的.因此如果有纯小白看到了这篇博文,不懂的地方请自行百度啦~ 另外,本系列所有内容的图片均来自于西北工业大学 ...
- 2. react 简书 头部 (header) 样式编写
1. 在 src 下 创建 common 文件下 创建 header 文件夹下 创建 index.js #src/common/header/index.js import React, {Compo ...
- 安装kubernetes遇见coredns坑
安装kubernetes遇见问题 kubectl describe pod coredns -n kube-system, 查看发现coredns readiness 一直unhealthy, 并且一 ...
- 《新标准C++程序设计》1.7-1.10(C++学习笔记2)
1.内联函数(inline关键字) eg.inline int Max(int a,int b) { if(a>b) return a; return b; } 当编译器处理调用内联函数的语句时 ...
- 对spring中IOC和AOP的理解
IOC:控制反转也叫依赖注入.利用了工厂模式. 为了方便理解,分解成每条以便记忆. 1.将对象交给容器管理,你只需要在spring配置文件总配置相应的bean,以及设置相关的属性,让spring容器 ...
- Failed to connect to raw.githubusercontent.com port 443: Connection refused
问题:macOS安装Homebrew时总是报错(Failed to connect to raw.githubusercontent.com port 443: Connection refused) ...
- 使用H5搭建webapp主页面
使用H5搭建webapp主页面 前言: 在一个h5和微信小程序火热的时代,作为安卓程序员也得涉略一下h5了,不然就要落后了,据说在简历上可以加分哦,如果没有html和css和js基础的朋友,可以自行先 ...
- pandas dataframe取差集:删掉已存在的数据,保留未插入的数据
适用场景: 插入数据到mysql中,中途中断,导致部分数据未插入成功.避免下次插入时插入了重复的数据. 思路: 1.读取已插入的数据, 2.读取全部数据(包含已插入和未插入的), 3.将已插入的数据添 ...
- Uber推出全新交通估算体系能颠覆传统模式吗?
当下,大众的交通出行正在被全面颠覆.除了传统的出行方式外,共享打车.共享单车.共享滑板车.分时租赁的共享汽车等,正在形成一个全新交通出行矩阵.鉴于交通出行市场的巨大潜力,众多巨头及独角兽企业在绞尽脑汁 ...