LoginSignup
0
1

More than 1 year has passed since last update.

CAGradientLayer( ) を少し使ってみました。

Posted at

今回の内容

B74F482A-9CA3-48F0-B256-FDC044BBB008_1_105_c.jpeg A11CDFB7-48A4-45AD-ACBA-84F1E2B33408_1_105_c.jpeg 102EFB9C-EAFF-407F-A4A7-EBF4F5444E08_1_105_c.jpeg

機能説明

  • Timer( )を使って、一定秒後にランダムに背景色が変わってるように見せる。

全体コード

import UIKit

class ViewController: UIViewController {

    var gradient = CAGradientLayer()

    override func viewDidLoad() {
        super.viewDidLoad()

        gradient.frame = CGRect(x: self.view.frame.minX, y: self.view.frame.minY, width: self.view.frame.size.width, height: self.view.frame.size.height)

        Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(changeGradient), userInfo: nil, repeats: true)

    }

    @objc func changeGradient(){

        UIView.animate(withDuration: 1,delay: 0,options: .allowUserInteraction ,animations: {[self] in

            gradient.colors = [

                UIColor(red: CGFloat.random(in: 0...255) / 255,
                        green:CGFloat.random(in:0...255) / 255,
                        blue: CGFloat.random(in: 0...255) / 255,
                        alpha: CGFloat.random(in: 0.0...1.0)).cgColor,
                UIColor(red: CGFloat.random(in: 0...255) / 255,
                        green:CGFloat.random(in: 0...255) / 255,
                        blue: CGFloat.random(in: 0...255) / 255,
                        alpha: CGFloat.random(in: 0.0...1.0)).cgColor
            ]  

            gradient.startPoint = CGPoint(x: 0.5, y: 0.5)
            gradient.endPoint = CGPoint(x: 0.5, y: 0)
            self.view.layer.insertSublayer(gradient, at: 0)

        },completion: nil)
    }

}

終わり

間違いの指摘、ご質問などありましたら、コメントまでお願い致します。

0
1
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
0
1