Swift - 点击

时间:2016-12-27 14:10:20

标签: ios swift

我正在创建循环按钮,如下所示:

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
            let button = GradientButton.createCircularButton(20, yPos: 20, width: 30, height: 30, circleValue:20)

        button.addTarget(self, action: #selector(ViewController.didCircleBtnTouched(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(button)
  }
 }


enum ColorType {
    case red, green, orange
}

class GradientButton: UIButton {
    var colorType: ColorType?   


public class func createCircularButton(xPos: CGFloat, yPos: CGFloat, width: CGFloat, height: CGFloat, circleValue: Int) -> GradientButton {
    let button = GradientButton()

    button.titleLabel!.font =  UIFont(name:"HelveticaNeue", size: 12)
    let buttonFrame = CGRect(x: xPos, y: yPos, width: width, height: height)

    button.frame = buttonFrame

    button.backgroundColor = UIColor.clearColor()
    button.backgroundColor = UIColor.whiteColor()


    button.layer.borderWidth = 1
    button.layer.cornerRadius = 15.0
    //this helps making it circular not rectangle
    button.clipsToBounds = true


    let red = UIColor(red:134/255, green:16/255, blue:1/255, alpha:1.0)//red
    let green = UIColor(red:0/255, green:136/255, blue:43/255, alpha:1.0)//green
    let orange = UIColor(red:243/255, green:144/255, blue:25/255, alpha:1.0)//orange

    if(circleValue <= 3){
        button.colorType = .green
        button.layer.borderColor = UIColor.greenColor().CGColor
        button.setTitleColor(green, forState: .Normal)
    } else if(circleValue > 3 && circleValue <= 7){
        button.colorType = .orange
        button.layer.borderColor = UIColor.orangeColor().CGColor
        button.setTitleColor(orange, forState: .Normal)
    } else if(circleValue > 7){
        button.colorType = .red
        button.layer.borderColor = UIColor.redColor().CGColor
        button.setTitleColor(red, forState: .Normal)

    }       
    button.setTitle("\(circleValue)", forState: .Normal)
    return button
}
}

点击按钮后,我按照以下方式应用渐变:

 func didCircleBtnTouched(sender:GradientButton!){
             ApplyGradientToButton(sender)
 }



func ApplyGradientToButton(sender: GradientButton!){
    var color1 = UIColor()
    var color2 = UIColor()

   if(sender.colorType == .red){
        color1 = UIColor(red:134/255, green:16/255, blue:1/255, alpha:1.0)
        color2 = UIColor(red:200/255, green:37/255, blue:6/255, alpha:1.0)
    }
    else if(sender.colorType == .green) {
        color1 = UIColor(red:0/255, green:136/255, blue:43/255, alpha:1.0)
        color2 = UIColor(red:112/255, green:191/255, blue:65/255, alpha:1.0)
    }
    else if(sender.colorType == .orange) {
        color1 = UIColor(red:200/255, green:110/255, blue:1/255, alpha:1.0)
        color2 = UIColor(red:239/255, green:149/255, blue:26/255, alpha:1.0)

    }

    sender.setTitleColor(UIColor.whiteColor(), forState: .Normal)

    var layer = sender.layer

    layer.shadowColor = UIColor.blackColor().CGColor
    layer.shadowOffset = CGSize(width: 0.0, height: 5.0)
    layer.shadowOpacity = 1.0
    layer.shadowRadius = 10.0


    sender.applyGradient([color2, color1], locations: [0.0, 0.90])
}

我需要在按钮点击时在圆形按钮周围显示阴影。但阴影没有显示出来。 我认为button.clipsToBounds = true负责,因为我没有使用此属性时出现阴影。不幸的是,通过删除此属性,我的圆形按钮在单击后变为矩形,这是不可取的。

有没有办法在不改变按钮形状的情况下显示阴影? 请指教?

当前输出:

Current

预期产出:

Expected

2 个答案:

答案 0 :(得分:4)

嗯,你不能剪辑/掩饰边界并添加阴影,因为它也会被剪裁或遮盖。

解决方案是在单独的图层上添加阴影并将图像添加为子图层。

这里有很多帖子描述:

https://stackoverflow.com/a/25591916/312312

Swift - Problems with corner radius and drop shadow

还有更多

答案 1 :(得分:-2)

将此代码放入viewController.swift文件..(或任何其他文件)

extension UIView {
    func setRadiusWithShadow(_ radius: CGFloat? = nil) {
        self.layer.cornerRadius = radius ?? self.frame.width / 2
        self.layer.shadowColor = UIColor.darkGray.cgColor
        self.layer.shadowOffset = CGSize(width: 1.5, height: 1.5)
        self.layer.shadowRadius = 1.0
        self.layer.shadowOpacity = 0.7
        self.layer.masksToBounds = false
    }

}

然后申请按钮..

btnEdit.setRadiusWithShadow()

是的......只看输出......一切都已完成......

注意:您可以根据需要更改 CGSize(宽度:1.5,高度:1.5) 值...