Last active
June 26, 2021 08:31
-
-
Save FStranieri/c97c8738079862a2e8f351b7cf709462 to your computer and use it in GitHub Desktop.
Harmony OS - Java SwipeGestureManager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fs.uiapplication; | |
import com.fs.uiapplication.slice.*; | |
import ohos.aafwk.ability.Ability; | |
import ohos.aafwk.content.Intent; | |
import ohos.multimodalinput.event.TouchEvent; | |
public class MainAbility extends Ability implements SwipeListener{ | |
private SwipeManager swipeManager = new SwipeManager(this); | |
@Override | |
public void onStart(Intent intent) { | |
super.onStart(intent); | |
super.setMainRoute(MainAbilitySlice.class.getName()); | |
setSwipeToDismiss(false); | |
} | |
@Override | |
protected boolean onTouchEvent(TouchEvent event) { | |
this.swipeManager.manageSwipe(event); | |
return super.onTouchEvent(event); | |
} | |
@Override | |
public void onSwipeLeft() { | |
} | |
@Override | |
public void onSwipeRight() { | |
} | |
@Override | |
public void onSwipeTop() { | |
} | |
@Override | |
public void onSwipeBottom() { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fs.uiapplication; | |
import ohos.multimodalinput.event.MmiPoint; | |
import ohos.multimodalinput.event.TouchEvent; | |
public class SwipeManager { | |
private final int LEFT = 150; | |
private final int RIGHT = 300; | |
private final int VERTICAL_THRESHOLD = 200; | |
private int LAST_DRAG = -1; | |
private final int D_LEFT = 0; | |
private final int D_RIGHT = 1; | |
private final int D_TOP = 2; | |
private final int D_BOTTOM = 3; | |
private SwipeListener listener = null; | |
public SwipeManager(SwipeListener listener) { | |
this.listener = listener; | |
} | |
public void manageSwipe(TouchEvent event) { | |
if (event.getPhase() == TouchEvent.PHASE_MOVE) { | |
MmiPoint point = event.getPointerScreenPosition(event.getIndex()); | |
if (point.getX() >= RIGHT) { | |
LAST_DRAG = D_RIGHT; | |
} else if (point.getX() <= LEFT) { | |
LAST_DRAG = D_LEFT; | |
} else if (point.getY() >= VERTICAL_THRESHOLD) { | |
LAST_DRAG = D_BOTTOM; | |
} else { | |
LAST_DRAG = D_TOP; | |
} | |
} else if (event.getPhase() == TouchEvent.PHASE_COMPLETED) { | |
switch (LAST_DRAG){ | |
case D_LEFT: | |
this.listener.onSwipeLeft(); | |
break; | |
case D_RIGHT: | |
this.listener.onSwipeRight(); | |
break; | |
case D_TOP: | |
this.listener.onSwipeTop(); | |
break; | |
case D_BOTTOM: | |
this.listener.onSwipeBottom(); | |
break; | |
} | |
LAST_DRAG = -1; | |
} | |
} | |
} | |
interface SwipeListener { | |
void onSwipeLeft(); | |
void onSwipeRight(); | |
void onSwipeTop(); | |
void onSwipeBottom(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment