如何在UIImageView底部应用曲线?

时间:2018-01-18 09:16:22

标签: ios swift uiimageview uiimage uibezierpath

我想屏蔽并在图像视图底部添加一些曲线。我试过下面的代码。

extension UIImage{
    var roundedImage: UIImage {
        let rect = CGRect(origin:CGPoint(x: 0, y: 0), size: self.size)
        UIGraphicsBeginImageContextWithOptions(self.size, false, 1)
        UIBezierPath(
            roundedRect: rect,
            cornerRadius: self.size.height
            ).addClip()
        self.draw(in: rect)
        return UIGraphicsGetImageFromCurrentImageContext()!
    }
  }

但没有获得成功。 让我把UI放在这里,我想在屏幕上显示。 enter image description here

让我知道如何在swift中显示UIImageView就像上面的屏幕截图一样。

我在Android中发现了一些完整的东西,但在iOS中没有。

链接:Crescento View

2 个答案:

答案 0 :(得分:9)

正如我在评论中所说,您需要自己UIBezierPath在路径的底部添加四边形曲线,curvedPercent将是您的曲线的显着程度,您可以调整它就像你需要它一样

自定义UIImageView类

@IBDesignable
class CurvedUIImageView: UIImageView {

    private func pathCurvedForView(givenView: UIView, curvedPercent:CGFloat) ->UIBezierPath
    {
        let arrowPath = UIBezierPath()
        arrowPath.move(to: CGPoint(x:0, y:0))
        arrowPath.addLine(to: CGPoint(x:givenView.bounds.size.width, y:0))
        arrowPath.addLine(to: CGPoint(x:givenView.bounds.size.width, y:givenView.bounds.size.height))
        arrowPath.addQuadCurve(to: CGPoint(x:0, y:givenView.bounds.size.height), controlPoint: CGPoint(x:givenView.bounds.size.width/2, y:givenView.bounds.size.height-givenView.bounds.size.height*curvedPercent))
        arrowPath.addLine(to: CGPoint(x:0, y:0))
        arrowPath.close()

        return arrowPath
    }

    @IBInspectable var curvedPercent : CGFloat = 0{
        didSet{
            guard curvedPercent <= 1 && curvedPercent >= 0 else{
                return
            }

            let shapeLayer = CAShapeLayer(layer: self.layer)
            shapeLayer.path = self.pathCurvedForView(givenView: self,curvedPercent: curvedPercent).cgPath
            shapeLayer.frame = self.bounds
            shapeLayer.masksToBounds = true
            self.layer.mask = shapeLayer
        }
    }

}

结果在故事板中可设计

enter image description here enter image description here enter image description here

对于任何类型的视图,添加了curvedPercent参数

func pathCurvedForView(givenView: UIView, curvedPercent:CGFloat) ->UIBezierPath
    {
        let arrowPath = UIBezierPath()
        arrowPath.move(to: CGPoint(x:0, y:0))
        arrowPath.addLine(to: CGPoint(x:givenView.bounds.size.width, y:0))
        arrowPath.addLine(to: CGPoint(x:givenView.bounds.size.width, y:givenView.bounds.size.height))
        arrowPath.addQuadCurve(to: CGPoint(x:0, y:givenView.bounds.size.height), controlPoint: CGPoint(x:givenView.bounds.size.width/2, y:givenView.bounds.size.height-givenView.bounds.size.height*curvedPercent))
        arrowPath.addLine(to: CGPoint(x:0, y:0))
        arrowPath.close()

        return arrowPath
    }

func applyCurvedPath(givenView: UIView,curvedPercent:CGFloat) {
    guard curvedPercent <= 1 && curvedPercent >= 0 else{
        return
    }

    let shapeLayer = CAShapeLayer(layer: givenView.layer)
    shapeLayer.path = self.pathCurvedForView(givenView: givenView,curvedPercent: curvedPercent).cgPath
    shapeLayer.frame = givenView.bounds
    shapeLayer.masksToBounds = true
    givenView.layer.mask = shapeLayer
}

我该如何使用?

self.applyCurvedPath(givenView: self.customView,curvedPercent: 0.5)

curvedPercent = 0.5

的结果

enter image description here

curvedPercent = 0.1

的结果

enter image description here

curvedPercent = 0.9

的结果

enter image description here

更新

对于倒置曲线,用此替换原始pathCurvedForView方法

func pathCurvedForView(givenView: UIView, curvedPercent:CGFloat) ->UIBezierPath
{
    let arrowPath = UIBezierPath()
    arrowPath.move(to: CGPoint(x:0, y:0))
    arrowPath.addLine(to: CGPoint(x:givenView.bounds.size.width, y:0))
    arrowPath.addLine(to: CGPoint(x:givenView.bounds.size.width, y:givenView.bounds.size.height - (givenView.bounds.size.height*curvedPercent)))
    arrowPath.addQuadCurve(to: CGPoint(x:0, y:givenView.bounds.size.height - (givenView.bounds.size.height*curvedPercent)), controlPoint: CGPoint(x:givenView.bounds.size.width/2, y:givenView.bounds.size.height))
    arrowPath.addLine(to: CGPoint(x:0, y:0))
    arrowPath.close()

    return arrowPath
}

<强>结果

enter image description here

答案 1 :(得分:4)

这将帮助您解决问题

extension UIImageView{
      var roundedImage: UIImageView {
        let maskLayer = CAShapeLayer(layer: self.layer)
        let bezierPath = UIBezierPath()
        bezierPath.move(to: CGPoint(x:0, y:0))
        bezierPath.addLine(to: CGPoint(x:self.bounds.size.width, y:0))
        bezierPath.addLine(to: CGPoint(x:self.bounds.size.width, y:self.bounds.size.height))
        bezierPath.addQuadCurve(to: CGPoint(x:0, y:self.bounds.size.height), controlPoint: CGPoint(x:self.bounds.size.width/2, y:self.bounds.size.height-self.bounds.size.height*0.3))
        bezierPath.addLine(to: CGPoint(x:0, y:0))
        bezierPath.close()
        maskLayer.path = bezierPath.cgPath
        maskLayer.frame = self.bounds
        maskLayer.masksToBounds = true
        self.layer.mask = maskLayer
        return self
      }
    }
  

<强>结果

enter image description here