How to programmatically trigger the touch event in android?

// Obtain MotionEvent object long downTime = SystemClock.uptimeMillis(); long eventTime = SystemClock.uptimeMillis() + 100; float x = 0.0f; float y = 0.0f; // List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState() int metaState = 0; MotionEvent motionEvent = MotionEvent.obtain( downTime, eventTime, MotionEvent.ACTION_UP, x, y, metaState ); // Dispatch touch event to view view.dispatchTouchEvent(motionEvent);

In iOS, how to drag down to dismiss a modal?

I just created a tutorial for interactively dragging down a modal to dismiss it. http://www.thorntech.com/2016/02/ios-tutorial-close-modal-dragging/ I found this topic to be confusing at first, so the tutorial builds this out step-by-step. If you just want to run the code yourself, this is the repo: https://github.com/ThornTechPublic/InteractiveModal This is the approach I used: View Controller You override …

Read more

How to simulate a touch event in Android?

Valentin Rocher’s method works if you’ve extended your view, but if you’re using an event listener, use this: view.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { Toast toast = Toast.makeText( getApplicationContext(), “View touched”, Toast.LENGTH_LONG ); toast.show(); return true; } }); // Obtain MotionEvent object long downTime = SystemClock.uptimeMillis(); long eventTime = SystemClock.uptimeMillis() + …

Read more

What is the difference between Pan and Swipe in iOS?

By definition, a swipe gesture is necessarily also a pan gesture — both involve translational movement of touch points. The difference is in the recognizer semantics: a pan recognizer looks for the beginning of translational movement and continues to report movement in any direction over time, while a swipe recognizer makes an instantaneous decision as …

Read more

Draw on HTML5 Canvas using a mouse

Here is a working sample. <html> <script type=”text/javascript”> var canvas, ctx, flag = false, prevX = 0, currX = 0, prevY = 0, currY = 0, dot_flag = false; var x = “black”, y = 2; function init() { canvas = document.getElementById(‘can’); ctx = canvas.getContext(“2d”); w = canvas.width; h = canvas.height; canvas.addEventListener(“mousemove”, function (e) { …

Read more

Long press on UITableView

First add the long press gesture recognizer to the table view: UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)]; lpgr.minimumPressDuration = 2.0; //seconds lpgr.delegate = self; [self.myTableView addGestureRecognizer:lpgr]; [lpgr release]; Then in the gesture handler: -(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer { CGPoint p = [gestureRecognizer locationInView:self.myTableView]; NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:p]; if (indexPath == nil) { NSLog(@”long press on …

Read more

UILongPressGestureRecognizer gets called twice when pressing down

UILongPressGestureRecognizer is a continuous event recognizer. You have to look at the state to see if this is the start, middle or end of the event and act accordingly. i.e. you can throw away all events after the start, or only look at movement as you need. From the Class Reference: Long-press gestures are continuous. …

Read more

Android: How to handle right to left swipe gestures

OnSwipeTouchListener.java: import android.content.Context; import android.view.GestureDetector; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; public class OnSwipeTouchListener implements OnTouchListener { private final GestureDetector gestureDetector; public OnSwipeTouchListener (Context ctx){ gestureDetector = new GestureDetector(ctx, new GestureListener()); } @Override public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); } private final class GestureListener extends SimpleOnGestureListener { private static final …

Read more

Fling gesture detection on grid layout

Thanks to Code Shogun, whose code I adapted to my situation. Let your activity implementOnClickListener as usual: public class SelectFilterActivity extends Activity implements OnClickListener { private static final int SWIPE_MIN_DISTANCE = 120; private static final int SWIPE_MAX_OFF_PATH = 250; private static final int SWIPE_THRESHOLD_VELOCITY = 200; private GestureDetector gestureDetector; View.OnTouchListener gestureListener; @Override protected void onCreate(Bundle …

Read more