Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- override func viewDidLoad() {
- super.viewDidLoad()
- setupCard()
- }
- func setupCard() {
- cardHeight = self.view.bounds.height * 0.7
- if UIDevice.hasTopNotch {
- cardHandleAreaHeight = 70
- }
- visualEffectView = UIVisualEffectView()
- visualEffectView.frame = self.view.frame
- self.view.addSubview(visualEffectView)
- self.visualEffectView.isHidden = true
- newBidCardViewController = NewBidCardViewController(nibName:"NewBidCardView", bundle:nil)
- self.addChild(newBidCardViewController)
- self.view.addSubview(newBidCardViewController.view)
- newBidCardViewController.didMove(toParent: self)
- newBidCardViewController.view.frame = CGRect(x: 0, y: self.view.frame.height - cardHandleAreaHeight, width: self.view.bounds.width, height: cardHeight)
- newBidCardViewController.view.clipsToBounds = true
- if self.id != nil {
- newBidCardViewController.loadId = self.id!
- }
- let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleCardTap(recognzier:)))
- let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(self.handleCardPan(recognizer:)))
- newBidCardViewController.handleArea.addGestureRecognizer(tapGestureRecognizer)
- newBidCardViewController.handleArea.addGestureRecognizer(panGestureRecognizer)
- }
- @objc
- func handleCardTap(recognzier:UITapGestureRecognizer) {
- switch recognzier.state {
- case .ended:
- animateTransitionIfNeeded(state: nextState, duration: 0.9)
- default:
- break
- }
- }
- @objc
- func handleCardPan(recognizer: UIPanGestureRecognizer) {
- switch recognizer.state {
- case .began:
- // startTransition
- startInteractiveTransition(state: nextState, duration: 0.9)
- case .changed:
- // updateTransition
- let translation = recognizer.translation(in: self.newBidCardViewController.handleArea)
- var fractionCompleted = translation.y / cardHeight
- fractionCompleted = cardVisible ? fractionCompleted : -fractionCompleted
- updateInteractiveTransition(fractionCompleted: fractionCompleted)
- case .ended:
- // continueTransition
- continueInteractiveTransition()
- default:
- break
- }
- }
- func animateTransitionIfNeeded(state: CardState, duration: TimeInterval) {
- if runningAnimations.isEmpty {
- let frameAnimator = UIViewPropertyAnimator(duration: duration, dampingRatio: 1) {
- switch state {
- case .expanded:
- UIView.animate(withDuration: 2) {
- self.newBidCardViewController.handleArrowImage.transform = CGAffineTransform(rotationAngle: .pi)
- self.newBidCardViewController.handleLabel.text = "Swipe bottom to close"
- }
- self.newBidCardViewController.view.frame.origin.y = self.view.frame.height - (self.view.frame.height * 0.7)
- self.visualEffectView.isHidden = false
- case .collapsed:
- UIView.animate(withDuration: 2) {
- self.newBidCardViewController.handleArrowImage.transform = CGAffineTransform.identity
- self.newBidCardViewController.handleLabel.text = "Swipe up to place bid"
- }
- self.newBidCardViewController.view.frame.origin.y = self.view.frame.height - self.view.safeAreaInsets.bottom - self.cardHandleAreaHeight
- self.visualEffectView.isHidden = true
- }
- }
- frameAnimator.addCompletion { _ in
- self.cardVisible = !self.cardVisible
- self.runningAnimations.removeAll()
- }
- frameAnimator.startAnimation()
- runningAnimations.append(frameAnimator)
- let blurAnimator = UIViewPropertyAnimator.init(duration: duration, dampingRatio: 1) {
- switch state {
- case .expanded:
- self.visualEffectView.effect = UIBlurEffect(style: .dark)
- case .collapsed:
- self.visualEffectView.effect = nil
- }
- }
- blurAnimator.startAnimation()
- runningAnimations.append(blurAnimator)
- blurAnimator.addCompletion{(_) in
- if state == .collapsed{
- self.visualEffectView.isHidden = true
- }
- }
- }
- }
- func startInteractiveTransition(state: CardState, duration: TimeInterval) {
- if runningAnimations.isEmpty {
- animateTransitionIfNeeded(state: state, duration: duration)
- }
- for animator in runningAnimations {
- animator.pauseAnimation()
- animationProgressWhenInterrupted = animator.fractionComplete
- }
- }
- func updateInteractiveTransition(fractionCompleted: CGFloat) {
- for animator in runningAnimations {
- animator.fractionComplete = fractionCompleted + animationProgressWhenInterrupted
- }
- }
- func continueInteractiveTransition() {
- for animator in runningAnimations {
- animator.continueAnimation(withTimingParameters: nil, durationFactor: 0)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement