Skip to content
This repository was archived by the owner on Feb 26, 2021. It is now read-only.
Open
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
44 changes: 37 additions & 7 deletions Sources/ObjC/UBSignatureDrawingViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,44 @@ - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];

[self _updateModelWithTouches:touches endContinuousLine:YES];
[self _updateModelWithTouches:touches event:event endContinuousLine:YES];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];

[self _updateModelWithTouches:touches endContinuousLine:NO];
[self _updateModelWithTouches:touches event:event endContinuousLine:NO];
}

#pragma mark - Private

- (void)_updateModelWithTouches:(NSSet<UITouch *> *)touches endContinuousLine:(BOOL)endContinuousLine
- (void)_updateModelWithTouches:(NSSet<UITouch *> *)touches event:(UIEvent *)event endContinuousLine:(BOOL)endContinuousLine
{
CGPoint touchPoint = [self.class _touchPointFromTouches:touches];
NSMutableSet<UITouch *> *const coalescedTouches = [NSMutableSet new];
for (UITouch *touch in touches) {
// Get high fidelity (i.e. paired Apple stylus) touches on devices that support it.
// https://apple.co/2E32vNk
[coalescedTouches addObjectsFromArray:[event coalescedTouchesForTouch:touch]];
}
if (coalescedTouches.count > 0) {
static NSArray<NSSortDescriptor *> *descriptors = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
descriptors = @[[NSSortDescriptor sortDescriptorWithKey:NSStringFromSelector(@selector(timestamp)) ascending:YES]];
});
NSArray<UITouch *> *const sortedCoalescedTouches = [coalescedTouches sortedArrayUsingDescriptors:descriptors];
[sortedCoalescedTouches enumerateObjectsUsingBlock:^(UITouch * _Nonnull touch, NSUInteger idx, BOOL * _Nonnull stop) {
[self _updateModelWithTouch:touch usePreciseLocation:YES endContinuousLine:endContinuousLine && idx == sortedCoalescedTouches.count - 1];
}];
} else {
[self _updateModelWithTouch:[touches anyObject] usePreciseLocation:NO endContinuousLine:endContinuousLine];
}
}

- (void)_updateModelWithTouch:(UITouch *)touch usePreciseLocation:(BOOL)usePreciseLocation endContinuousLine:(BOOL)endContinuousLine
{
CGPoint touchPoint = [self.class _touchPointFromTouch:touch usePreciseLocation:usePreciseLocation];

if (endContinuousLine) {
[self.model asyncEndContinuousLine];
Expand All @@ -193,11 +216,18 @@ - (void)_updateViewFromModel

#pragma mark - Helpers

+ (CGPoint)_touchPointFromTouches:(NSSet<UITouch *> *)touches
+ (CGPoint)_touchPointFromTouch:(UITouch *)touch usePreciseLocation:(BOOL)usePreciseLocation
{
UITouch *touch = [touches anyObject];
CGPoint point;
if (usePreciseLocation) {
// Use precise location for touches captured with paired Apple styluses.
// https://apple.co/2GzmDrX
point = [touch preciseLocationInView:touch.view];
} else {
point = [touch locationInView:touch.view];
}

return [touch locationInView:touch.view];
return point;
}

@end