Skip to content

Fix FingerUp causing UITouch to end in start location #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ extension EventGenerator {
throw HammerError.touchForFingerDoesNotExist(index: finger.fingerIndex)
}

try self.activeTouches.update(finger: finger, forIdentifier: existingIdentifier)
return existingIdentifier
case .ended, .cancelled:
guard let existingIdentifier = existingIdentifier else {
Expand Down
6 changes: 6 additions & 0 deletions Sources/Hammer/Utilties/TouchStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,10 @@ struct TouchStorage {
self.stylusStore = nil
}
}

mutating func update(finger: FingerInfo, forIdentifier identifier: UInt32) throws {
if let index = self.fingerStore.firstIndex(where: { $0.identifier == identifier }) {
self.fingerStore[index].finger = finger
}
}
}
24 changes: 24 additions & 0 deletions Tests/HammerTests/DragTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Hammer
import XCTest
import Foundation

final class DragTests: XCTestCase {

func test_drag() throws {
let view = TouchTestView()

view.widthAnchor.constraint(equalToConstant: 300).isActive = true
view.heightAnchor.constraint(equalToConstant: 300).isActive = true

let eventGenerator = try EventGenerator(view: view)
try eventGenerator.waitUntilHittable(timeout: 1)

try eventGenerator.fingerDown(at: view.frame.center.offset(x: -50, y: 0))
try eventGenerator.fingerMove(to: view.frame.center.offset(x: 50, y: 0), duration: 0.3)
try eventGenerator.fingerUp()

let endPoint = view.touches.first!.location(in: view)
XCTAssertEqual(endPoint, CGPoint(x: 200, y: 150), accuracy: 0.001)
}

}
13 changes: 13 additions & 0 deletions Tests/HammerTests/Utilities/TouchTestView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import UIKit

final class TouchTestView: UIView {

var touches: Set<UITouch> = .init()

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)

touches.forEach { self.touches.insert($0) }
}

}