簡體   English   中英

帶有陰影的UIView,僅將頂部圓角和遮蓋的底部邊緣倒圓

[英]UIView with shadow, rounded only top corners and masked bottom edge

我正在努力實現只能圍繞頂部拐角和陰影的視圖。 整個視圖應在底部由超級視圖遮蓋,以防止陰影在子視圖之后重疊(陰影偏移應為(0,0))。

當我使用時:

    // Adding shadow
    let contentSubviewLayer = contentSubview.layer
    contentSubviewLayer.masksToBounds = false
    contentSubviewLayer.shadowOpacity = 0.3
    contentSubviewLayer.shadowRadius = 2.5
    contentSubviewLayer.shadowOffset = CGSizeMake(0, 0)
    contentSubviewLayer.cornerRadius = 5.0
    contentSubviewLayer.shadowPath = UIBezierPath(rect: contentSubview.bounds).CGPath
    self.layer.masksToBounds = true

我得到: 在此處輸入圖片說明

但是在添加遮罩的底部邊緣之后:

    // Adding maskView to round only top corners
    let maskLayer = CAShapeLayer()
    maskLayer.frame = self.contentSubview.bounds
    let roundedPath = UIBezierPath(roundedRect: maskLayer.bounds, byRoundingCorners: [UIRectCorner.TopLeft, UIRectCorner.TopRight], cornerRadii: CGSizeMake(CORNER_RADIUS, CORNER_RADIUS))
    maskLayer.fillColor = UIColor.whiteColor().CGColor
    maskLayer.backgroundColor = UIColor.clearColor().CGColor
    maskLayer.path = roundedPath.CGPath
    contentSubview.layer.mask = maskLayer

我越來越: 在此處輸入圖片說明

所需的效果應該是以上兩者的結合,並且應該類似於這樣, 在左右,左,右邊緣都帶有陰影 在此處輸入圖片說明

請幫忙! 先感謝您。

我傾向於使用一個名為PaintCode的應用程序來生成此類事情所需的Swift代碼。 最近我做了類似的事情,它為我吐了出來:

let context = UIGraphicsGetCurrentContext()

    //// Color Declarations
    let chiesi_light_grey = UIColor(red: 0.851, green: 0.851, blue: 0.851, alpha: 1.000)

    //// Shadow Declarations
    let chiesi_shadow = NSShadow()
    chiesi_shadow.shadowColor = chiesi_light_grey.colorWithAlphaComponent(0.8 * CGColorGetAlpha(chiesi_light_grey.CGColor))
    chiesi_shadow.shadowOffset = CGSize(width: 0.1, height: 4.1)
    chiesi_shadow.shadowBlurRadius = 2

    //// Rectangle Drawing
    let rectanglePath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 50, height: 54), byRoundingCorners: [UIRectCorner.BottomLeft, UIRectCorner.BottomRight], cornerRadii: CGSize(width: 8, height: 8))
    rectanglePath.closePath()
    CGContextSaveGState(context)
    CGContextSetShadowWithColor(context, chiesi_shadow.shadowOffset, chiesi_shadow.shadowBlurRadius, (chiesi_shadow.shadowColor as! UIColor).CGColor)
    UIColor.whiteColor().setFill()
    rectanglePath.fill()
    CGContextRestoreGState(context)

    chiesi_light_grey.setStroke()
    rectanglePath.lineWidth = 2
    rectanglePath.stroke()

也許會對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM