zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

iOS swift 5 present从下面弹出一半 设置速度档位

ios 设置 swift 速度 下面 一半 present
2023-09-14 09:04:13 时间

请添加图片描述

上半部分放一个半透明的View

请添加图片描述

代码

import UIKit

extension UIView {    
    //radius:切圆角的半径
    //corner:要切四个角中的哪个角
    func cornerCut(radius:Int,corner:UIRectCorner){
        let maskPath = UIBezierPath.init(roundedRect: bounds, byRoundingCorners: corner, cornerRadii: CGSize.init(width: radius, height: radius))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = bounds
        maskLayer.path = maskPath.cgPath
        layer.mask = maskLayer
    }
}

class FDSpeedPresentVC: UIViewController, UITableViewDataSource, UITableViewDelegate {

    typealias SpeedClosure = (Int)->Void
    var callBackClosure:SpeedClosure!
    
    @IBOutlet var topView: UIView!
    
    @IBOutlet var bottomView: UIView!
    
    @IBOutlet var navigationBarMy: UINavigationBar!
    
    @IBOutlet var tableView: UITableView!
    
    
    override func viewDidLayoutSubviews() {
        bottomView.cornerCut(radius: 10, corner: [.topLeft,.topRight])
       
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationBarMy.titleTextAttributes = [NSAttributedString.Key.font:UIFont(name: "PingFang SC", size: 18) as Any]
      
        tableView.dataSource = self
        tableView.delegate = self
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(tapView))
        topView.addGestureRecognizer(tap)
        
         
    }
    
    @objc func tapView() {
       dismiss(animated: true, completion: nil)
    }

    
    
    var selectedIndex = 0
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:FDSpeedCell = tableView.dequeueReusableCell(withIdentifier: "FDSpeedCell", for: indexPath) as! FDSpeedCell
        
        if indexPath.row == selectedIndex {
            cell.btn.isSelected = true
        }
        
        switch indexPath.row {
        case 0:
            cell.label.text = "速度等级1"
        case 1:
            cell.label.text = "速度等级2"
        case 2:
            cell.label.text = "速度等级3"
        default:
            break
        }
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        for cell in tableView.visibleCells {
            let speedCell:FDSpeedCell = cell as! FDSpeedCell
            speedCell.btn.isSelected = false
        }
        let selectedCell:FDSpeedCell = tableView.cellForRow(at: indexPath) as! FDSpeedCell
        selectedCell.btn.isSelected = true
        callBackClosure(indexPath.row)
        dismiss(animated: true, completion: nil)
    }
    
}