iOS swift5 轮播图 UITableView横向(水平方向)滚动

1.UITableView横向滚动

  • 核心代码
tableView.transform = CGAffineTransformMakeRotation(-Double.pi / 2)

cell.transform = CGAffineTransformMakeRotation(.pi / 2)

2.轮播图(代码)

CarouselCell

import UIKit

class CarouselCell: UITableViewCell {
    
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    
    
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
    
    
        fatalError("init(coder:) has not been implemented")
    }
    
    let imageViewM = UIImageView()
    
    func setupUI(){
    
    
        imageViewM.frame = contentView.bounds
        contentView.addSubview(imageViewM)
    }
    
    
    override func layoutSubviews() {
    
    
        imageViewM.frame = contentView.bounds
    }
}

CarouselView

import UIKit

class CarouselView: UIView {
    
    
    
    var getIndexBlock:(Int)->Void = {
    
     index in }
   
    override init(frame: CGRect) {
    
    
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
    
    
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
    
    
        let tableView = UITableView()
        tableView.frame = bounds
        tableView.backgroundColor = .yellow
        addSubview(tableView)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.showsVerticalScrollIndicator = false
        tableView.isUserInteractionEnabled = false
        tableView.transform = CGAffineTransformMakeRotation(-Double.pi / 2)
        
        tableView.scrollToRow(at: IndexPath(row: 1, section: 0), at: .top, animated: false)
        
        //TimeInterval 1后面的单位是秒
        var index = 1
        weak var weakSelf = self
        Timer.scheduledTimer(withTimeInterval: TimeInterval(3), repeats: true, block:{
    
    (timer: Timer) -> Void in
            index += 1
            let indexPath = IndexPath(row: index, section: 0)
            tableView.scrollToRow(at: indexPath, at: .top, animated: true)
            
            if index == 5{
    
    
                //now后面的单位是秒
                DispatchQueue.main.asyncAfter(deadline: .now()+0.5, execute:
                                                {
    
    
                    tableView.scrollToRow(at: IndexPath(row: 1, section: 0), at: .top, animated: false)
                    index = 1
                    weakSelf?.getIndexBlock(index-1)
                })
            }else{
    
    
                weakSelf?.getIndexBlock(index-1)
            }
        })
    }
}

extension CarouselView : UITableViewDataSource, UITableViewDelegate{
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    
        6
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    
        let cell = CarouselCell()
        switch indexPath.row {
    
    
        case 0:
            cell.imageViewM.image = UIImage(named: "image4")
        case 1:
            cell.imageViewM.image = UIImage(named: "image1")
        case 2:
            cell.imageViewM.image = UIImage(named: "image2")
        case 3:
            cell.imageViewM.image = UIImage(named: "image3")
        case 4:
            cell.imageViewM.image = UIImage(named: "image4")
        case 5:
            cell.imageViewM.image = UIImage(named: "image1")
        default:
            break
        }
        cell.transform = CGAffineTransformMakeRotation(.pi / 2)
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    
    
           frame.size.width
    }
}

ViewController

import UIKit

class ViewController: UIViewController{
    
    
    
    override func viewDidLoad() {
    
    
        super.viewDidLoad()
        let carouselView = CarouselView(frame: CGRect(x: 100, y: 200, width: 200, height: 200))
        view.addSubview(carouselView)
        
        carouselView.getIndexBlock = {
    
     index in
            print(index)
        }
    }
    
}

参考博客:
ios之UITableView设置横向 - CSDN
iOS UITableView横向滚动 - CSDN

猜你喜欢

转载自blog.csdn.net/baidu_40537062/article/details/130794786