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

feat(all): Add rotate event #13882

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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 @@ -157,6 +157,7 @@ public class TiC
public static final String EVENT_RESUME = "resume";
public static final String EVENT_RESUMED = "resumed";
public static final String EVENT_RETURN = "return";
public static final String EVENT_ROTATE = "rotate";
public static final String EVENT_ROWS_SELECTED = "rowsselected";
public static final String EVENT_SCROLL = "scroll";
public static final String EVENT_SCROLLEND = "scrollend";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1803,6 +1803,22 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve
int pointersDown = 0;
int touchSlop;

private void doRotationEvent(MotionEvent event)
{
// Calculate the angle between the two fingers
float deltaX = event.getX(0) - event.getX(1);
float deltaY = event.getY(0) - event.getY(1);
double radians = Math.atan(deltaY / deltaX);
double degrees = Math.toDegrees(radians);

if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
KrollDict data = new KrollDict();
data.put(TiC.PROPERTY_ROTATE, degrees);
data.put(TiC.EVENT_PROPERTY_SOURCE, proxy);
fireEvent(TiC.EVENT_ROTATE, data);
}
}

@Override
public boolean onTouch(View view, MotionEvent event)
{
Expand All @@ -1820,6 +1836,10 @@ public boolean onTouch(View view, MotionEvent event)
lastUpEvent.put(TiC.EVENT_PROPERTY_OBSCURED, wasObscured(event));
}

if (proxy != null && proxy.hierarchyHasListener(TiC.EVENT_ROTATE) && event.getPointerCount() == 2) {
doRotationEvent(event);
}

// Do custom "longpress" event tracking. Store motion event data to be used by onLongClick() listener.
// Note: Can't use GestureDetector for this since we would have to handle onDown() to make it work,
// which would prevent view's onClick() and onLongClick() listeners from being called.
Expand Down
12 changes: 12 additions & 0 deletions apidoc/Titanium/UI/View.yml
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,18 @@ events:
may result in an endless loop.
since: "2.0.0"

- name: rotate
summary: Fired when the device detects a two finger rotation.
description: |
This event is fired when doing a two finger rotation and returning the angle.
The event occurs continuously until a finger is lifted again.
platforms: [android, iphone, ipad, macos]
properties:
- name: rotate
summary: Rotation in degrees.
type: Number
since: "12.3.0"
m1ga marked this conversation as resolved.
Show resolved Hide resolved

