How to apply Curve at bottom of UIImageView?(如何在 UIImageView 底部应用曲线?)
问题描述
我想在图像视图的底部屏蔽并添加一些曲线.我尝试了下面的代码.
扩展 UIImage{var roundedImage:UIImage {让 rect = CGRect(origin:CGPoint(x: 0, y: 0), size: self.size)UIGraphicsBeginImageContextWithOptions(self.size, false, 1)UIBezierPath(roundedRect:矩形,角半径:self.size.height).addClip()self.draw(in: rect)返回 UIGraphicsGetImageFromCurrentImageContext()!}}
但没有成功.让我把我想在屏幕上显示的 UI 放在这里.
让我知道如何像上面的屏幕截图一样快速显示 UIImageView.
我在 android 中发现了一些使用完整的东西,但在 iOS 中却没有.
链接:
对于任何一种View,添加curvedPercent
参数
func pathCurvedForView(givenView: UIView,curvedPercent:CGFloat) ->UIBezierPath{让箭头路径 = 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))箭头路径.close()返回箭头路径}func applyCurvedPath(givenView: UIView,curvedPercent:CGFloat) {守卫曲线百分比 <= 1 &&曲线百分比 >= 0 其他{返回}让 shapeLayer = CAShapeLayer(layer: givenView.layer)shapeLayer.path = self.pathCurvedForView(givenView: givenView,curvedPercent:curvedPercent).cgPathshapeLayer.frame = givenView.boundsshapeLayer.masksToBounds = truegivenView.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{让箭头路径 = 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))箭头路径.close()返回箭头路径}
结果
I want to mask and add some curve at bottom of image view. i have try below code .
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()!
}
}
But not getting success. Let me put UI here that i want to show in screen.
Let me know how to show UIImageView Like above screen shots in swift.
I have found some use full thing in android but not in iOS.
Link : Crescento View
As I said in my comment you need to make your own UIBezierPath
adding a quad curve in the bottom part of your path, the curvedPercent
will be how pronounced your curve will be, you can adjust it as you need it
Custom UIImageView class
@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
}
}
}
Result in Storyboard as is Designable
For any kind of View, added curvedPercent
parameter
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
}
How can I use it?
self.applyCurvedPath(givenView: self.customView,curvedPercent: 0.5)
Result for curvedPercent = 0.5
Result for curvedPercent = 0.1
Result for curvedPercent = 0.9
UPDATE
For inverted curve replace original pathCurvedForView
method by this one
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
}
Result
这篇关于如何在 UIImageView 底部应用曲线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 UIImageView 底部应用曲线?
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01