Skip to content

Commit

Permalink
Adding support for force touch
Browse files Browse the repository at this point in the history
  • Loading branch information
YoniTsafir committed Jan 27, 2016
1 parent 9361df4 commit ed3ed28
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions sparrow/src/Classes/SPTouch.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ typedef NS_ENUM(NSInteger, SPTouchPhase)
/// The display object at which the touch occurred.
@property (nonatomic, readonly) SPDisplayObject *target;

/// The amount of force that was applied to the touch, as a factor from 0 to 1.
@property (nonatomic, readonly) float *forceFactor;

@end

NS_ASSUME_NONNULL_END
3 changes: 3 additions & 0 deletions sparrow/src/Classes/SPTouch.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ @interface SPTouch ()
@property (nonatomic, assign) SPTouchPhase phase;
@property (nonatomic, strong) SPDisplayObject *target;
@property (nonatomic, assign) size_t touchID;
@property (nonatomic, assign) float forceFactor;

@end

Expand All @@ -41,6 +42,7 @@ @implementation SPTouch
SPTouchPhase _phase;
SPDisplayObject *_target;
size_t _touchID;
float _forceFactor;
}

#pragma mark Initialization
Expand Down Expand Up @@ -140,6 +142,7 @@ - (instancetype)copyWithZone:(NSZone *)zone
clone->_tapCount = _tapCount;
clone->_timestamp = _timestamp;
clone->_target = [_target retain];
clone->_forceFactor = _forceFactor;
return clone;
}

Expand Down
1 change: 1 addition & 0 deletions sparrow/src/Classes/SPTouchProcessor.m
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ - (SPTouch *)createOrUpdateTouch:(SPTouch *)touch
currentTouch.previousGlobalX = touch.previousGlobalX;
currentTouch.previousGlobalY = touch.previousGlobalY;
currentTouch.phase = touch.phase;
currentTouch.forceFactor = touch.forceFactor
currentTouch.timestamp = _elapsedTime;

if (currentTouch.phase == SPTouchPhaseBegan)
Expand Down
1 change: 1 addition & 0 deletions sparrow/src/Classes/SPTouch_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, assign) NSInteger tapCount;
@property (nonatomic, assign) SPTouchPhase phase;
@property (nonatomic, strong, nullable) SPDisplayObject *target;
@property (nonatomic, assign) float forceFactor;

@end

Expand Down
9 changes: 9 additions & 0 deletions sparrow/src/Classes/SPViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,16 @@ - (void)processTouchEvent:(UIEvent *)event
touch.previousGlobalY = previousLocation.y * yConversion;
touch.tapCount = (int)uiTouch.tapCount;
touch.phase = (SPTouchPhase)uiTouch.phase;
#pragma clang diagnostic push
#pragma ide diagnostic ignored "UnavailableInDeploymentTarget"
if ([uiTouch respondsToSelector:@selector(force)] && uiTouch.maximumPossibleForce > 0) {
touch.forceFactor = uiTouch.force / uiTouch.maximumPossibleForce;
} else {
touch.forceFactor = 0;
}
#pragma clang diagnostic pop
touch.touchID = (size_t)uiTouch;

[_touchProcessor enqueueTouch:touch];
}

Expand Down

0 comments on commit ed3ed28

Please sign in to comment.