- name: singletap
summary: Fired when the device detects a single tap against the view.
properties:
Expand Down
8 changes: 5 additions & 3 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiUIView.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ void ModifyScrollViewForKeyboardHeightAndContentHeightWithResponderRect(UIScroll
UITapGestureRecognizer *doubleTapRecognizer;
UITapGestureRecognizer *twoFingerTapRecognizer;
UIPinchGestureRecognizer *pinchRecognizer;
UIRotationGestureRecognizer *rotationRegognizer;
UISwipeGestureRecognizer *leftSwipeRecognizer;
UISwipeGestureRecognizer *rightSwipeRecognizer;
UISwipeGestureRecognizer *upSwipeRecognizer;
Expand All @@ -105,13 +106,13 @@ void ModifyScrollViewForKeyboardHeightAndContentHeightWithResponderRect(UIScroll
- (BOOL)animating;

/**
Provides access to a proxy object of the view.
Provides access to a proxy object of the view.
*/
@property (nonatomic, readwrite, assign) TiProxy *proxy;

/**
Provides access to touch delegate of the view.

Touch delegate is the control that receives all touch events.
*/
@property (nonatomic, readwrite, assign) UIView *touchDelegate;
Expand All @@ -137,6 +138,7 @@ void ModifyScrollViewForKeyboardHeightAndContentHeightWithResponderRect(UIScroll
@property (nonatomic, readonly) UITapGestureRecognizer *doubleTapRecognizer;
@property (nonatomic, readonly) UITapGestureRecognizer *twoFingerTapRecognizer;
@property (nonatomic, readonly) UIPinchGestureRecognizer *pinchRecognizer;
@property (nonatomic, readonly) UIRotationGestureRecognizer *rotationRegognizer;
@property (nonatomic, readonly) UISwipeGestureRecognizer *leftSwipeRecognizer;
@property (nonatomic, readonly) UISwipeGestureRecognizer *rightSwipeRecognizer;
@property (nonatomic, readonly) UILongPressGestureRecognizer *longPressRecognizer;
Expand Down Expand Up @@ -242,7 +244,7 @@ void ModifyScrollViewForKeyboardHeightAndContentHeightWithResponderRect(UIScroll

/**
Returns default enablement for interactions.

Subclasses may override.
@return _YES_ if the control has interactions enabled by default, _NO_ otherwise.
*/
Expand Down
29 changes: 28 additions & 1 deletion iphone/TitaniumKit/TitaniumKit/Sources/API/TiUIView.m
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ - (void)dealloc
[doubleTapRecognizer release];
[twoFingerTapRecognizer release];
[pinchRecognizer release];
[rotationRegognizer release];
[leftSwipeRecognizer release];
[rightSwipeRecognizer release];
[upSwipeRecognizer release];
Expand Down Expand Up @@ -241,6 +242,9 @@ - (void)ensureGestureListeners
if ([(TiViewProxy *)proxy _hasListeners:@"pinch"]) {
[[self gestureRecognizerForEvent:@"pinch"] setEnabled:YES];
}
if ([(TiViewProxy *)proxy _hasListeners:@"rotate"]) {
[[self gestureRecognizerForEvent:@"rotate"] setEnabled:YES];
}
if ([(TiViewProxy *)proxy _hasListeners:@"longpress"]) {
[[self gestureRecognizerForEvent:@"longpress"] setEnabled:YES];
}
Expand All @@ -253,6 +257,7 @@ - (BOOL)proxyHasGestureListeners
[proxy _hasListeners:@"twofingertap"] ||
[proxy _hasListeners:@"swipe"] ||
[proxy _hasListeners:@"pinch"] ||
[proxy _hasListeners:@"rotate"] ||
[proxy _hasListeners:@"longpress"];
}

Expand Down Expand Up @@ -1355,6 +1360,17 @@ - (UIPinchGestureRecognizer *)pinchRecognizer
return pinchRecognizer;
}

- (UIRotationGestureRecognizer *)rotationRegognizer
{
if (rotationRegognizer == nil) {
rotationRegognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(recognizedRotation:)];
[self configureGestureRecognizer:rotationRegognizer];
[self addGestureRecognizer:rotationRegognizer];
}
rotationRegognizer.delegate = self;
return rotationRegognizer;
}

- (UISwipeGestureRecognizer *)leftSwipeRecognizer
{
if (leftSwipeRecognizer == nil) {
Expand Down Expand Up @@ -1433,6 +1449,14 @@ - (void)recognizedTap:(UITapGestureRecognizer *)recognizer
}
}

- (void)recognizedRotation:(UIRotationGestureRecognizer *)recognizer
{
NSDictionary *event = [NSDictionary dictionaryWithObjectsAndKeys:
NUMDOUBLE(radiansToDegrees(recognizer.rotation)), @"rotate",
nil];
[self.proxy fireEvent:@"rotate" withObject:event];
}

- (void)recognizedPinch:(UIPinchGestureRecognizer *)recognizer
{
NSDictionary *event = [NSDictionary dictionaryWithObjectsAndKeys:
Expand Down Expand Up @@ -1686,6 +1710,9 @@ - (UIGestureRecognizer *)gestureRecognizerForEvent:(NSString *)event
if ([event isEqualToString:@"pinch"]) {
return [self pinchRecognizer];
}
if ([event isEqualToString:@"rotate"]) {
return [self rotationRegognizer];
}
if ([event isEqualToString:@"longpress"]) {
return [self longPressRecognizer];
}
Expand Down Expand Up @@ -1741,7 +1768,7 @@ - (void)sanitycheckListeners //TODO: This can be optimized and unwound later.
{
if (listenerArray == nil) {
listenerArray = [[NSArray alloc] initWithObjects:@"singletap",
@"doubletap", @"twofingertap", @"swipe", @"pinch", @"longpress", nil];
@"doubletap", @"twofingertap", @"swipe", @"rotate", @"pinch", @"longpress", nil];
}
for (NSString *eventName in listenerArray) {
if ([proxy _hasListeners:eventName]) {
Expand Down