在Swift中设置按钮上的背景渐变 [英] Set Background Gradient on Button in Swift

查看:133
本文介绍了在Swift中设置按钮上的背景渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何在按钮上设置背景渐变(不将背景渐变设置为图像)。这与Android有很大不同。

I have no idea how to set the background gradient on a button (without making the background gradient an image). This is so different from Android.

这是一个我必须定义可退回渐变方案的类:

Here's a class I have to define a returnable gradient scheme:

import UIKit

extension CAGradientLayer {

    func backgroundGradientColor() -> CAGradientLayer {
        let topColor = UIColor(red: (0/255.0), green: (153/255.0), blue:(51/255.0), alpha: 1)
        let bottomColor = UIColor(red: (0/255.0), green: (153/255.0), blue:(255/255.0), alpha: 1)

        let gradientColors: [CGColor] = [topColor.CGColor, bottomColor.CGColor]
        let gradientLocations: [Float] = [0.0, 1.0]

        let gradientLayer: CAGradientLayer = CAGradientLayer()
        gradientLayer.colors = gradientColors
        gradientLayer.locations = gradientLocations

        return gradientLayer

    }
}

我可以使用它来设置我的整个视图的背景如下:

I can use this to set the background of my entire view with the following:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let background = CAGradientLayer().backgroundGradientColor()
        background.frame = self.view.bounds
        self.view.layer.insertSublayer(background, atIndex: 0)
    }
    //...
}

但是如何访问按钮的视图并插入子图层或类似的东西?我用谷歌搜索了几个小时,找不到任何有用的东西。我很失落。

But how can I access the view of the button and insert the sublayer or something like that? I've Googled for a couple hours and can't find anything helpful. I'm pretty lost with this.

推荐答案

你的代码工作正常。您只需记住每次都设置渐变的帧。最好只让渐变类别为你设置视图的框架。

Your code works fine. You just have to remember to set the gradient's frame every time. It is better to just make the gradient category also set the frame of the view for you.

这样你就不会忘记并且它适用。

That way you don't forget and it applies fine.

extension UIView {
    func applyGradient(colours: [UIColor]) -> Void {
        self.applyGradient(colours, locations: nil)
    }

    func applyGradient(colours: [UIColor], locations: [NSNumber]?) -> Void {
        let gradient: CAGradientLayer = CAGradientLayer()
        gradient.frame = self.bounds
        gradient.colors = colours.map { $0.CGColor }
        gradient.locations = locations
        self.layer.insertSublayer(gradient, atIndex: 0)
    }
}

class ViewController: UIViewController {

    @IBOutlet weak var btn: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.btn.titleLabel?.textColor = UIColor.whiteColor()

        self.btn.applyGradient([UIColor.yellowColor(), UIColor.blueColor()])
        self.view.applyGradient([UIColor.yellowColor(), UIColor.blueColor(), UIColor.redColor()], locations: [0.0, 0.5, 1.0])
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

按钮是视图。您将渐变应用于它,就像将其应用于任何其他视图一样。

Buttons are views. You apply gradients to it the same way you would apply it to any other view.

这篇关于在Swift中设置按钮上的背景渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