Skip to content

Commit 07c6a59

Browse files
author
Hubert Kuczynski
committed
Fix animation issues
1 parent 0469eb8 commit 07c6a59

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

SwipeableTableViewCell/Classes/StretchyCircleButton.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ final class StretchyCircleButton: UIButton {
4242
}
4343

4444
override var isHighlighted: Bool {
45-
didSet {
45+
willSet {
46+
guard isHighlighted != newValue else { return }
4647
UIView.animate(
4748
withDuration: 0.25,
4849
delay: 0,
4950
options: .curveEaseInOut,
5051
animations: {
51-
self.transform = self.isHighlighted ? CGAffineTransform(scaleX: 1.12, y: 1.12) : .identity
52-
self.backgroundColor = self.backgroundColor?.withAlphaComponent(self.isHighlighted ? 0.6 : 1)
52+
self.backgroundColor = self.backgroundColor?.withAlphaComponent(newValue ? 0.6 : 1)
53+
self.transform = newValue ? CGAffineTransform(scaleX: 1.12, y: 1.12) : .identity
5354
}
5455
)
5556
}

SwipeableTableViewCell/Classes/SwipeableTableViewCell.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,16 @@ open class SwipeableTableViewCell: UITableViewCell {
162162
}
163163

164164
@objc private func handlePan(_ recognizer: UIPanGestureRecognizer) {
165-
if recognizer.state == .ended {
165+
switch recognizer.state {
166+
case .ended, .cancelled:
166167
startSlideAnimation(recognizer)
168+
default:
169+
break
167170
}
168171
}
169172

170173
@objc private func handlePrimaryButtonTap() {
174+
guard contentOffset.x >= Constants.disconnectPoint else { return }
171175
onPrimaryButtonTap?()
172176
}
173177

@@ -193,6 +197,7 @@ open class SwipeableTableViewCell: UITableViewCell {
193197
self.scrollView.setContentOffset(self.slideTargetPoint, animated: false)
194198
}, completion: { _ in
195199
self.buttonScaleAnimationIsRunning = false
200+
self.updateSlideDestination()
196201
displayLink?.invalidate()
197202
}
198203
)
@@ -215,12 +220,12 @@ open class SwipeableTableViewCell: UITableViewCell {
215220

216221
private func updateShape() {
217222
guard slideDestination == .end else { return }
218-
219223
scrollViewContentView.move(by: contentOffset.x)
220-
221224
button.stretch(by: calulateCircleOffset(for: contentOffset.x))
222225
button.imageView?.alpha = contentOffset.x > Constants.buttonDimension / 2 + Constants.primaryButtonTrailingOffset ? 1 : 0
223-
button.backgroundColor = contentOffset.x > Constants.disconnectPoint ? buttonActiveBackgroundColor : scrollViewContentView.backgroundColor
226+
if !button.isHighlighted {
227+
button.backgroundColor = contentOffset.x > Constants.disconnectPoint ? buttonActiveBackgroundColor : scrollViewContentView.backgroundColor
228+
}
224229
}
225230

226231
private func calulateCircleOffset(for x: CGFloat) -> CGFloat {
@@ -230,7 +235,7 @@ open class SwipeableTableViewCell: UITableViewCell {
230235
private func updateSlideDestination() {
231236
if contentOffset.x <= 5 {
232237
slideDestination = .end
233-
} else if contentOffset.x >= Constants.maxOffset - 5 {
238+
} else if contentOffset.x >= Constants.maxOffset {
234239
slideDestination = .begin
235240
}
236241
}
@@ -243,8 +248,8 @@ extension SwipeableTableViewCell: UIScrollViewDelegate {
243248
y: scrollView.contentOffset.y / Constants.contentOffsetMovementDivider
244249
)
245250

246-
updateSlideDestination()
247251
updateShape()
252+
updateSlideDestination()
248253
}
249254
}
250255

0 commit comments

Comments
 (0)