Skip to content

Instantly share code, notes, and snippets.

@ohlulu
Last active June 21, 2019 13:55
Show Gist options
  • Save ohlulu/a741f2529d0d4435b3f2b618b59eed01 to your computer and use it in GitHub Desktop.
Save ohlulu/a741f2529d0d4435b3f2b618b59eed01 to your computer and use it in GitHub Desktop.
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
// 取出 toView, 在上面的示意圖中代表的就是 A 畫面
guard let toView = transitionContext.viewController(forKey: .to)?.view else {
return
}
// 取出 container view
let containerView = transitionContext.containerView
// 建立我們的 mask view,並坐初步設置
let maskView = UIView()
maskView.frame.size = CGSize(width: 1, height: 1)
maskView.center = startPoint
maskView.backgroundColor = .black
maskView.layer.cornerRadius = 0.5
toView.mask = maskView
containerView.addSubview(toView)
// 因為最終 mask view 的圓,需要覆蓋到整個畫面,所以計算出 mask view需要的大小
let containerFrame = containerView.frame
let maxY = max(containerFrame.height - startPoint.y, startPoint.y)
let maxX = max(containerFrame.width - startPoint.x, startPoint.x)
let maxSize = max(maxY, maxX) * 2.1
// 最後我們使用 UIView.animation 顯式動畫,將我們的 mask view 擴散到整個畫面
UIView.animate(
withDuration: durationTime,
delay: 0,
options: [.curveEaseOut],
animations: {
maskView.frame.size = CGSize(width: maxSize, height: maxSize)
maskView.layer.cornerRadius = maxSize / 2.0
maskView.center = self.startPoint
}) { (flag) in
transitionContext.completeTransition(flag)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment