10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swift2 背景グラデーションの各種設定

Last updated at Posted at 2016-01-12

背景グラデーション

色の設定と角度の設定
img.png

ViewController.sift
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let gradientLayer = CAGradientLayer()
        let angle:String = "0"
        
        let start:CGPoint
        let end:CGPoint
        
        
        gradientLayer.frame = self.view.bounds
        
        // グラデーションの色
        let red = UIColor.redColor().CGColor
        let green = UIColor.greenColor().CGColor
        let yellow = UIColor.yellowColor().CGColor
        let blue = UIColor.blueColor().CGColor
        let purple = UIColor.purpleColor().CGColor
        
        // グラデーションの角度
        switch (angle) {
        case "0":
            start = CGPointMake(0.5, 0.0);
            end = CGPointMake(0.5, 1.0);
            break;
        case "45":
            start = CGPointMake(1.0, 0.0)
            end = CGPointMake(0.0, 1.0);
            break;
        case "90":
            start = CGPointMake(1.0, 0.5);
            end = CGPointMake(0.0, 0.5);
            break;
        default:
            start = CGPointMake(0.5, 0.0)
            end = CGPointMake(0.5, 1.0)
            break;
        }
        
        gradientLayer.colors = [red, green, yellow, blue, purple]
        gradientLayer.locations = [0.0, 0.25, 0.5, 0.75, 1.0]
        gradientLayer.startPoint = start
        gradientLayer.endPoint = end
        // self.view.layer.addSublayer(gradientLayer)
        self.view.layer.insertSublayer(gradientLayer, atIndex: 0)
    }
}
10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?