Skip to content

Commit 27b7e1d

Browse files
authored
Merge pull request #27 from jtrivedi/janum/immediate-model-updates
Non-animated updates shouldn't wait until the next turn of the run loop #26
2 parents a52498e + ff2f467 commit 27b7e1d

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

Sources/Wave/Internal/AnimationController.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ internal class AnimationController {
7474
}
7575

7676
animations[animation.id] = animation
77+
78+
animation.updateAnimation(dt: .zero)
7779
}
7880

7981
internal func executeHandler(uuid: UUID?, finished: Bool, retargeted: Bool) {

Sources/Wave/SpringAnimator.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ public class SpringAnimator<T: SpringInterpolatable>: AnimatorProviding {
216216
let newValue: T.ValueType
217217
let newVelocity: T.VelocityType
218218

219-
if spring.response > .zero && mode != .nonAnimated {
219+
let isAnimated = spring.response > .zero && mode != .nonAnimated
220+
221+
if isAnimated {
220222
(newValue, newVelocity) = T.updateValue(spring: spring, value: value, target: target, velocity: velocity, dt: dt)
221223
} else {
222224
newValue = target
@@ -226,7 +228,7 @@ public class SpringAnimator<T: SpringInterpolatable>: AnimatorProviding {
226228
self.value = newValue
227229
self.velocity = newVelocity
228230

229-
let animationFinished = runningTime >= (self.settlingTime)
231+
let animationFinished = (runningTime >= settlingTime) || !isAnimated
230232

231233
if animationFinished {
232234
self.value = target

Tests/WaveTests/AnimatableUIViewPropertyTests.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ final class UIViewAnimatablePropertyTests: XCTestCase {
9797
}
9898
}
9999

100+
func testNonAnimatedBoundsSize() {
101+
let v = UIView()
102+
103+
let initialBoundsSize = CGSize(width: 50, height: 50)
104+
let targetFrameSize = CGSize(width: 25, height: 25)
105+
106+
v.bounds.size = initialBoundsSize
107+
v.animator.scale = CGPoint(x: 0.5, y: 0.5)
108+
109+
XCTAssertEqual(v.bounds.size, initialBoundsSize)
110+
XCTAssertEqual(v.frame.size, targetFrameSize)
111+
}
112+
100113
func testCenter() {
101114
let view = UIView()
102115

@@ -228,13 +241,43 @@ final class UIViewAnimatablePropertyTests: XCTestCase {
228241

229242
XCTAssertEqual(view.alpha, initialValue)
230243
XCTAssertEqual(view.animator.alpha, targetValue)
244+
XCTAssertEqual(Double(view.layer.animator.opacity), targetValue)
231245

232246
wait(for: .defaultAnimated) {
233247
XCTAssertEqual(view.alpha, targetValue)
234248
XCTAssertEqual(view.animator.alpha, targetValue)
235249
}
236250
}
237251

252+
func testNonAnimatedAlpha() {
253+
let view = UIView()
254+
255+
let initialValue = 1.0
256+
let targetValue = 0.5
257+
258+
view.alpha = initialValue
259+
260+
Wave.animate(withSpring: .defaultNonAnimated) {
261+
view.animator.alpha = targetValue
262+
}
263+
264+
XCTAssertEqual(view.alpha, targetValue)
265+
XCTAssertEqual(view.animator.alpha, targetValue)
266+
267+
XCTAssertEqual(Double(view.layer.opacity), targetValue)
268+
XCTAssertEqual(view.layer.animator.opacity, targetValue)
269+
270+
Wave.animate(withSpring: .defaultAnimated, mode: .nonAnimated) {
271+
view.animator.alpha = initialValue
272+
}
273+
274+
XCTAssertEqual(view.alpha, initialValue)
275+
XCTAssertEqual(view.animator.alpha, initialValue)
276+
277+
XCTAssertEqual(Double(view.layer.opacity), initialValue)
278+
XCTAssertEqual(view.layer.animator.opacity, initialValue)
279+
}
280+
238281
func testPropertyAnimation() {
239282
let animator = SpringAnimator<CGFloat>(spring: .defaultAnimated)
240283
animator.value = 0

0 commit comments

Comments
 (0)