如何在UIView下繪製陰影?

我正在嘗試在Cocoa Touch中的UIView底部邊緣下方繪製陰影。 我知道我應該使用CGContextSetShadow()來繪製陰影,可是Quartz 2D編程指南有點模糊: 編程

  1. 保存圖形狀態。
  2. 調用函數CGContextSetShadow ,傳遞適當的值。
  3. 執行要向其應用陰影的全部圖形。
  4. 恢復圖形狀態

我在UIView子類中嘗試瞭如下方法: swift

- (void)drawRect:(CGRect)rect {
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextSaveGState(currentContext);
    CGContextSetShadow(currentContext, CGSizeMake(-15, 20), 5);
    CGContextRestoreGState(currentContext);
    [super drawRect: rect];
}

..但這對我不起做用,我對(a)接下來要去哪裏和(b)是否須要對UIView進行任何操做以使其工做感到有些UIViewide


#1樓

相同的解決方案,只是提醒您:您能夠直接在情節提要中定義陰影。 函數

例如: 工具

在此處輸入圖片說明


#2樓

對於在這裏嘗試了全部答案後都沒法正常工做的人(以我本身!),只需確保在「屬性」檢查器中未啓用「 剪輯子視圖」 ... ui


#3樓

使用Interface Builder的簡單幹淨的解決方案 spa

在您的項目中添加一個名爲UIView.swift的文件(或僅將其粘貼到任何文件中): code

import UIKit

@IBDesignable extension UIView {

    /* The color of the shadow. Defaults to opaque black. Colors created
    * from patterns are currently NOT supported. Animatable. */
    @IBInspectable var shadowColor: UIColor? {
        set {
            layer.shadowColor = newValue!.CGColor
        }
        get {
            if let color = layer.shadowColor {
                return UIColor(CGColor:color)
            }
            else {
                return nil
            }
        }
    }

    /* The opacity of the shadow. Defaults to 0. Specifying a value outside the
    * [0,1] range will give undefined results. Animatable. */
    @IBInspectable var shadowOpacity: Float {
        set {
            layer.shadowOpacity = newValue
        }
        get {
            return layer.shadowOpacity
        }
    }

    /* The shadow offset. Defaults to (0, -3). Animatable. */
    @IBInspectable var shadowOffset: CGPoint {
        set {
            layer.shadowOffset = CGSize(width: newValue.x, height: newValue.y)
        }
        get {
            return CGPoint(x: layer.shadowOffset.width, y:layer.shadowOffset.height)
        }
    }

    /* The blur radius used to create the shadow. Defaults to 3. Animatable. */
    @IBInspectable var shadowRadius: CGFloat {
        set {
            layer.shadowRadius = newValue
        }
        get {
            return layer.shadowRadius
        }
    }
}

而後,它將在「界面構建器」中爲「實用工具面板」>「屬性檢查器」中的每一個視圖提供: 圖片

實用程序面板

您如今能夠輕鬆設置陰影。 ip

筆記:
-僅在運行時,陰影不會出如今IB中。
-正如Mazen Kasser所說

對於未能經過此方法工做的人,請確保未啓用剪輯子視圖( clipsToBounds


#4樓

您能夠嘗試這個....您能夠使用這些值。 shadowRadius指示模糊量。 shadowOffset指示陰影的位置。

斯威夫特2.0

let radius: CGFloat = demoView.frame.width / 2.0 //change it to .height if you need spread for height
let shadowPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 2.1 * radius, height: demoView.frame.height))
//Change 2.1 to amount of spread you need and for height replace the code for height

demoView.layer.cornerRadius = 2
demoView.layer.shadowColor = UIColor.blackColor().CGColor
demoView.layer.shadowOffset = CGSize(width: 0.5, height: 0.4)  //Here you control x and y
demoView.layer.shadowOpacity = 0.5
demoView.layer.shadowRadius = 5.0 //Here your control your blur
demoView.layer.masksToBounds =  false
demoView.layer.shadowPath = shadowPath.CGPath

斯威夫特3.0

let radius: CGFloat = demoView.frame.width / 2.0 //change it to .height if you need spread for height 
let shadowPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 2.1 * radius, height: demoView.frame.height)) 
//Change 2.1 to amount of spread you need and for height replace the code for height

demoView.layer.cornerRadius = 2
demoView.layer.shadowColor = UIColor.black.cgColor
demoView.layer.shadowOffset = CGSize(width: 0.5, height: 0.4)  //Here you control x and y
demoView.layer.shadowOpacity = 0.5
demoView.layer.shadowRadius = 5.0 //Here your control your blur
demoView.layer.masksToBounds =  false
demoView.layer.shadowPath = shadowPath.cgPath

傳播實例

傳播實例

建立基本陰影

demoView.layer.cornerRadius = 2
    demoView.layer.shadowColor = UIColor.blackColor().CGColor
    demoView.layer.shadowOffset = CGSizeMake(0.5, 4.0); //Here your control your spread
    demoView.layer.shadowOpacity = 0.5 
    demoView.layer.shadowRadius = 5.0 //Here your control your blur

Swift 2.0中的基本Shadow示例

輸出值


#5樓

迅捷3

extension UIView {
    func installShadow() {
        layer.cornerRadius = 2
        layer.masksToBounds = false
        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOffset = CGSize(width: 0, height: 1)
        layer.shadowOpacity = 0.45
        layer.shadowPath = UIBezierPath(rect: bounds).cgPath
        layer.shadowRadius = 1.0
    }
}
相關文章
相關標籤/搜索