swift 轮播图 (二) 带自定义指示标

效果图:
swift 轮播图 (二) 带自定义指示标_第1张图片

直接上代码

//
//  LHWhellPlantImage.swift
//  safari
//
//  Created by 磊怀王 on 2019/12/2.
//  Copyright © 2019 磊怀王. All rights reserved.
//

import UIKit

class LHWhellPlantImage: NSObject,UIScrollViewDelegate {
    
    override init() {
        super.init()
    }
    
    var line_Arr : [UIView] = Array.init()
    
    
    /// 轮播图的实现类
    /// - Parameters:
    ///   - imageArr: 原始图片数组   即图片的名字
    ///   - superView: 所要加载的父视图
    ///   - rect: 轮播图的位置
    func initWhellPantImage(_ imageArr : Array,_ superView : UIView,_ rect : CGRect) -> UIView {
        let vi = UIView.init(frame: rect)
        superView.addSubview(vi)
        
        let imageScro : UIScrollView = UIScrollView.init()
        imageScro.frame = CGRect.init(x: 0, y: 0, width: Int(vi.frame.size.width), height: Int(vi.frame.size.height))
        imageScro.backgroundColor = UIColor.black
        vi.addSubview(imageScro)
        
        let scroW = imageScro.frame.size.width
        let scroH = imageScro.frame.size.height
        
        var newImageArr : Array = Array.init()
        newImageArr += imageArr
        newImageArr.insert(imageArr.last!, at: 0)
        newImageArr.insert(imageArr.first!, at: newImageArr.count)
        for tempImage in newImageArr.enumerated() {
            print(tempImage)
            let imageR : CGRect = CGRect.init(x: scroW * CGFloat(tempImage.offset), y: 0, width: scroW, height: scroH)
            
            let imageV = UIImageView.init(frame: imageR)
            imageV.image = UIImage.init(named: tempImage.element)
            imageScro.addSubview(imageV)
            // 添加点击事件
            let tap = UITapGestureRecognizer.init(target: self, action: #selector(touchWhellImage(_:)))
            imageV.addGestureRecognizer(tap)
            imageV.isUserInteractionEnabled = true
            imageV.tag = 100 + tempImage.offset
        }
        
        imageScro.contentSize = CGSize.init(width: scroW * CGFloat(newImageArr.count), height: scroH)
        imageScro.contentOffset = CGPoint.init(x: scroW, y: 0);
        imageScro.isUserInteractionEnabled = true
        imageScro.isPagingEnabled = true
        imageScro.delegate = self
        
        return vi
    }
    let markW = 12
    
    func initWhellWithMark(_ imageArr : Array,_ superView : UIView,_ rect : CGRect) -> Void {
        let endScroll = self.initWhellPantImage(imageArr, superView, rect)
        var diff_v = 0.0
        for tempObj in imageArr.enumerated() {
            var line_w = markW
            
            if tempObj.offset == 0 {
                line_w += 6
            }else{
                line_w = 12
            }
            let point_x = Float(imageArr.count) * Float(markW) * 1.5 + Float(markW)
            let l_rect = CGRect.init(x: rect.width - CGFloat(point_x) +  CGFloat(diff_v), y: rect.size.height - CGFloat(14), width: CGFloat(line_w), height: CGFloat(4))
            
            let line = UIView.init(frame: l_rect)
            line.backgroundColor = UIColor.white
            endScroll.addSubview(line)
            diff_v += Double(l_rect.size.width) + Double(markW) / 2.0
            
            line_Arr.append(line)
        }
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let scroW = scrollView.frame.size.width
        let pageAll = scrollView.contentSize.width / scroW
        let page = scrollView.contentOffset.x / scroW
        if page == 0 {
            scrollView.contentOffset = CGPoint.init(x: scroW * CGFloat(pageAll - 2), y: 0)
        }else if page == (pageAll - 1){
            scrollView.contentOffset = CGPoint.init(x: scroW, y: 0)
        }
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let page = scrollView.contentOffset.x / scrollView.frame.size.width;
        let parcentPage = Double(page) - floor(Double(page))
        let allPage = scrollView.contentSize.width / scrollView.frame.size.width - CGFloat(2)
        if parcentPage == 0 {
            return
        }
        var currentV = UIView.init()
        var nextV = UIView.init()
        
        if page < 1 {
            currentV = line_Arr.first!
            nextV = line_Arr.last!
        }else if page > allPage {
            currentV = line_Arr.last!
            nextV = line_Arr.first!
        }else{
            currentV = line_Arr[Int(floor(Double(page))) - 1]
            nextV = line_Arr[Int(ceil(Double(page)) - 1)]
        }
        //当前 指示图
        let point_x = scrollView.frame.size.width - (CGFloat(Float(allPage) * Float(Float(markW) * 1.5))) - CGFloat(markW)
        
        let current_v_x = Double(point_x) + (1.5 * Double(markW)) * (floor(Double(page)) - 1)
        let current_v_y = Double(currentV.frame.origin.y)
        let current_v_w = Float(markW) * 1.5 - Float(Double(markW / 2) * parcentPage)
        let current_v_h = Double(currentV.frame.size.height)
    
        // 接下来的指示图
        let next_v_x = current_v_x + Double(Float(markW) * 1.5) + Double(markW / 2) - Double((Float(markW) / 2) * Float(parcentPage))
        let next_v_w = Double(markW) + Double(markW / 2) * parcentPage
        
        if page < 1 {
            for temp in line_Arr.enumerated() {
                if temp.offset == 0 {
                    currentV.frame = CGRect.init(x: Double(point_x), y: current_v_y, width: Double(markW) * 1.5 - Double(markW) * 0.5 * (1.0 - parcentPage), height: current_v_h);
                }else if temp.offset == Int(allPage - 1) {
                    let n_x = Double(point_x) + (1.5 * Double(markW)) * Double(Int(allPage) - 1) + Double(markW) / 2.0
                    nextV.frame = CGRect.init(x: n_x - (Double(markW) / 2.0) * (1.0 - parcentPage), y: current_v_y, width: Double(markW) + (Double(markW) / 2.0) * (1.0 - parcentPage) , height: current_v_h)
                }else{
                    let v_x = Double(point_x) + (1.5 * Double(markW)) * Double(temp.offset) + Double(markW) / 2.0
                    temp.element.frame = CGRect.init(x: v_x - Double(Float(markW) / 2.0) * (1.0 - parcentPage), y: current_v_y, width: Double(markW), height: current_v_h)
                }
            }
        }else if page > allPage {
            for temp in line_Arr.enumerated() {
                if temp.offset == Int(allPage - 1) {
                    currentV.frame = CGRect.init(x: current_v_x + Double(Float(markW) / 2) * parcentPage, y: current_v_y, width: Double(current_v_w), height: current_v_h);
                }else if temp.offset == 0 {
                    nextV.frame = CGRect.init(x: Double(point_x), y: current_v_y, width: Double(markW) + (Double(markW) / 2.0) * parcentPage , height: current_v_h)
                }else{
                    let v_x = Double(point_x) + (1.5 * Double(markW)) * Double(temp.offset)
                    temp.element.frame = CGRect.init(x: v_x + Double(Float(markW) / 2.0) * parcentPage, y: current_v_y, width: Double(markW), height: current_v_h)
                }
            }
        }else{
            currentV.frame = CGRect.init(x: current_v_x, y: current_v_y, width: Double(current_v_w), height: current_v_h)
            nextV.frame = CGRect.init(x: Double(next_v_x), y: current_v_y, width: next_v_w, height: current_v_h)
        }
        
        print("拖动了---\(page) ----- \(ceil(Double(page))) ---- \(parcentPage)")
    }
    
    @objc func touchWhellImage(_ ges : UITapGestureRecognizer) -> Void {
        print("点击了第\(ges.view!.tag - 100)张图片")
    }
}

引用:

func bannerScroller() -> Void {
        let imageArr : Array = ["scroll001","scroll002","scroll003","scroll004","scroll005","scroll006","scroll007","scroll008","scroll009"]
        let rect : CGRect = CGRect.init(x: 10, y: 200, width: UIScreen.main.bounds.size.width - 20, height: 100)
        
        whell.initWhellWithMark(imageArr, self.view, rect)
    }

下篇微博加上 时间控制器

2849765859 是我QQ ,欢迎联系我

你可能感兴趣的:(IT,Swift)