Skip to content

Commit a724bb6

Browse files
committed
Expose hideDelay option for custom presentation
1 parent 2c68287 commit a724bb6

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

CHANGELOG.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ All notable changes to this project will be documented in this file.
44
## 6.0.1
55

66
### Features
7-
* #257 Add the ability to specify the hide delay on `PhysicsPanHandler`. When `PhysicsPanHandler`'s dismissal gesture determines that the view is out of the container view's bounds, it waits `hideDelay` seconds before calling `SwiftMessages.hide()`. The default value is 0.2, which is what you'll get when using `presentationStyle = .center`. To specify a different delay, use `presentationStyle = .custom(animator:)` and supply an instance of `PhysicsPanHandler` configured to your liking.
7+
* #257 The `.centered` presentation style, which is a shortcut for a specific configuration of the `PhysicsAnimation` animator, provides a physics-based dismissal gesture where the view can be flung off screen. When the view goes out of the container view's bounds, the animator calls `SwiftMessages.hide()`, which animates the dim view away and concludes the message view's lifecycle. There is currently a small delay of 0.2s before calling `hide()`.
8+
9+
This change adds the ability to configure the delay by customizing the animator. For example, to set the delay to zero, one would do:
10+
11+
````swift
12+
let animation = PhysicsAnimation()
13+
animation.panHandler.hideDelay = 0
14+
config.presentationStyle = .custom(animator: animation)
15+
````
816

917
## 6.0.0
1018

SwiftMessages/PhysicsAnimation.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class PhysicsAnimation: NSObject, Animator {
1818

1919
public var placement: Placement = .center
2020

21+
public var panHandler = PhysicsPanHandler()
22+
2123
public weak var delegate: AnimationDelegate?
2224
weak var messageView: UIView?
2325
weak var containerView: UIView?
@@ -37,9 +39,9 @@ public class PhysicsAnimation: NSObject, Animator {
3739

3840
public func hide(context: AnimationContext, completion: @escaping AnimationCompletion) {
3941
NotificationCenter.default.removeObserver(self)
40-
if panHandler?.isOffScreen ?? false {
42+
if panHandler.isOffScreen {
4143
context.messageView.alpha = 0
42-
panHandler?.state?.stop()
44+
panHandler.state?.stop()
4345
}
4446
let view = context.messageView
4547
self.context = context
@@ -114,11 +116,9 @@ public class PhysicsAnimation: NSObject, Animator {
114116
CATransaction.commit()
115117
}
116118

117-
var panHandler: PhysicsPanHandler?
118-
119119
func installInteractive(context: AnimationContext) {
120120
guard context.interactiveHide else { return }
121-
panHandler = PhysicsPanHandler(context: context, animator: self)
121+
panHandler.configure(context: context, animator: self)
122122
}
123123
}
124124

SwiftMessages/PhysicsPanHandler.swift

+14-10
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import UIKit
1010

1111
open class PhysicsPanHandler {
1212

13-
/// During the dismiss gesture, specifies the delay between the view
14-
/// going out of the screen's bounds and `SwiftMessages.hide()` being called.
1513
public var hideDelay: TimeInterval = 0.2
1614

1715
public struct MotionSnapshot {
@@ -91,17 +89,23 @@ open class PhysicsPanHandler {
9189
private(set) public var isOffScreen = false
9290
private var restingCenter: CGPoint?
9391

94-
public init(context: AnimationContext, animator: Animator) {
95-
messageView = context.messageView
96-
containerView = context.containerView
97-
self.animator = animator
92+
public init() {}
93+
94+
lazy var pan: UIPanGestureRecognizer = {
9895
let pan = UIPanGestureRecognizer()
9996
pan.addTarget(self, action: #selector(pan(_:)))
100-
if let view = messageView as? BackgroundViewable {
101-
view.backgroundView.addGestureRecognizer(pan)
102-
} else {
103-
context.messageView.addGestureRecognizer(pan)
97+
return pan
98+
}()
99+
100+
func configure(context: AnimationContext, animator: Animator) {
101+
if let oldView = (messageView as? BackgroundViewable)?.backgroundView ?? messageView {
102+
oldView.removeGestureRecognizer(pan)
104103
}
104+
messageView = context.messageView
105+
let view = (messageView as? BackgroundViewable)?.backgroundView ?? messageView
106+
view?.addGestureRecognizer(pan)
107+
containerView = context.containerView
108+
self.animator = animator
105109
}
106110

107111
@objc func pan(_ pan: UIPanGestureRecognizer) {

0 commit comments

Comments
 (0)