import UIKit
//图片轮播组件代理协议
protocol BrowserViewDelegate{
//获取数据源
func browserHandleTapAction(index:Int)->Void
}
class ImgeBrowserManage: UIView,UIScrollViewDelegate {
//代理对象
var delegate : BrowserViewDelegate!
//屏幕宽度
let kScreenWidth = UIScreen.main.bounds.size.width
//当前展示的图片索引
var currentIndex : Int = 0
//数据源
var dataSource : [String]?
//用于轮播的左中右三个image(不管几张图片都是这三个imageView交替使用)
var leftImageView , middleImageView , rightImageView : UIImageView?
//放置imageView的滚动视图
var scrollerView : UIScrollView?
//scrollView的宽和高
var scrollerViewWidth : CGFloat?
var scrollerViewHeight : CGFloat?
//页控制器(小圆点)
var pageControl : UIPageControl?
func relodataData(ImagesArr:[String]){
//获取数据
self.dataSource = ImagesArr
//设置imageView
self.configureImageView()
//设置页控制器
self.configurePageController()
if ImagesArr.count > 1 {
//设置自动滚动计时器
}else{
self.scrollerView?.isScrollEnabled = false
}
}
init(frame: CGRect, ImagesArr:[String]) {
super.init(frame: frame)
//获取并设置scrollerView尺寸
self.scrollerViewWidth = frame.width
self.scrollerViewHeight = frame.height
//获取数据
self.dataSource = ImagesArr
//设置scrollerView
self.configureScrollerView()
//设置imageView
self.configureImageView()
//设置页控制器
self.configurePageController()
if ImagesArr.count > 1 {
//设置自动滚动计时器
}else{
self.scrollerView?.isScrollEnabled = false
}
self.backgroundColor = UIColor.gray
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//设置scrollerView
func configureScrollerView(){
self.scrollerView = UIScrollView(frame: CGRect(x: 0,y: 0,
width: self.scrollerViewWidth!, height: self.scrollerViewHeight!))
self.scrollerView?.delegate = self
self.scrollerView?.contentSize = CGSize(width: self.scrollerViewWidth! * 3,
height: self.scrollerViewHeight!)
//滚动视图内容区域向左偏移一个view的宽度
self.scrollerView?.contentOffset = CGPoint(x: self.scrollerViewWidth!, y: 0)
self.scrollerView?.isPagingEnabled = true
self.scrollerView?.bounces = false
self.scrollerView?.maximumZoomScale = 2
self.scrollerView?.minimumZoomScale = 1
self.addSubview(self.scrollerView!)
}
//设置imageView
func configureImageView(){
self.leftImageView = UIImageView(frame: CGRect(x: 0, y: 0,
width: self.scrollerViewWidth!, height: self.scrollerViewHeight!))
self.middleImageView = UIImageView(frame: CGRect(x: self.scrollerViewWidth!, y: 0,
width: self.scrollerViewWidth!, height: self.scrollerViewHeight! ));
self.rightImageView = UIImageView(frame: CGRect(x: 2*self.scrollerViewWidth!, y: 0,
width: self.scrollerViewWidth!, height: self.scrollerViewHeight!));
self.scrollerView?.showsHorizontalScrollIndicator = false
// self.leftImageView?.isUserInteractionEnabled = true
// self.middleImageView?.isUserInteractionEnabled = true
// self.rightImageView?.isUserInteractionEnabled = true
self.leftImageView?.contentMode = .scaleAspectFit
self.middleImageView?.contentMode = .scaleAspectFit
self.rightImageView?.contentMode = .scaleAspectFit
let singleTap = UITapGestureRecognizer.init(target: self,
action: #selector(self.oneTouch(_:)))
let doubleTap = UITapGestureRecognizer.init(target: self,
action: #selector(self.twoTouch(_:)))
let longpressGesutre = UILongPressGestureRecognizer(target: self,
action: #selector(self.handleLongpressGesture(_:)))
doubleTap.numberOfTapsRequired = 2
singleTap.require(toFail: doubleTap)
doubleTap.require(toFail: longpressGesutre)
self.scrollerView?.addGestureRecognizer(singleTap)
self.scrollerView?.addGestureRecognizer(doubleTap)
self.scrollerView?.addGestureRecognizer(longpressGesutre)
//设置初始时左中右三个imageView的图片(分别时数据源中最后一张,第一张,第二张图片)
if(self.dataSource?.count != 0){
self.touchViewAction()
resetImageViewSource()
}else{
// 如果没有图片,站位图
self.middleImageView?.image = UIImage(named: "img_05")
}
self.scrollerView?.addSubview(self.leftImageView!)
self.scrollerView?.addSubview(self.middleImageView!)
self.scrollerView?.addSubview(self.rightImageView!)
}
func oneTouch(_ sender: UITapGestureRecognizer){
UIView.animate(withDuration: 0.3, animations: {
self.alpha = 0
}, completion: { (callBack) in
self.removeFromSuperview()
// self.superview?.superview?.removeFromSuperview()
})
}
func twoTouch(_ sender: UITapGestureRecognizer){
}
func handleLongpressGesture(_ sender : UILongPressGestureRecognizer){
}
func scrollViewDidZoom(_ scrollView: UIScrollView) {
let image = scrollView.subviews[0]
if image.frame.size.height > UIScreen.main.bounds.width {
UIView.animate(withDuration: 0.2, animations: {
self.scrollerView?.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
})
}else{
UIView.animate(withDuration: 0.2, animations: {
self.scrollerView?.contentInset = UIEdgeInsetsMake(((self.scrollerView?.frame.size.height)! - image.frame.size.height)/2, 0, 0, 0)
})
}
}
internal func show(){
let window = UIApplication.shared.keyWindow
self.frame = (window?.bounds)!
window?.addSubview(self)
}
//设置页控制器
func configurePageController() {
self.pageControl = UIPageControl(frame: CGRect(x: kScreenWidth/2-60,
y: self.scrollerViewHeight! - 20, width: 120, height: 20))
self.pageControl?.numberOfPages = (self.dataSource?.count)!
self.pageControl?.isUserInteractionEnabled = false
self.addSubview(self.pageControl!)
}
//每当滚动后重新设置各个imageView的图片
func resetImageViewSource() {
//当前显示的是第一张图片
if self.currentIndex == 0 {
self.leftImageView?.image = UIImage(named: self.dataSource!.last!)
self.middleImageView?.image = UIImage(named: self.dataSource!.first!)
let rightImageIndex = (self.dataSource?.count)!>1 ? 1 : 0 //保护
self.rightImageView?.image = UIImage(named: self.dataSource![rightImageIndex])
}
//当前显示的是最好一张图片
else if self.currentIndex == (self.dataSource?.count)! - 1 {
self.leftImageView?.image = UIImage(named: self.dataSource![self.currentIndex-1])
self.middleImageView?.image = UIImage(named: self.dataSource!.last!)
self.rightImageView?.image = UIImage(named: self.dataSource!.first!)
}
//其他情况
else{
self.leftImageView?.image = UIImage(named: self.dataSource![self.currentIndex-1])
self.middleImageView?.image = UIImage(named: self.dataSource![self.currentIndex])
self.rightImageView?.image = UIImage(named: self.dataSource![self.currentIndex+1])
}
}
func touchViewAction(){
//添加组件的点击事件
// let tap = UITapGestureRecognizer(target: self,
// action: #selector(handleTapAction(_:)))
// self.addGestureRecognizer(tap)
}
//点击事件响应
func handleTapAction(_ tap:UITapGestureRecognizer)->Void{
self.delegate.browserHandleTapAction(index: self.currentIndex)
}
//scrollView滚动完毕后触发
func scrollViewDidScroll(_ scrollView: UIScrollView) {
//获取当前偏移量
let offset = scrollView.contentOffset.x
if(self.dataSource?.count != 0){
//如果向左滑动(显示下一张)
if(offset >= self.scrollerViewWidth!*2){
//还原偏移量
scrollView.contentOffset = CGPoint(x: self.scrollerViewWidth!, y: 0)
//视图索引+1
self.currentIndex = self.currentIndex + 1
if self.currentIndex == self.dataSource?.count {
self.currentIndex = 0
}
}
//如果向右滑动(显示上一张)
if(offset <= 0){
//还原偏移量
scrollView.contentOffset = CGPoint(x: self.scrollerViewWidth!, y: 0)
//视图索引-1
self.currentIndex = self.currentIndex - 1
if self.currentIndex == -1 {
self.currentIndex = (self.dataSource?.count)! - 1
}
}
//重新设置各个imageView的图片
resetImageViewSource()
//设置页控制器当前页码
self.pageControl?.currentPage = self.currentIndex
}
}
//手动拖拽滚动开始
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
//使自动滚动计时器失效(防止用户手动移动图片的时候这边也在自动滚动)
}
//手动拖拽滚动结束
func scrollViewDidEndDragging(_ scrollView: UIScrollView,
willDecelerate decelerate: Bool) {
//重新启动自动滚动计时器
}
}
调用方法:
let bview = HJImageBrowser()
// bview.delegate = self
bview.bottomView = self.view
bview.indexImage = index
bview.defaultImage = imagview.image //getColorImageWithColor()
bview.arrayImage = dataArray
bview.show()
代理方法:
func getTheThumbnailImage(_ indexRow: Int) -> UIImage {
return getColorImageWithColor()
// 或者返回imge 目的为了layar动画
}
网友评论