diff --git a/Sources/SwiftUIPager/PagerContent.swift b/Sources/SwiftUIPager/PagerContent.swift index ba8438f..8cee623 100644 --- a/Sources/SwiftUIPager/PagerContent.swift +++ b/Sources/SwiftUIPager/PagerContent.swift @@ -217,44 +217,42 @@ extension Pager.PagerContent { } func onDragChanged(with value: DragGesture.Value) { - withAnimation { - if self.lastDraggingValue == nil { - onDraggingBegan?() - } - - let lastLocation = self.lastDraggingValue?.location ?? value.location - let swipeAngle = (value.location - lastLocation).angle ?? .zero - // Ignore swipes that aren't on the X-Axis - guard swipeAngle.isAlongXAxis else { - self.pagerModel.lastDraggingValue = value - return - } + if self.lastDraggingValue == nil { + onDraggingBegan?() + } - let side = self.isHorizontal ? self.size.width : self.size.height - let normalizedRatio = self.allowsMultiplePagination ? 1 : (self.pageDistance / side) - let offsetIncrement = (value.location.x - lastLocation.x) * normalizedRatio + let lastLocation = self.lastDraggingValue?.location ?? value.location + let swipeAngle = (value.location - lastLocation).angle ?? .zero + // Ignore swipes that aren't on the X-Axis + guard swipeAngle.isAlongXAxis else { + self.pagerModel.lastDraggingValue = value + return + } - // If swipe hasn't started yet, ignore swipes if they didn't start on the X-Axis - let isTranslationInXAxis = abs(value.translation.width) > abs(value.translation.height) - guard self.draggingOffset != 0 || isTranslationInXAxis else { - return - } + let side = self.isHorizontal ? self.size.width : self.size.height + let normalizedRatio = self.allowsMultiplePagination ? 1 : (self.pageDistance / side) + let offsetIncrement = (value.location.x - lastLocation.x) * normalizedRatio - let timeIncrement = value.time.timeIntervalSince(self.lastDraggingValue?.time ?? value.time) - if timeIncrement != 0 { - self.pagerModel.draggingVelocity = Double(offsetIncrement) / timeIncrement - } + // If swipe hasn't started yet, ignore swipes if they didn't start on the X-Axis + let isTranslationInXAxis = abs(value.translation.width) > abs(value.translation.height) + guard self.draggingOffset != 0 || isTranslationInXAxis else { + return + } - var newOffset = self.draggingOffset + offsetIncrement - if !allowsMultiplePagination { - newOffset = self.direction == .forward ? max(newOffset, self.pageRatio * -self.pageDistance) : min(newOffset, self.pageRatio * self.pageDistance) - } + let timeIncrement = value.time.timeIntervalSince(self.lastDraggingValue?.time ?? value.time) + if timeIncrement != 0 { + self.pagerModel.draggingVelocity = Double(offsetIncrement) / timeIncrement + } - self.pagerModel.draggingOffset = newOffset - self.pagerModel.lastDraggingValue = value - self.onDraggingChanged?(Double(-self.draggingOffset / self.pageDistance)) - self.pagerModel.objectWillChange.send() + var newOffset = self.draggingOffset + offsetIncrement + if !allowsMultiplePagination { + newOffset = self.direction == .forward ? max(newOffset, self.pageRatio * -self.pageDistance) : min(newOffset, self.pageRatio * self.pageDistance) } + + self.pagerModel.draggingOffset = newOffset + self.pagerModel.lastDraggingValue = value + self.onDraggingChanged?(Double(-self.draggingOffset / self.pageDistance)) + self.pagerModel.objectWillChange.send() } func onDragGestureEnded() {