To handle native touch events on iOS, we are going to build a VisualElementRenderer
. These work similar to CustomRenderers, but instead of rendering and replacing the entire control, we are able to render specific attributes, so we are able to attach native attributes to a Xamarin.Forms
view.
Let's start with adding a new folder inside the Renderers
folder called FocusView
. Add in a new file called FocusViewRendererTouchAttribute.cs
and implement the following:
public class FocusViewRendererTouchAttribute : VisualElementRenderer<FocusView> { public override void TouchesBegan (NSSet touches, UIEvent evt) { base.TouchesBegan (touches, evt); FocusView focusView = ((FocusView)this.Element); UITouch touch = touches.AnyObject as UITouch; if (touch != null) { var posc = touch.LocationInView (touch.View); focusView.NotifyFocus (new Xamarin.Forms.Point(posc.X, posc.Y)); } } }
Don't forget to add the assembly line above the namespace like the following:
[assembly: Xamarin.Forms.ExportRendererAttribute (typeof(Camera.Controls.FocusView), typeof(Camera.iOS.Renderers.FocusView.FocusViewRendererTouchAttribute))]
When the element is rendered, we will now have the access to the TouchesBegan
override. Inside this function, we have access to the render object (FocusView
), where we can call the NotifyFocus
function and pass the current touch (x, y) coordinate back to the FocusView
.
That's all for our FocusView
renderers. Let's now move on to the CustomImageRenderer
so that we can apply color tinting to an image.
18.117.182.179