Stop ScrollView scrolling from child elements (works in Android but not iOS)

RobotAppDevRobotAppDev CAMember ✭✭

Hi - i've seen this discussed a lot with Android, and with plenty of examples it was an easy fix. The ScrollView has a child with a custom renderer, in which i capture Touch events, like a joystick pad.

In Android i use this.Parent.RequestDisallowInterceptTouchEvent(true) successfully, as such...

void progress_Touch(object sender, Android.Views.View.TouchEventArgs e) {

  switch (e.Event.Action) {

    case MotionEventActions.Down:
      this.Parent.RequestDisallowInterceptTouchEvent(true);
      Element.BindableTouchDown((int)e.Event.GetX(), (int)e.Event.GetY());
      break;

    case MotionEventActions.Move:
      Element.BindableTouchMove((int)e.Event.GetX(), (int)e.Event.GetY());
      break;

    case MotionEventActions.Up:
    case MotionEventActions.Cancel:
      Element.BindableTouchUp((int)e.Event.GetX(), (int)e.Event.GetY());
      this.Parent.RequestDisallowInterceptTouchEvent(false);
      break;
  }
}

However, in iOS i tried a bunch of different things without success. Such as...

void setParentTouches(bool enabled) {

  UIView t = this.Superview;
  while (t != null) {

    if (t.GetType() == typeof(ScrollViewRenderer))
      t.UserInteractionEnabled = enabled;

    t = t.Superview;
  }
}

public override void TouchesBegan(Foundation.NSSet touches, UIEvent evt) {

  setParentTouches(false);

  var touch = touches.AnyObject as UITouch;

  Element.BindableTouchDown((int)touch.LocationInView(this).X, (int)touch.LocationInView(this).Y);

  base.TouchesBegan(touches, evt);
}

public override void TouchesMoved(Foundation.NSSet touches, UIKit.UIEvent evt) {

  var touch = touches.AnyObject as UITouch;

  Element.BindableTouchMove((int)touch.LocationInView(this).X, (int)touch.LocationInView(this).Y);

  base.TouchesMoved(touches, evt);
}

public override void TouchesEnded(Foundation.NSSet touches, UIKit.UIEvent evt) {

  setParentTouches(true);

  var touch = touches.AnyObject as UITouch;

  Element.BindableTouchUp((int)touch.LocationInView(this).X, (int)touch.LocationInView(this).Y);

  base.TouchesEnded(touches, evt);
}

public override void TouchesCancelled(Foundation.NSSet touches, UIEvent evt) {

  setParentTouches(true);

  var touch = touches.AnyObject as UITouch;

  Element.BindableTouchUp((int)touch.LocationInView(this).X, (int)touch.LocationInView(this).Y);

  base.TouchesCancelled(touches, evt);
}

Best Answer

Answers

  • DeepakDeepsDeepakDeeps INMember ✭✭✭

    Please help me how to implement this one and what are the classes need to be inherit.

    i don't know how to implement this.

  • SzbestaSzbesta Member ✭✭✭

    @DeepakDeeps
    You need to implement this in a CustomRenderer which is a class you declare on your own.
    Info on how to create a CustomRenderer is found here https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/ everything you need to know should be covered there.

  • GauravKatdareGauravKatdare USMember ✭✭

    Hi @Szbesta , @RobotAppDev ,

    I am trying to use this functionality using custom renderer and in Android it is woring fine but in iOS firstly when I override above given methods they are not executing. therefore I used Control.AddGestureRecognizer(new iOSTouchListener());

    iOSTouchListener is a class which extends UIGestureRecognizer and all the above code is written inside it.
    but it is not working as expected.
    upon hovering on editor it is not scrolling. but when editor is focused after that it starts scrolling. in both cases page scrolling is disabled.
    if touchend method is executed at that time page scrolling is restored. but touchcancelled method is never executed.

    And you have mentioned Element.BindableTouchDown what is that.

    Thanks in Advance.

Sign In or Register to comment.