【问题标题】:How to make the UIView as Like Ticket in UITablevViewCell如何使 UIView 像 UITableviewCell 中的 Ticket
【发布时间】: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。

【问题讨论】:

    标签: swift uitableview uiview swift5


    【解决方案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)
        }
        
    }
    

    【讨论】:

      猜你喜欢
      • 2012-09-14
      • 1970-01-01
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多