butterknife icon indicating copy to clipboard operation
butterknife copied to clipboard

Allow @OnTouch receive not only method but instance of View.OnTouchListener

Open snorkel123 opened this issue 6 years ago • 2 comments

@OnTouch is sometimes used to implement dragging.

Dragging in turn requires storing dX and dY variables in MotionEvent.ACTION_DOWN as fields.

This breaks encapsulation a little as dX, dY are needed for dragging only, outer class does not need them.

Of course, one can save them via View.setTag(), but this solution is a bit obscure.

So proposal is to allow @OnTouch take not only methods but instances of View.OnTouchListener.

Current state

 private float dX;
 private float dY;

    @OnTouch(R.id.corner_point_top_left)  
    boolean onPointTouch(View cornerPoint, MotionEvent event) {
        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                dX = cornerPoint.getX() - event.getRawX();
                dY = cornerPoint.getY() - event.getRawY();
                return true;

            case MotionEvent.ACTION_MOVE:
                cornerPoint.animate()
                        .x(event.getRawX() + dX)
                        .y(event.getRawY() + dY)
                        .setDuration(0)
                        .start();

                return true;

            default:
                return false;
        }
    }

Proposed state

@OnTouch(R.id.corner_point_top_left)  
View.OnTouchListener listener = new View.OnTouchListener() {

        private float dX;
        private float dY;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // same code as in example above
        }
    };

P.S. Thanks for great library!

snorkel123 avatar Apr 01 '18 11:04 snorkel123

This seems fine, yes.

On Sun, Apr 1, 2018, 7:02 AM Alex Shafir [email protected] wrote:

@OnTouch is sometimes used to implement dragging.

Dragging in turn requires storing dX and dY variables in MotionEvent.ACTION_DOWN as fields.

This breaks encapsulation a little as dX, dY are needed for dragging only, outer class does not need them.

Of course, one can save them via View.setTag(), but this solution is a bit obscure.

So proposal is to allow @OnTouch take not only methods but instances of View.OnTouchListener.

Current state

private float dX; private float dY;

@OnTouch(R.id.corner_point_top_left)
boolean onPointTouch(View cornerPoint, MotionEvent event) {
    switch (event.getAction()) {

        case MotionEvent.ACTION_DOWN:

            dX = cornerPoint.getX() - event.getRawX();
            dY = cornerPoint.getY() - event.getRawY();
            return true;

        case MotionEvent.ACTION_MOVE:
            cornerPoint.animate()
                    .x(event.getRawX() + dX)
                    .y(event.getRawY() + dY)
                    .setDuration(0)
                    .start();

            return true;

        default:
            return false;
    }
}

Proposed state

@OnTouch(R.id.corner_point_top_left) View.OnTouchListener listener = new View.OnTouchListener() {

    private float dX;
    private float dY;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // same code as in example above
    }
};

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/JakeWharton/butterknife/issues/1237, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEeFS15zh_5ddATBR1dFndB1JXTBGks5tkLO9gaJpZM4TCyu8 .

JakeWharton avatar Apr 01 '18 15:04 JakeWharton

Any traction here?

snorkel123 avatar May 14 '19 09:05 snorkel123