【问题标题】:UIImageview disappear when its inside a UIBezierPathUIImageview 在 UIBezierPath 内时消失
【发布时间】:2020-08-04 02:23:00
【问题描述】:

我有一个与 UIBezierpath 一起创建的自定义 xib,当我将图像添加到该 UIBezierCurve 的封闭部分时,图像未显示。有没有办法在 UIBezierpath 的封闭部分顶部添加图像。

图像应显示在白色部分

代码如下

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  
         let cell = Bundle.main.loadNibNamed(TableViewCells.ForgotPwCell.rawValue, owner: self, options: nil)?.first as! ForgetPasswordTableViewCell
         let shape = Utils.drawBazierCurve(width: tableView.frame.width, height: tableView.frame.height)
         cell.contentView.layer.addSublayer(shape)
         let theImage = UIImage(named: "forgot_pw.png") // create a UIImage
         cell.forgotpwImageVw.image = theImage
        return cell
  
}

路径创建方法如下

static func drawBazierCurve(width:CGFloat,height:CGFloat) -> CAShapeLayer {

let path = UIBezierPath()

let controlPoint1 = CGPoint(x: (3/8)*width, y: (4/18)*height)
let controlPoint2 = CGPoint(x: (6/8)*width, y: (8/18)*height)
let end = CGPoint(x: width, y: (6/18)*height)

path.move(to: CGPoint(x: 0, y: height/2))
path.addCurve(to: end, controlPoint1: controlPoint1, controlPoint2: controlPoint2)
path.addLine(to: CGPoint(x: width, y: 0))
path.addLine(to: CGPoint(x: 0, y: 0))

path.close()

let shape = CAShapeLayer()
shape.path = path.cgPath;
shape.fillColor = UIColor.white.cgColor

return shape
    
}

【问题讨论】:

    标签: ios swift swift5 uibezierpath


    【解决方案1】:

    在路径顶部添加第二层并将图像设置为新层中的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      • 2013-12-21
      • 2011-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多