Ios 在Swift上创建neumorphic软UI是不可能的吗?

Ios 在Swift上创建neumorphic软UI是不可能的吗?,ios,swift,Ios,Swift,现在,我看到了太多关于Neumorphic软UI的设计,我对iOS上的实现(swift 4)一无所知。我看过很多关于这方面的教程,但对于iOS13+和SwiftUI,我并不想看到swift4 如果有人知道,请帮助我 为了实现这一点,Swift 4代码与Swift 5代码没有区别。因此,您应该能够使用您找到的任何非SwiftUI教程中的代码 不过,复制这一点的基础是需要两个阴影,一个位于视图的上方和左侧,另一个位于视图的下方和右侧 yourView.layer.masksToBounds = fa

现在,我看到了太多关于Neumorphic软UI的设计,我对iOS上的实现(swift 4)一无所知。我看过很多关于这方面的教程,但对于iOS13+和SwiftUI,我并不想看到swift4

如果有人知道,请帮助我


为了实现这一点,Swift 4代码与Swift 5代码没有区别。因此,您应该能够使用您找到的任何非SwiftUI教程中的代码

不过,复制这一点的基础是需要两个阴影,一个位于视图的上方和左侧,另一个位于视图的下方和右侧

yourView.layer.masksToBounds = false

let cornerRadius: CGFloat = 15
let shadowRadius: CGFloat = 2

let darkShadow = CALayer()
darkShadow.frame = bounds
darkShadow.backgroundColor = backgroundColor?.cgColor
darkShadow.shadowColor = UIColor(red: 0.87, green: 0.89, blue: 0.93, alpha: 1.0).cgColor
darkShadow.cornerRadius = cornerRadius
darkShadow.shadowOffset = CGSize(width: shadowRadius, height: shadowRadius)
darkShadow.shadowOpacity = 1
darkShadow.shadowRadius = shadowRadius
yourView.layer.insertSublayer(darkShadow, at: 0)

let lightShadow = CALayer()
lightShadow.frame = bounds
lightShadow.backgroundColor = backgroundColor?.cgColor
lightShadow.shadowColor = UIColor.white.cgColor
lightShadow.cornerRadius = cornerRadius
lightShadow.shadowOffset = CGSize(width: -shadowRadius, height: -shadowRadius)
lightShadow.shadowOpacity = 1
lightShadow.shadowRadius = shadowRadius
yourView.layer.insertSublayer(lightShadow, at: 0)
在上面的代码中有三个主要的东西是必需的,否则阴影将不会渲染

  • 视图必须将
    masksToBounds
    设置为
    false
    ——因为阴影将超出边界
  • 您必须为图层指定
    背景色
  • 您必须设置
    shadowOpacity
    ,因为默认值为
    0.0
    -设置
    1.0
    会产生与图像最接近的结果
  • 当按下视图时,您可能希望阴影翻转,因此只需切换两个子层的
    shadowOffset
    s即可

    lightShadow.shadowOffset = CGSize(width: shadowRadius, height: shadowRadius)
    darkShadow.shadowOffset = CGSize(width: -shadowRadius, height: -shadowRadius)
    
    当用户停止按时,重置它们

    lightShadow.shadowOffset = CGSize(width: -shadowRadius, height: -shadowRadius)
    darkShadow.shadowOffset = CGSize(width: shadowRadius, height: shadowRadius)
    

    对于
    UIControl
    您可能会在
    ishighlight
    覆盖的
    didSet
    中执行上述操作。

    找不到任何教程并不意味着不可能,但是为什么你仍然使用Swift 4?@Sweeper,因为我的公司仍然使用xcode 10进行构建和测试,所以如果我们移动了,我们将为此付出很多努力。在UIButton中使用此解决方案会隐藏按钮图像,因此在添加阴影效果后,你应该以编程方式添加新的图像按钮。