Skip to content

Commit

Permalink
enhancement(animation): dont animate translate with dragging
Browse files Browse the repository at this point in the history
  • Loading branch information
nylki authored and fermoya committed Feb 20, 2021
1 parent a9d9b30 commit 59e3d20
Showing 1 changed file with 30 additions and 32 deletions.
62 changes: 30 additions & 32 deletions Sources/SwiftUIPager/PagerContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 59e3d20

Please sign in to comment.