2017-09-27 202 views
15

我在我的iOS項目中創建了一個具有陰影的自定義UIView。 我的目標是將相同的漸變應用於陰影,因爲它在視圖的背景上。UIView陰影漸變

下面是我當前純色陰影外觀的一個示例。

Shadow Example 這是通過的UIView與下面的代碼的子類進行:

override func layoutSubviews() { 
    let gradientLayer = layer as! CAGradientLayer 
    gradientLayer.colors = [topColor.cgColor, bottomColor.cgColor] 
    gradientLayer.startPoint = CGPoint(x: startPointX, y: startPointY) 
    gradientLayer.endPoint = CGPoint(x: endPointX, y: endPointY) 
    layer.cornerRadius = cornerRadius 
    layer.shadowColor = shadowColor.cgColor 
    layer.shadowOffset = CGSize(width: shadowX, height: shadowY) 
    layer.shadowRadius = shadowBlur 
    layer.shadowOpacity = 1 

    let inset: CGFloat = bounds.width * 0.05 
    layer.shadowPath = UIBezierPath(roundedRect: bounds.insetBy(dx: inset, dy: 0.0), cornerRadius: cornerRadius).cgPath 
} 

我一直在創造第二梯度層,它掩蓋的陰影,但沒有運氣玩耍。請指向正確的方向!

回答

0

我相信@jacek是正確的,你推的你可以用CALayer陰影做什麼限制(這是沒有太多要誠實)

最好的東西做,因爲亞採解釋是創造你自己的影子,儘管另一個CAGradientLayer應用高斯模糊作爲影子。

另外,我不相信把你的代碼放在layoutSubviews裏面是最合適的地方。一旦配置好你的圖層就不需要改變太多,因此如果你的佈局改變很多,它會被調用很多。

通常,如果使用來自接口構建器的視圖,我會在configureBackgroundViews函數內將awakeFromNib這個邏輯稱爲例如。

0

使用我的代碼可用您的要求,我gitRepo https://github.com/Krishnarjun-Banoth/BlurView/tree/master/Shadows

第一步:只需將您的項目,請BlurEffect.swift文件。

第2步:然後簡單地使用下面分別爲每個視圖查看擴展方法。

testView.applyGradient(colours: [UIColor.blue,UIColor.orange]) //pass your required colours. 
    testView.blur(blurRadius: 5.0) 

注意:啓發自@JacekGłazik答案和FlexMonkey的教程。