Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.58 KB | None | 0 0
  1. override func viewDidLoad() {
  2.         super.viewDidLoad()
  3.         setupCard()
  4.     }
  5.    
  6.     func setupCard() {
  7.         cardHeight = self.view.bounds.height * 0.7
  8.         if UIDevice.hasTopNotch {
  9.             cardHandleAreaHeight = 70
  10.         }
  11.        
  12.         visualEffectView = UIVisualEffectView()
  13.         visualEffectView.frame = self.view.frame
  14.         self.view.addSubview(visualEffectView)
  15.        
  16.         self.visualEffectView.isHidden = true
  17.        
  18.         newBidCardViewController = NewBidCardViewController(nibName:"NewBidCardView", bundle:nil)
  19.         self.addChild(newBidCardViewController)
  20.         self.view.addSubview(newBidCardViewController.view)
  21.         newBidCardViewController.didMove(toParent: self)
  22.        
  23.         newBidCardViewController.view.frame = CGRect(x: 0, y: self.view.frame.height - cardHandleAreaHeight, width: self.view.bounds.width, height: cardHeight)
  24.         newBidCardViewController.view.clipsToBounds = true
  25.        
  26.         if self.id != nil {
  27.             newBidCardViewController.loadId = self.id!
  28.         }
  29.        
  30.         let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleCardTap(recognzier:)))
  31.         let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(self.handleCardPan(recognizer:)))
  32.        
  33.         newBidCardViewController.handleArea.addGestureRecognizer(tapGestureRecognizer)
  34.         newBidCardViewController.handleArea.addGestureRecognizer(panGestureRecognizer)
  35.     }
  36.    
  37.     @objc
  38.     func handleCardTap(recognzier:UITapGestureRecognizer) {
  39.        switch recognzier.state {
  40.        case .ended:
  41.            animateTransitionIfNeeded(state: nextState, duration: 0.9)
  42.        default:
  43.            break
  44.        }
  45.     }
  46.    
  47.     @objc
  48.     func handleCardPan(recognizer: UIPanGestureRecognizer) {
  49.         switch recognizer.state {
  50.         case .began:
  51.             // startTransition
  52.             startInteractiveTransition(state: nextState, duration: 0.9)
  53.         case .changed:
  54.             // updateTransition
  55.             let translation = recognizer.translation(in: self.newBidCardViewController.handleArea)
  56.             var fractionCompleted = translation.y / cardHeight
  57.             fractionCompleted = cardVisible ? fractionCompleted : -fractionCompleted
  58.             updateInteractiveTransition(fractionCompleted: fractionCompleted)
  59.         case .ended:
  60.             // continueTransition
  61.             continueInteractiveTransition()
  62.         default:
  63.             break
  64.         }
  65.     }
  66.    
  67.     func animateTransitionIfNeeded(state: CardState, duration: TimeInterval) {
  68.         if runningAnimations.isEmpty {
  69.             let frameAnimator = UIViewPropertyAnimator(duration: duration, dampingRatio: 1) {
  70.                 switch state {
  71.                 case .expanded:
  72.                     UIView.animate(withDuration: 2) {
  73.                         self.newBidCardViewController.handleArrowImage.transform = CGAffineTransform(rotationAngle: .pi)
  74.                         self.newBidCardViewController.handleLabel.text = "Swipe bottom to close"
  75.                     }
  76.                     self.newBidCardViewController.view.frame.origin.y = self.view.frame.height - (self.view.frame.height * 0.7)
  77.                     self.visualEffectView.isHidden = false
  78.                 case .collapsed:
  79.                     UIView.animate(withDuration: 2) {
  80.                         self.newBidCardViewController.handleArrowImage.transform = CGAffineTransform.identity
  81.                         self.newBidCardViewController.handleLabel.text = "Swipe up to place bid"
  82.                     }
  83.                     self.newBidCardViewController.view.frame.origin.y = self.view.frame.height - self.view.safeAreaInsets.bottom - self.cardHandleAreaHeight
  84.                     self.visualEffectView.isHidden = true
  85.                 }
  86.             }
  87.            
  88.             frameAnimator.addCompletion { _ in
  89.                 self.cardVisible = !self.cardVisible
  90.                 self.runningAnimations.removeAll()
  91.             }
  92.            
  93.             frameAnimator.startAnimation()
  94.             runningAnimations.append(frameAnimator)
  95.            
  96.             let blurAnimator = UIViewPropertyAnimator.init(duration: duration, dampingRatio: 1) {
  97.                 switch state {
  98.                 case .expanded:
  99.                     self.visualEffectView.effect = UIBlurEffect(style: .dark)
  100.                 case .collapsed:
  101.                     self.visualEffectView.effect = nil
  102.                 }
  103.             }
  104.  
  105.             blurAnimator.startAnimation()
  106.             runningAnimations.append(blurAnimator)
  107.  
  108.             blurAnimator.addCompletion{(_) in
  109.                 if state == .collapsed{
  110.                     self.visualEffectView.isHidden = true
  111.                 }
  112.             }
  113.         }
  114.     }
  115.    
  116.     func startInteractiveTransition(state: CardState, duration: TimeInterval) {
  117.         if runningAnimations.isEmpty {
  118.             animateTransitionIfNeeded(state: state, duration: duration)
  119.         }
  120.        
  121.         for animator in runningAnimations {
  122.             animator.pauseAnimation()
  123.             animationProgressWhenInterrupted = animator.fractionComplete
  124.         }
  125.     }
  126.    
  127.     func updateInteractiveTransition(fractionCompleted: CGFloat) {
  128.         for animator in runningAnimations {
  129.             animator.fractionComplete = fractionCompleted + animationProgressWhenInterrupted
  130.         }
  131.     }
  132.    
  133.    func continueInteractiveTransition() {
  134.         for animator in runningAnimations {
  135.             animator.continueAnimation(withTimingParameters: nil, durationFactor: 0)
  136.         }
  137.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement