单击按钮时需要去除内部阴影

时间:2018-06-20 22:59:06

标签: swift uibutton shadow

这是我的扩展程序,用于在按钮中添加内部阴影。 我会在所有情况下都希望将阴影放到按钮或视图中。

public extension UIView

{     //不同的内部阴影样式     公共枚举innerShadowSide     {         大小写全部,左,右,上,下,topAndLeft,topAndRight,bottomAndLeft,bottomAndRight,exceptLeft,exceptRight,exceptTop,exceptBottom     }

// define function to add inner shadow
public func addInnerShadow(onSide: innerShadowSide, shadowColor: UIColor, shadowSize: CGFloat, cornerRadius: CGFloat = 0.0, shadowOpacity: Float) -> CAShapeLayer

{
    // define and set a shaow layer
    let shadowLayer = CAShapeLayer()
    shadowLayer.frame = bounds
    shadowLayer.shadowColor = shadowColor.cgColor
    shadowLayer.shadowOffset = CGSize(width: 0.0, height: 0.0)
    shadowLayer.shadowOpacity = shadowOpacity
    shadowLayer.shadowRadius = shadowSize
    shadowLayer.fillRule = kCAFillRuleEvenOdd

    // define shadow path
    let shadowPath = CGMutablePath()

    // define outer rectangle to restrict drawing area
    let insetRect = bounds.insetBy(dx: -shadowSize * 2.0, dy: -shadowSize * 2.0)

    // define inner rectangle for mask
    let innerFrame: CGRect = { () -> CGRect in
        switch onSide
        {

        case .all:
            return CGRect(x: 0.0, y: 0.0, width: frame.size.width, height: frame.size.height)
        case .left:
            return CGRect(x: 0.0, y: -shadowSize * 2.0, width: frame.size.width + shadowSize * 2.0, height: frame.size.height + shadowSize * 4.0)
        case .right:
            return CGRect(x: -shadowSize * 2.0, y: -shadowSize * 2.0, width: frame.size.width + shadowSize * 2.0, height: frame.size.height + shadowSize * 4.0)
        case .top:
            return CGRect(x: -shadowSize * 2.0, y: 0.0, width: frame.size.width + shadowSize * 4.0, height: frame.size.height + shadowSize * 2.0)
        case.bottom:
            return CGRect(x: -shadowSize * 2.0, y: -shadowSize * 2.0, width: frame.size.width + shadowSize * 4.0, height: frame.size.height + shadowSize * 2.0)
        case .topAndLeft:
            return CGRect(x: 0.0, y: 0.0, width: frame.size.width + shadowSize * 2.0, height: frame.size.height + shadowSize * 2.0)
        case .topAndRight:
            return CGRect(x: -shadowSize * 2.0, y: 0.0, width: frame.size.width + shadowSize * 2.0, height: frame.size.height + shadowSize * 2.0)
        case .bottomAndLeft:
            return CGRect(x: 0.0, y: -shadowSize * 2.0, width: frame.size.width + shadowSize * 2.0, height: frame.size.height + shadowSize * 2.0)
        case .bottomAndRight:
            return CGRect(x: -shadowSize * 2.0, y: -shadowSize * 2.0, width: frame.size.width + shadowSize * 2.0, height: frame.size.height + shadowSize * 2.0)
        case .exceptLeft:
            return CGRect(x: -shadowSize * 2.0, y: 0.0, width: frame.size.width + shadowSize * 2.0, height: frame.size.height)
        case .exceptRight:
            return CGRect(x: 0.0, y: 0.0, width: frame.size.width + shadowSize * 2.0, height: frame.size.height)
        case .exceptTop:
            return CGRect(x: 0.0, y: -shadowSize * 2.0, width: frame.size.width, height: frame.size.height + shadowSize * 2.0)
        case .exceptBottom:
            return CGRect(x: 0.0, y: 0.0, width: frame.size.width, height: frame.size.height + shadowSize * 2.0)
        }
    }()

    // add outer and inner rectangle to shadow path
    shadowPath.addRect(insetRect)
    shadowPath.addRect(innerFrame)

    // set shadow path as show layer's
    shadowLayer.path = shadowPath

    // add shadow layer as a sublayer
    layer.addSublayer(shadowLayer)

    // hide outside drawing area
    clipsToBounds = true

    return shadowLayer

}

} 当我第一次看到我的按钮时,我将使用以下代码: 在这种情况下,我想在所有按钮中都加入阴影,然后使用黑色。

var shadowLayer : CAShapeLayer?

            self.shadowLayer = okButton.addInnerShadow(onSide: UIView.innerShadowSide.all, shadowColor: UIColor.black, shadowSize: 10.0, shadowOpacity: 10.0)

当我在函数中单击按钮时,我需要删除此内部阴影:

       @IBAction func btnAction(_ sender: DLRadioButton) {
    if sender.tag == 1
    {
        statoBici = "ok"
        print(statoBici)
        setConfermaButton()
        self.shadowLayer.removeFromSuperLayer()


    }

1 个答案:

答案 0 :(得分:0)

修改您的UIView函数以返回CAShapeLayer:

public func addInnerShadow(onSide: innerShadowSide, shadowColor: UIColor, shadowSize: CGFloat, cornerRadius: CGFloat = 0.0, shadowOpacity: Float) -> CAShapeLayer
{
.
.
. 
  return shadowLayer
}

然后将变量或成员变量添加到您的类中:

class ViewController: UIViewController {
  var shadowLayer : CAShapeLayer!



 override func viewDidLoad() {
    super.viewDidLoad()  
    self.shadowLayer = btnShadow.addInnerShadow(onSide: UIView.innerShadowSide.all, shadowColor: UIColor.black, shadowSize: 10.0, shadowOpacity: 10.0)
  }
}

最后,从按钮中删除阴影层:

  @IBAction func btnAction(_ sender: Any) {
        if let btn = sender as? UIButton
        {
          if btn.tag == 1 {
            self.shadowLayer.removeFromSuperlayer()
          }
        }
      }

enter image description here enter image description here