Skip to content
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

Adding support for force touch in SPTouch #51

Merged
merged 2 commits into from
Jan 28, 2016
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
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