如何使 UIView 像 UITableviewCell 中的 Ticket

Posted

技术标签:

【中文标题】如何使 UIView 像 UITableviewCell 中的 Ticket【英文标题】:How to make the UIView as Like Ticket in UITablevViewCell 【发布时间】:2020-06-25 16:16:47 【问题描述】:

TicketViewTableViewCell.swift

@IBOutlet weak var ticketView: TestView!
override func awakeFromNib() 
    super.awakeFromNib()
    // Initialization code
    
    ticketView.layer.masksToBounds = true
    ticketView.layer.cornerRadius = 8.0
    ticketView.circleRadius = 10
    
    ticketView.circleY = ticketView.frame.height * 0.6



override func setSelected(_ selected: Bool, animated: Bool) 
    super.setSelected(selected, animated: animated)
    
    // Configure the view for the selected state
   

enter image description here

我想在 TableViewCell 中获取像上图一样的 UIView。

【问题讨论】:

【参考方案1】:

您可以将此自定义视图用作背景视图:

@IBDesignable class TicketBackground: UIView 
    
    @IBInspectable var cornerRadius : CGFloat = 4
    @IBInspectable var indentRadius : CGFloat = 10
    @IBInspectable var color : UIColor = UIColor.lightGray

    
    override func draw(_ rect: CGRect) 
        drawBackground()
    
    
    private func drawBackground()
    
        
        self.backgroundColor = UIColor.clear
        
        let width = self.frame.width
        let height = self.frame.height
        
        let path = UIBezierPath()
        path.move(to: CGPoint(x: cornerRadius, y: 0))
        path.addLine(to: CGPoint(x: width-cornerRadius, y: 0))
        path.addArc(withCenter: CGPoint(x: width-cornerRadius, y: cornerRadius), radius: cornerRadius, startAngle: 3*CGFloat.pi/2, endAngle: 0, clockwise: true)
        path.addLine(to: CGPoint(x: width, y: height/2-indentRadius))
        path.addArc(withCenter: CGPoint(x: width, y: height/2), radius: indentRadius, startAngle: 3*CGFloat.pi/2, endAngle: CGFloat.pi/2, clockwise: false)
        path.addLine(to: CGPoint(x: width, y: height-cornerRadius))
        path.addArc(withCenter: CGPoint(x: width-cornerRadius, y: height-cornerRadius), radius: cornerRadius, startAngle: 0, endAngle: CGFloat.pi/2, clockwise: true)
        path.addLine(to: CGPoint(x: cornerRadius, y: height))
        path.addArc(withCenter: CGPoint(x: cornerRadius, y: height-cornerRadius), radius: cornerRadius, startAngle: CGFloat.pi/2, endAngle: CGFloat.pi, clockwise: true)
        path.addLine(to: CGPoint(x: 0, y: height/2+indentRadius))
        path.addArc(withCenter: CGPoint(x: 0, y: height/2), radius: indentRadius, startAngle: CGFloat.pi/2, endAngle: 3*CGFloat.pi/2, clockwise: false)
        path.addLine(to: CGPoint(x: 0, y: cornerRadius))
        path.addArc(withCenter: CGPoint(x: cornerRadius, y: cornerRadius), radius: cornerRadius, startAngle: CGFloat.pi, endAngle: 3*CGFloat.pi/2, clockwise: true)
        
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = path.cgPath
        shapeLayer.fillColor = self.color.cgColor
        
        self.layer.addSublayer(shapeLayer)
    
    

【讨论】:

以上是关于如何使 UIView 像 UITableviewCell 中的 Ticket的主要内容,如果未能解决你的问题,请参考以下文章

UISearchBar 与普通 UIView 中的控制器?

使 UIVIew 看起来像 IB 中的 UIView 的最佳方法?

当调整大小小于元素时,如何使元素隐藏在 UIView 中

如何在 Swift 中使用 SnapKit 使 UIView 具有相等的宽度和高度?

你如何使 UIView 的幻灯片从左到右并在 iPhone 中相互对接?

如何创建 UIView 的凹形