【问题标题】:How to apply Curve at bottom of UIImageView?如何在 UIImageView 底部应用曲线?
【发布时间】:2018-01-18 09:16:22
【问题描述】:

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

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 放在这里。

让我知道如何像上面的屏幕截图一样快速显示 UIImageView。

我在 android 中发现了一些使用完整的东西,但在 iOS 中却没有。

链接:Crescento View

【问题讨论】:

  • 您需要自己创建UIBezierPath 来执行此操作,roundedRect 会在您的矩形视图中添加边框角,您需要的比这更具体
  • @ReinierMelian 我对 UIBezierPath 没有深入的了解,如果你帮助我编写代码的 sn-p,那对我来说会很棒。
  • 答案已添加@JayMehta
  • 我的答案已更新@JayMehta

标签: ios swift uiimageview uiimage uibezierpath


【解决方案1】:

正如我在评论中所说,您需要制作自己的 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
        }
    }

}

按原样生成故事板可设计

对于任何一种View,添加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的结果

curvedPercent = 0.1的结果

curvedPercent = 0.9 的结果

更新

对于倒置曲线,用这个替换原来的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
}

结果

【讨论】:

  • 感谢@chiragshah 赞赏
  • 解释清楚的答案
  • 感谢@LeNI 赞赏
  • 为什么不能做出完美的曲线?为什么一开始更弯,最后更不弯?
  • 我会审查它@KaranveerSingh,应该是因为我使用 QuadCurve,我会更新结果
【解决方案2】:

这将帮助您解决问题

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
      }
    }

结果

【讨论】:

  • 您的答案仅适用于 imageview,否则很好。
猜你喜欢
  • 2019-01-29
  • 1970-01-01
  • 2022-08-02
  • 2021-06-17
  • 1970-01-01
  • 2020-10-05
  • 2014-12-10
  • 2020-11-13
  • 1970-01-01
相关资源
最近更新 更多