|
| 1 | +// |
| 2 | +// TopBottomAnimation.swift |
| 3 | +// SwiftMessages |
| 4 | +// |
| 5 | +// Created by Timothy Moose on 6/4/17. |
| 6 | +// Copyright © 2017 SwiftKick Mobile. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | + |
| 11 | +public class TopBottomAnimation: NSObject, Animator { |
| 12 | + |
| 13 | + enum Style { |
| 14 | + case top |
| 15 | + case bottom |
| 16 | + } |
| 17 | + |
| 18 | + public weak var delegate: AnimationDelegate? |
| 19 | + |
| 20 | + var style: Style |
| 21 | + |
| 22 | + var translationConstraint: NSLayoutConstraint! = nil |
| 23 | + |
| 24 | + weak var messageView: UIView? |
| 25 | + |
| 26 | + weak var containerView: UIView? |
| 27 | + |
| 28 | + init(style: Style, delegate: AnimationDelegate) { |
| 29 | + self.style = style |
| 30 | + self.delegate = delegate |
| 31 | + } |
| 32 | + |
| 33 | + public func show(context: AnimationContext, completion: @escaping AnimationCompletion) { |
| 34 | + install(context: context) |
| 35 | + showAnimation(completion: completion) |
| 36 | + } |
| 37 | + |
| 38 | + public func hide(context: AnimationContext, completion: @escaping AnimationCompletion) { |
| 39 | + let view = context.messageView |
| 40 | + let container = context.containerView |
| 41 | + UIView.animate(withDuration: 0.2, delay: 0, options: [.beginFromCurrentState, .curveEaseIn], animations: { |
| 42 | + let size = view.systemLayoutSizeFitting(UILayoutFittingCompressedSize) |
| 43 | + self.translationConstraint.constant -= size.height |
| 44 | + container.layoutIfNeeded() |
| 45 | + }, completion: { completed in |
| 46 | + completion(completed) |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + func install(context: AnimationContext) { |
| 51 | + let view = context.messageView |
| 52 | + let container = context.containerView |
| 53 | + messageView = view |
| 54 | + containerView = container |
| 55 | + if let adjustable = context.messageView as? MarginAdjustable { |
| 56 | + bounceOffset = adjustable.bounceAnimationOffset |
| 57 | + } |
| 58 | + view.translatesAutoresizingMaskIntoConstraints = false |
| 59 | + container.addSubview(view) |
| 60 | + let leading = NSLayoutConstraint(item: view, attribute: .leading, relatedBy: .equal, toItem: container, attribute: .leading, multiplier: 1.00, constant: 0.0) |
| 61 | + let trailing = NSLayoutConstraint(item: view, attribute: .trailing, relatedBy: .equal, toItem: container, attribute: .trailing, multiplier: 1.00, constant: 0.0) |
| 62 | + switch style { |
| 63 | + case .top: |
| 64 | + translationConstraint = NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: container, attribute: .top, multiplier: 1.00, constant: 0.0) |
| 65 | + case .bottom: |
| 66 | + translationConstraint = NSLayoutConstraint(item: container, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1.00, constant: 0.0) |
| 67 | + } |
| 68 | + container.addConstraints([leading, trailing, translationConstraint]) |
| 69 | + if let adjustable = view as? MarginAdjustable { |
| 70 | + var top: CGFloat = 0.0 |
| 71 | + var bottom: CGFloat = 0.0 |
| 72 | + switch style { |
| 73 | + case .top: |
| 74 | + top += adjustable.bounceAnimationOffset |
| 75 | + if context.behindStatusBar { |
| 76 | + top += adjustable.statusBarOffset |
| 77 | + } |
| 78 | + case .bottom: |
| 79 | + bottom += adjustable.bounceAnimationOffset |
| 80 | + } |
| 81 | + view.layoutMargins = UIEdgeInsets(top: top, left: 0.0, bottom: bottom, right: 0.0) |
| 82 | + } |
| 83 | + let size = view.systemLayoutSizeFitting(UILayoutFittingCompressedSize) |
| 84 | + translationConstraint.constant -= size.height |
| 85 | + container.layoutIfNeeded() |
| 86 | + if context.interactiveHide { |
| 87 | + let pan = UIPanGestureRecognizer() |
| 88 | + pan.addTarget(self, action: #selector(pan(_:))) |
| 89 | + if let view = view as? BackgroundViewable { |
| 90 | + view.backgroundView.addGestureRecognizer(pan) |
| 91 | + } else { |
| 92 | + view.addGestureRecognizer(pan) |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + func showAnimation(completion: @escaping AnimationCompletion) { |
| 98 | + guard let container = containerView else { |
| 99 | + completion(false) |
| 100 | + return |
| 101 | + } |
| 102 | + let animationDistance = self.translationConstraint.constant + bounceOffset |
| 103 | + // Cap the initial velocity at zero because the bounceOffset may not be great |
| 104 | + // enough to allow for greater bounce induced by a quick panning motion. |
| 105 | + let initialSpringVelocity = animationDistance == 0.0 ? 0.0 : min(0.0, closeSpeed / animationDistance) |
| 106 | + UIView.animate(withDuration: 0.4, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: initialSpringVelocity, options: [.beginFromCurrentState, .curveLinear, .allowUserInteraction], animations: { |
| 107 | + self.translationConstraint.constant = -self.bounceOffset |
| 108 | + container.layoutIfNeeded() |
| 109 | + }, completion: { completed in |
| 110 | + completion(completed) |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | + fileprivate var bounceOffset: CGFloat = 5 |
| 115 | + |
| 116 | + /* |
| 117 | + MARK: - Pan to close |
| 118 | + */ |
| 119 | + |
| 120 | + fileprivate var closing = false |
| 121 | + fileprivate var closeSpeed: CGFloat = 0.0 |
| 122 | + fileprivate var closePercent: CGFloat = 0.0 |
| 123 | + fileprivate var panTranslationY: CGFloat = 0.0 |
| 124 | + |
| 125 | + @objc func pan(_ pan: UIPanGestureRecognizer) { |
| 126 | + switch pan.state { |
| 127 | + case .changed: |
| 128 | + guard let view = pan.view else { return } |
| 129 | + let height = view.bounds.height - bounceOffset |
| 130 | + if height <= 0 { return } |
| 131 | + let point = pan.location(ofTouch: 0, in: view) |
| 132 | + var velocity = pan.velocity(in: view) |
| 133 | + var translation = pan.translation(in: view) |
| 134 | + if case .top = style { |
| 135 | + velocity.y *= -1.0 |
| 136 | + translation.y *= -1.0 |
| 137 | + } |
| 138 | + if !closing { |
| 139 | + if view.bounds.contains(point) && velocity.y > 0.0 && velocity.x / velocity.y < 5.0 { |
| 140 | + closing = true |
| 141 | + pan.setTranslation(CGPoint.zero, in: view) |
| 142 | + delegate?.panStarted(animator: self) |
| 143 | + } |
| 144 | + } |
| 145 | + if !closing { return } |
| 146 | + let translationAmount = -bounceOffset - max(0.0, translation.y) |
| 147 | + translationConstraint.constant = translationAmount |
| 148 | + closeSpeed = velocity.y |
| 149 | + closePercent = translation.y / height |
| 150 | + panTranslationY = translation.y |
| 151 | + case .ended, .cancelled: |
| 152 | + if closeSpeed > 750.0 || closePercent > 0.33 { |
| 153 | + delegate?.hide(animator: self) |
| 154 | + } else { |
| 155 | + closing = false |
| 156 | + closeSpeed = 0.0 |
| 157 | + closePercent = 0.0 |
| 158 | + panTranslationY = 0.0 |
| 159 | + showAnimation(completion: { (completed) in |
| 160 | + self.delegate?.panEnded(animator: self) |
| 161 | + }) |
| 162 | + } |
| 163 | + default: |
| 164 | + break |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments