横から出でくるViewの作成

#完成形はこんな感じです
<img width="150" alt="45FE0102-9A13-41A4-9965-61D81A644D72_1_201_a.jpeg" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1466343/4e08482e-b310-e39d-6b89-5682870c63b6.jpeg"> <img width="151" alt="3047E16A-1C15-4B2C-9622-864C827BC620_1_201_a.jpeg" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1466343/27227bc4-a7fc-a433-17ee-65d2ac10b110.jpeg"> <img width="151" alt="73B7756C-C02D-4F48-8980-F4C826730D6A_1_201_a.jpeg" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1466343/2c4944e7-5f07-2a34-b52b-533946c082cf.jpeg">


#機能説明
* 画面の真ん中のleftSideViewButtonを押すと、左からUIViewが表示されます。
* 表示されたViewには、UIButtonを表示しています。その、Buttonを押すと右からもう一つUIViewが表示されます。

#コードと簡単解説

##左から表示されるUIView

* 左から表示するUIViewの作成と作成したUIViewを動かす処理を作成します

* `UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.leftSlideView.frame.origin.x = 0}, completion: nil)`は0.7秒でleftSlideViewのX軸を0の位置までアニメーションさせます。

```Swift:

 var leftSlideView = UIView()

           ~~~一部省略~~~

 func createLeftSlideView(){        

        leftSlideView = UIView(frame: CGRect(x: self.view.frame.minX - self.view.frame.midX, y: self.view.frame.minY + 80, width: self.view.frame.size.width / 2, height: self.view.frame.size.height - 160))
    
        leftSlideView.backgroundColor = UIColor.systemGreen
        leftSlideView.layer.cornerRadius = 20.0

        self.view.addSubview(leftSlideView)
    }

    func leftSlideViewShowHidden(){
        
        if leftSlideView.frame.origin.x == self.view.frame.minX - self.view.frame.midX{
            
            UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.leftSlideView.frame.origin.x = 0}, completion: nil)
            createButton(xPoint: self.leftSlideView.frame.origin.x)
            
        }else if leftSlideView.frame.origin.x == self.view.frame.minX{
            
            UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.leftSlideView.frame.origin.x = self.view.frame.minX - self.view.frame.midX}, completion: nil)

        }        
    } 
``` 
##右から表示されるUIView

*  右から表示するUIViewの作成と作成したUIViewを動かす処理を作成します

* rightSlideViewShowHidden( )を持った、UIButtonをleftSlideViewに作成します。

```Swift
 var rightSlideView = UIView()

           ~~~一部省略~~~

 func createRightSlideView(){
        
        rightSlideView = UIView(frame: CGRect(x: self.view.frame.maxX, y: self.view.frame.minY + 80, width: self.view.frame.size.width / 2, height: self.view.frame.size.height - 160))
        
        rightSlideView.backgroundColor = UIColor.systemIndigo
        rightSlideView.layer.cornerRadius = 20.0
        
        self.view.addSubview(rightSlideView)
        
    }

func createButton(xPoint:CGFloat){
        
        let button = UIButton(frame: CGRect(x: xPoint + 5, y: leftSlideView.frame.minY - 70, width: leftSlideView.frame.size.width - 10, height: leftSlideView.frame.size.height / 12))
        
        button.setTitle("ShowRightView", for: .normal)
        button.setTitleColor(UIColor.black, for: .normal)
        button.backgroundColor = UIColor.white
        button.layer.cornerRadius = 20.0
        button.addTarget(self, action: #selector(rightSlideViewShowHidden), for: .touchDown)
        
        leftSlideView.addSubview(button)
        
    }
    
    @objc func rightSlideViewShowHidden(sender:UIButton){
        
        if rightSlideView.frame.origin.x == self.view.frame.maxX{
            
            UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.rightSlideView.frame.origin.x = self.view.frame.maxX - self.view.frame.midX}, completion: nil)
            
        }else if rightSlideView.frame.origin.x == self.view.frame.maxX - self.view.frame.midX{
            
            UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.rightSlideView.frame.origin.x = self.view.frame.maxX}, completion: nil)
            
        }
        
    }
```

##全体コード
```Swift:ViewController
import UIKit

class ViewController: UIViewController {
        
    var leftSlideView = UIView()
    var rightSlideView = UIView()
        
    override func viewDidLoad() {
        super.viewDidLoad()
                       
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
               
        createLeftSlideView()
        createRightSlideView()
        
    }

    func createLeftSlideView(){
        
        leftSlideView = UIView(frame: CGRect(x: self.view.frame.minX - self.view.frame.midX, y: self.view.frame.minY + 80, width: self.view.frame.size.width / 2, height: self.view.frame.size.height - 160))
    
        leftSlideView.backgroundColor = UIColor.systemGreen
        leftSlideView.layer.cornerRadius = 20.0

        self.view.addSubview(leftSlideView)
    }
        
    func createRightSlideView(){
        
        rightSlideView = UIView(frame: CGRect(x: self.view.frame.maxX, y: self.view.frame.minY + 80, width: self.view.frame.size.width / 2, height: self.view.frame.size.height - 160))
        
        rightSlideView.backgroundColor = UIColor.systemIndigo
        rightSlideView.layer.cornerRadius = 20.0
        
        self.view.addSubview(rightSlideView)
        
    }
    
    func leftSlideViewShowHidden(){
        
        if leftSlideView.frame.origin.x == self.view.frame.minX - self.view.frame.midX{
            
            UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.leftSlideView.frame.origin.x = 0}, completion: nil)
            createButton(xPoint: self.leftSlideView.frame.origin.x)
            
        }else if leftSlideView.frame.origin.x == self.view.frame.minX{
            
            UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.leftSlideView.frame.origin.x = self.view.frame.minX - self.view.frame.midX}, completion: nil)

        }        
    }
    
    func createButton(xPoint:CGFloat){
        
        let button = UIButton(frame: CGRect(x: xPoint + 5, y: leftSlideView.frame.minY - 70, width: leftSlideView.frame.size.width - 10, height: leftSlideView.frame.size.height / 12))
        
        button.setTitle("ShowRightView", for: .normal)
        button.setTitleColor(UIColor.black, for: .normal)
        button.backgroundColor = UIColor.white
        button.layer.cornerRadius = 20.0
        button.addTarget(self, action: #selector(rightSlideViewShowHidden), for: .touchDown)
        
        leftSlideView.addSubview(button)
        
    }
    
    @objc func rightSlideViewShowHidden(sender:UIButton){
        
        if rightSlideView.frame.origin.x == self.view.frame.maxX{
            
            UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.rightSlideView.frame.origin.x = self.view.frame.maxX - self.view.frame.midX}, completion: nil)
            
        }else if rightSlideView.frame.origin.x == self.view.frame.maxX - self.view.frame.midX{
            
            UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.rightSlideView.frame.origin.x = self.view.frame.maxX}, completion: nil)
            
        }      
    }
   
    @IBAction func slide(_ sender: UIButton) {
        
        
        leftSlideViewShowHidden()
        
    }   
}
```
# 終わり

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