首页 技术 正文
技术 2022年11月11日
0 收藏 952 点赞 5,130 浏览 3866 个字
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
}
  
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,028
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,366
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,780
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,858