Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  FloatingButton.swift
  3. //  DreamCloud
  4. //
  5. //  Created by Marian Fotev on 17/06/2018.
  6. //  Copyright © 2018 Marian Fotev. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import RxSwift
  11. import RxCocoa
  12.  
  13. class FloatingButton: UIButton {
  14.    
  15.     let bag = DisposeBag()
  16.    
  17.     func attach(to scrollView: UIScrollView) {
  18.         let topConstraint = NSLayoutConstraint(item: self,
  19.                                                attribute: NSLayoutAttribute.top,
  20.                                                relatedBy: NSLayoutRelation.equal,
  21.                                                toItem: self.superview,
  22.                                                attribute: NSLayoutAttribute.top,
  23.                                                multiplier: 1,
  24.                                                constant: 20)
  25.        
  26.        
  27.         let fr = self.convert(CGPoint.zero, from:scrollView)
  28.        
  29.        
  30.        
  31.        
  32.        
  33.         superview?.addConstraints([topConstraint])
  34.        
  35.         layer.shadowColor = UIColor.darkGray.cgColor
  36.         layer.cornerRadius = 8
  37.         layer.shadowOpacity = 0.7
  38.         layer.shadowOffset = CGSize.zero
  39.         layer.shadowRadius = 10
  40.        
  41.         scrollView.rx
  42.             .contentOffset
  43.             .asObservable()
  44.             .bind { offset in
  45.            
  46.                 if abs(fr.y + scrollView.frame.height - self.frame.height / 2 - 10) > offset.y {
  47.                     topConstraint.constant = fr.y + scrollView.frame.height - self.frame.height / 2 + offset.y - 10
  48.                     UIView.animate(withDuration: 0.25, delay: 0.05, options: .curveEaseInOut, animations: {
  49.                         self.layer.shadowRadius = 10
  50.                         self.layer.shadowOpacity = 0.7
  51.                         self.transform = CGAffineTransform(scaleX: 1.05, y: 1.05)
  52.                         self.layoutIfNeeded()
  53.                        
  54.                     })
  55.                    
  56.                 } else {
  57.                     UIView.animate(withDuration: 0.25, delay: 0.05, options: .curveEaseInOut, animations: {
  58.                         topConstraint.constant = 20
  59.                         self.layer.shadowRadius = 5
  60.                         self.layer.shadowOpacity = 1
  61.                         self.transform = CGAffineTransform(scaleX: 1, y: 1)
  62.                         self.layoutIfNeeded()
  63.                     })
  64.                 }
  65.                
  66.         }.disposed(by: bag)
  67.     }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement