Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // FloatingButton.swift
- // DreamCloud
- //
- // Created by Marian Fotev on 17/06/2018.
- // Copyright © 2018 Marian Fotev. All rights reserved.
- //
- import UIKit
- import RxSwift
- import RxCocoa
- class FloatingButton: UIButton {
- let bag = DisposeBag()
- func attach(to scrollView: UIScrollView) {
- let topConstraint = NSLayoutConstraint(item: self,
- attribute: NSLayoutAttribute.top,
- relatedBy: NSLayoutRelation.equal,
- toItem: self.superview,
- attribute: NSLayoutAttribute.top,
- multiplier: 1,
- constant: 20)
- let fr = self.convert(CGPoint.zero, from:scrollView)
- superview?.addConstraints([topConstraint])
- layer.shadowColor = UIColor.darkGray.cgColor
- layer.cornerRadius = 8
- layer.shadowOpacity = 0.7
- layer.shadowOffset = CGSize.zero
- layer.shadowRadius = 10
- scrollView.rx
- .contentOffset
- .asObservable()
- .bind { offset in
- if abs(fr.y + scrollView.frame.height - self.frame.height / 2 - 10) > offset.y {
- topConstraint.constant = fr.y + scrollView.frame.height - self.frame.height / 2 + offset.y - 10
- UIView.animate(withDuration: 0.25, delay: 0.05, options: .curveEaseInOut, animations: {
- self.layer.shadowRadius = 10
- self.layer.shadowOpacity = 0.7
- self.transform = CGAffineTransform(scaleX: 1.05, y: 1.05)
- self.layoutIfNeeded()
- })
- } else {
- UIView.animate(withDuration: 0.25, delay: 0.05, options: .curveEaseInOut, animations: {
- topConstraint.constant = 20
- self.layer.shadowRadius = 5
- self.layer.shadowOpacity = 1
- self.transform = CGAffineTransform(scaleX: 1, y: 1)
- self.layoutIfNeeded()
- })
- }
- }.disposed(by: bag)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement