Swift

Floating Button

khon98 2021. 2. 13. 21:15

새로운 파일 생성 시 subclass of에 UIView인지 Controller인지 확인

self 객체는 앞에 self를 입력해줘야 함

 

Up Casting

- 여러 가지 타입을 포괄하는 타입

 

Down Casting

- 여러 가지 포괄하는 타입의 하위 타입 중에 하나를 선택하는 개념

 

viewDidLoad

- view controller가 실행하면서 같이 실행되는 func

- 화면이 그려지기 전에 오브젝트들이 생성되면서 실행되는 부분

 

viewDidAppear

- 디바이스가 실행되고 보이는 시점

 

Duration

- 초를 의미(1은 1초, 0.3은 0.3초)

 

Damping

- 흔들림 애니메이션

 

delay

- Duration과 비슷한 개념,  delay 할 필요가 없으면 0

 

usingSpringWithDamping

- 0에 가까울수록 흔들림이 커짐(0 ~ 1 사이에 값을 넣음 / 0.3이나 0.8 같은 소수, 1 이상 갑은 흔들림 없음)

 

layoutIfNeeded

- layoutIfNeeded가 없으면 화면 갱신이 안됨

- Duration에 입력한 숫자만큼 분할해서 갱신(Duration에 1을 넣으면 1초의 시간에 맞춰서 움직임이 표현)

 

 

ViewController.swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "ShowPopup" {
            
            // 다운 캐스팅
            let FloatingVC = segue.destination as! FloatingButtonListViewController
            
            // 해당하는 화면이 투명하게 나오게 하기
            FloatingVC.modalPresentationStyle = .overCurrentContext
        }
    }
}

 

 

cocoa touch class

import UIKit

class FloatingButtonListViewController: UIViewController {

    @IBOutlet weak var btn1CenterY: NSLayoutConstraint!
    @IBOutlet weak var btn2CenterY: NSLayoutConstraint!
    @IBOutlet weak var btn3CenterY: NSLayoutConstraint!
    
    // 처음 실행되는 부분
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // constant 값 초기화
        btn1CenterY.constant = 0
        btn2CenterY.constant = 0
        btn3CenterY.constant = 0

    }
    
    
    // 디바이스를 실행해서 화면이 보이는 시점
    override func viewDidAppear(_ animated: Bool) {
        // super는 규칙이라고 생각하면 됨
        super.viewDidAppear(animated)
        
        // withDuration 버튼이 올라오는 시간 0에 가까울수록 빨리 올라감
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseOut) {
            
            self.btn1CenterY.constant = 80
            self.btn2CenterY.constant = 160
            self.btn3CenterY.constant = 240
            
            // 화면 갱신,layoutIfNeeded를 꼭 해야 갱신 됨
            self.view.layoutIfNeeded()
            
            // 애니메이션이 끝나는 시점
        } completion: { (completion) in
        }
    }

    
    @IBAction func DismissFloating(_ sender: Any) {
        
        // withDuration 버튼이 내려가는 시간 0에 가까울수록 빨리 내려감
        UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0.5, options: .curveEaseOut) {
            
            self.btn1CenterY.constant = 0
            self.btn2CenterY.constant = 0
            self.btn3CenterY.constant = 0
            
            self.view.layoutIfNeeded()
            
        } completion: { (completion) in
            
            // animation 효과를 사용안하면 animated에 false입력
            // completion은 옵셔널이라 사용 안할경우 nil입력 가능
            self.dismiss(animated: false, completion: nil)
            
        }

    }
    
    @IBAction func Food3Action(_ sender: Any) {
        print("food3 selected")
    }
    
    @IBAction func Food2Action(_ sender: Any) {
        print("food2 selected")
    }
    
    @IBAction func Food1Action(_ sender: Any) {
        print("food1 selected")
    }
}