简体   繁体   English

当 UIImageview 在 UIBezierPath 内时消失

[英]UIImageview disappear when its inside a UIBezierPath

I Have a custom xib created along with a UIBezierpath, and when I add an image to the closed section of that UIBezierCurve, the image is not showing.我有一个与 UIBezierpath 一起创建的自定义 xib,当我将图像添加到该 UIBezierCurve 的封闭部分时,图像未显示。 Is there a way to add an image on top of the UIBezierpath's closed section.有没有办法在 UIBezierpath 的封闭部分顶部添加图像。

The image should display in the white section图像应显示在白色部分

在此处输入图像描述

Code as bellow代码如下

 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
  
}

Path creation method as bellow路径创建方法如下

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
    
}

Add a Second Layer on the Top of your path and set image as content in your new layer.在路径顶部添加第二层,并将图像设置为新层中的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM