0
0

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.

NavigationBarにItem(ボタン)を追加 押したらiPhoneのホーム画面のドックみたいなUIViewを下から表示

Posted at

完成形

EE0F3C7E-D57F-4A20-9FED-87CF9964EB5B_1_201_a.jpeg F183359C-ECD9-4F8E-85FA-CBA9E5FEF31E_1_201_a.jpeg

機能説明

  • NavigationBarの右端に設置したItemを押すと、下からiPhoneのドックみたいなUIViewを表示させます。

コードと簡単解説

NavigationBarにItem(ボタン)を追加

  • NavigationBarの右端に.actionアイコンを設定して追加しています。

  • action: #selector(showUnderView)は、UIViewを上げ下げする為の処理を設定しています。

    override func viewDidLoad() {
       super.viewDidLoad()
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(showUnderView))
        
    }
  • 0.3秒でUIViewとUIButtonをanimations:{}で指定した位置まで移動させます。 
           ~~~一部省略~~~

    var underView = UIView()
    var underViewButton = UIButton()

           ~~~一部省略~~~

    @objc func showUnderView(){
        
        if underView.frame.origin.y == self.view.frame.maxY{
            
            UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.underView.frame.origin.y = self.view.frame.maxY - (self.view.frame.maxY / 7.5)}, completion: nil)
            
            UIButton.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.underViewButton.frame.origin.y = self.underView.frame.origin.y + 10}, completion: nil)
            
        }else if underView.frame.origin.y == self.view.frame.maxY - (self.view.frame.maxY / 7.5){
            
            UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.underView.frame.origin.y = self.view.frame.maxY}, completion: nil)

            UIButton.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.underViewButton.frame.origin.y = self.underView.frame.origin.y + 3}, completion: nil)
        }
    }

iPhoneのホーム画面のドックみたいなUIViewを作成

  • ホーム画面のドックみたいなUIViewを作成
           ~~~一部省略~~~

    var underView = UIView()

           ~~~一部省略~~~

func createUnderView(){
        
        underView = UIView(frame: CGRect(x: view.frame.minX + 5, y: self.view.frame.maxY, width: view.frame.size.width - 10, height: view.frame.size.height / 9))
        
        underView.layer.cornerRadius = 15.0   //背景色を設定
        underView.backgroundColor = .systemGray2
        underView.alpha = 0.35   //透過度を設定
        
        view.addSubview(underView)
        
    }

全てのコード

ViewController
import UIKit

class ViewController: UIViewController {

    var underView = UIView()
    var underViewButton = UIButton()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(showUnderView))
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        createUnderView()
        createUnderViewButton(yPoint: underView.frame.origin.y)
    }

    @objc func showUnderView(){
        
        if underView.frame.origin.y == self.view.frame.maxY{
            
            UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.underView.frame.origin.y = self.view.frame.maxY - (self.view.frame.maxY / 7.5)}, completion: nil)
            
            UIButton.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.underViewButton.frame.origin.y = self.underView.frame.origin.y + 10}, completion: nil)
            
        }else if underView.frame.origin.y == self.view.frame.maxY - (self.view.frame.maxY / 7.5){
            
            UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.underView.frame.origin.y = self.view.frame.maxY}, completion: nil)

            UIButton.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.underViewButton.frame.origin.y = self.underView.frame.origin.y + 3}, completion: nil)
        }
    }
    
    func createUnderView(){
        
        underView = UIView(frame: CGRect(x: view.frame.minX + 5, y: self.view.frame.maxY, width: view.frame.size.width - 10, height: view.frame.size.height / 9))
        
        underView.layer.cornerRadius = 15.0
        underView.backgroundColor = .systemGray2
        underView.alpha = 0.35
        
        view.addSubview(underView)
        
    }
    
    func createUnderViewButton(yPoint:CGFloat){

        underViewButton = UIButton(frame: CGRect(x: underView.frame.minX + 10, y: yPoint + 10, width: underView.frame.size.height - 20, height:underView.frame.size.height - 20))
        
        underViewButton.backgroundColor = UIColor.systemIndigo
        underViewButton.layer.cornerRadius = 15.0
        underViewButton.setTitle("Button", for: .normal)
        underViewButton.setTitleColor(.white, for: .normal)
        underViewButton.addTarget(self, action: #selector(test), for: .touchDown)
        
        view.addSubview(underViewButton)
        print("underViewButton表示")

    }
    
    @objc func test(){
        print("underViewButton押されました")
    }
    
}

終わり

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

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?