xamarin / XamarinCommunityToolkit

The Xamarin Community Toolkit is a collection of Animations, Behaviors, Converters, and Effects for mobile development with Xamarin.Forms. It simplifies and demonstrates common developer tasks building iOS, Android, and UWP apps with Xamarin.Forms.
MIT License
1.58k stars 471 forks source link

Expose touch events in TouchEffect #1372

Closed follesoe closed 3 years ago

follesoe commented 3 years ago

Summary

Today the TouchEffect provide several high-level effects and properties such as animations, hover effects, etc. However, there is no way to get access cross-platform touch events for situations where you wish to write your own touch-related logic (such as custom drag and drop etc).

There are several NuGet packets related to Xamarin.Forms touch effects, where where AiForms.Effects is one example.

From Microsoft there are documentation and examples such Invoking Events from Effects explaining how to create a Touch effect yourself.

API Changes

I suggest the TouchEffect gets expanded with new events such as:

Each providing event arguments with information about X and Y position of the touch event.

Intended Use Case

The intended use case is to provide a cross platform way to write code responding to touch event. My particular use case is for a picture-in-picture video player, where one smaller video is played on top of a full size video view. The smaller view can be moved using touch to select which corner of the screen to snap the window to.

In order to implement such logic I need to subscribe to TouchBegin and TouchMove and TouchEnd events.

Who Will Do The Work?

AndreiMisiukevich commented 3 years ago

@follesoe frankly speaking it sounds like you need PanGestureRecognizer https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/gestures/pan

follesoe commented 3 years ago

Was not aware of the PanGestureRecognizer, @AndreiMisiukevich. Just tried to refactor my code to use this gesture recognizer, and it almost works as expected. However, I can't figure out how to determine the initial touch location, so that I can offset the e.TotalX and e.TotalY to be relative to the center of the element being panned.

follesoe commented 3 years ago

Never mind, my logic was flawed, and I was able to reproduce the behavior I wanted using the existing gesture recognizer. However, I still have a feeling something such as the TouchEffect would be of value, to have easy cross-platform access to touch events at a slightly lower abstraction level (i.e. the touch events, not as gestures).

But for my particular use case the PanGestureRecognizer is a better solution allowing me to remove the dependency on AiForms, so thanks for the tip 👍

AndreiMisiukevich commented 3 years ago

@follesoe you are welcome)

AndreiMisiukevich commented 3 years ago

@follesoe TouchEffect allows handling touch state now (Bindable property or event)

Works in OneWayToSource mode

<ContentView xct:TouchEffect.State={Binding CurrentState} />

Or event

<ContentView>
  <ContentVIew.Effects>
    <xct:TouchEffect StateChanged="OnStateChanged" />
  </ContentView.Effects />
</ContentView>

Also, you can check the status of touch (Started, Completed, Canceled). The same for hover etc. You can check exact properties :)

So only TouchMove and finger coordinates are not available for now, but it's not the goal of TouchEffect, I think. I propose to use PanGesture for it.

If you think that those particular features are important, please open a separate ticket (cuz this one contains too many requests at the same place and part of the are addressed, I hope :))