0
1

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 3 years have passed since last update.

フルスクリーンではない画面作成Part2〜viewとButtonに影をつけよう編〜

Last updated at Posted at 2020-08-10

前回までの流れ

フルスクリーンではないViewを設置しよう!!
前回の記事を見ていない方はぜひ上のリンクから入ってみてください!!

現在完成しているコード

ViewController.swift
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setRegistVIew()
        setSaveButton()
    }
    
    func setRegistVIew(){
        let screenFrameX = self.view.frame.width/2
        let screenFrameY = self.view.frame.width/2
        let viewX = 300
        let viewY = viewX*4/3
        registView.frame = CGRect.init(x: Int(screenFrameX)-viewX/2, y: Int(screenFrameY), width: viewX, height: viewY)
    }

    func setSaveButton(){
        saveButton.layer.cornerRadius = 10
        saveButton.tintColor = .white
        saveButton.backgroundColor = .red
    }

    @IBOutlet var registView: UIView!
//OutletでUIButtonを追加
    @IBOutlet weak var saveButton: UIButton!
    
    @IBAction func showRegistView(_ sender: Any) {
        self.view.addSubview(registView)
    }
//ActionでUIButtonを追加
    @IBAction func saveButton(_ sender: Any) {
//Viewを消す
        registView.removeFromSuperview()
    }
    
    
}

今回これに加えて、viewとButtonに影をつけます!!

方法はとても簡単で前回作成した関数setRegistとsaveButtonに要素を入れていくだけで完成します!!

加えるコードはこちら!!
[RegistView]

ViewController.swiftの関数:SetRegistView内に追
//Viewに丸みを持たせる
        registView.layer.cornerRadius = 20
//影の大きさを決定する(width:幅、height:高さ)
        registView.layer.shadowOffset = CGSize(width: 0, height: 2)
//影の色を決定する
        registView.layer.shadowColor = UIColor.darkGray.cgColor
//影の濃さを決定する
        registView.layer.shadowOpacity = 0.5
//影の丸みを決定する
        registView.layer.shadowRadius = 4

[SaveButton]

ViewController.swiftの関数:SaveButton内に追加!!
//影の大きさを決定する(width:幅、height:高さ)
        saveButton.layer.shadowOffset = CGSize(width: 0, height: 2)
//影の色を決定する
        saveButton.layer.shadowColor = UIColor.darkGray.cgColor
//影の濃さを決定する
        saveButton.layer.shadowOpacity = 0.5
//影の丸みを決定する
        saveButton.layer.shadowRadius = 4

変更した部分はこんな感じになる

ViewController.swift
    func setRegistVIew(){
        let screenFrameX = self.view.frame.width/2
        let screenFrameY = self.view.frame.width/2
        let viewX = 300
        let viewY = viewX*4/3

        registView.layer.cornerRadius = 20
        registView.layer.shadowOffset = CGSize(width: 0, height: 2)
        registView.layer.shadowColor = UIColor.darkGray.cgColor
        registView.layer.shadowOpacity = 0.5
        registView.layer.shadowRadius = 4
        registView.frame = CGRect.init(x: Int(screenFrameX)-viewX/2, y: Int(screenFrameY), width: viewX, height: viewY)
        setSaveButton()
    }
    
    func setSaveButton(){
        saveButton.layer.cornerRadius = 10
        saveButton.tintColor = .white
        saveButton.backgroundColor = .red
        saveButton.layer.shadowOffset = CGSize(width: 0, height: 2)
        saveButton.layer.shadowColor = UIColor.darkGray.cgColor
        saveButton.layer.shadowOpacity = 0.5
        saveButton.layer.shadowRadius = 4
    }

こんな感じになっていたら成功です!!
Simulator Screen Shot - iPhone 11 - 2020-08-10 at 11.39.45.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?