[TIL] 키오스크 프로젝트 - 개발 2일차 -

신승현·2024년 4월 2일

TIL

목록 보기
51/72
post-thumbnail

1️⃣ SegmentControl 커스텀

SegementControl은 기본적으로 배경이 있고, 배경색이 있어서 이거를 없애기 위해 extension을 이용해 커스텀 해줬다.

extension UISegmentedControl {
    func removeBorders(andBackground:Bool=false) {
        setBackgroundImage(imageWithColor(color: backgroundColor ?? .clear), for: .normal, barMetrics: .default)
        setBackgroundImage(imageWithColor(color: backgroundColor ?? .clear), for: .selected, barMetrics: .default)
        setDividerImage(imageWithColor(color: UIColor.clear), forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
        setTitleTextAttributes([.foregroundColor: UIColor.black], for: .selected)
        setTitleTextAttributes([.foregroundColor: UIColor.gray], for: .normal)

    }

    private func imageWithColor(color: UIColor) -> UIImage {
        let rect = CGRect(x: 0.0, y: 0.0, width:  1.0, height: 22.0)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        context!.setFillColor(color.cgColor);
        context!.fill(rect);
        let image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image!
    }
}

2️⃣ View 전환 Animation 커스텀

View 전환 Animation이 기본적으로 여러개 있지만 오른쪽에서 왼쪽으로 화면이 넘어가는 Animation이 없어서 이 부분도 커스텀으로 수정해줬다.

extension UIViewController {
    func presentFromRight(_ viewControllerToPresent: UIViewController, animated: Bool, completion: (() -> Void)? = nil) {
        if animated {
            // 새 뷰 컨트롤러의 초기 위치를 화면 오른쪽 바깥으로 설정
            viewControllerToPresent.view.frame = CGRect(x: self.view.frame.width, y: 0, width: self.view.frame.width, height: self.view.frame.height)
            
            // 새 뷰 컨트롤러 뷰를 현재 뷰 위에 추가
            self.view.addSubview(viewControllerToPresent.view)
            self.addChild(viewControllerToPresent)
            
            // 애니메이션으로 뷰를 왼쪽으로 이동
            UIView.animate(withDuration: 0.5, animations: {
                viewControllerToPresent.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
            }, completion: { finished in
                viewControllerToPresent.didMove(toParent: self)
                completion?()
            })
        } else {
            // 애니메이션이 없는 경우, 바로 뷰 컨트롤러를 추가
            self.view.addSubview(viewControllerToPresent.view)
            self.addChild(viewControllerToPresent)
            viewControllerToPresent.didMove(toParent: self)
            completion?()
        }
    }
}
profile
개발자

0개의 댓글