2017-09-27 199 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的教程。