Skip to content

Commit

Permalink
Fix FingerUp causing UITouch to end in start location (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsoojin authored Dec 11, 2021
1 parent dd19bd9 commit 6006079
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
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) }
}

}

0 comments on commit 6006079

Please sign in to comment.