Swift
CATransaction 사용하여 complete callback 함수 만들어보기.
진태우
2017. 6. 6. 19:02
CATransaction 사용하여 애니매이션되는 속성(트리거 여부, 타이밍, 기간 등)을 설정할 수 있다.
아래 코드는 navigationController에서 pushViewController를 사용하여 화면을 전환할 경우, complete method를 추가하여 사용할 수 있도록 컨트롤러를 확장한 코드이다.
// navigationController 확장
extension UINavigationController {
// pushViewController 실행후 complete callback 구현 가능하도록 수정
public func pushViewController(viewController: UIViewController, animated: Bool, completion: (() -> Void)?) {
CATransaction.begin()
CATransaction.setAnimationDuration(2) // sleep 2 seconds
CATransaction.setCompletionBlock(completion)
CATransaction.setCompletionBlock {
// callback 함수를 받지 않고 직접 코드 작성도 가능함.
}
pushViewController(viewController, animated: animated)
CATransaction.commit()
}
}
// 메서드 실행 예시
navigationController.pushViewController(viewController: presentingView!, animated: true) {
// 위 코드에서 complete에 들어갈 코드블럭
self.view.removeFromSuperview()
}