UIScrollView with touchsMoved not called

I have a UIScrollView containing a UIImageView. UIScrollView allows you to scale and pan through UIImageView.

The problem is that I would like to know the movement of the fingers every time, and I'm trying to catch an event using the touchesMoved method. But it does not work, despite the touches of Began and touchesEnded, which are called correctly.

In fact, touchsMoved is called if the movement of the finger is really small, and UIScrollView does not trigger panning. At the moment, UIScrollView starts moving, the event ceases to be triggered.

Does anyone know what the problem is and how to fix it? I thought maybe the UIImageView inside would catch an event or something like that.

+2
source share
4 answers

Actually, this is really a problem, because the UIScrollView events " are " TouchesMoved (even if they distribute the first few).

So, I just came up with an approach to get events directly from UIWindow . This is probably not the best approach in terms of application structure, but in some user situation (which was exactly what I needed) in order.

(Examples are given in MonoTouch C #).

UIWindow ( , UIWindow MyWindow - ( MvvmCross), appDelegate - google/stack):

public class MyWindow : UIWindow
{
    public MyWindow(RectangleF bounds) : base(bounds)
    {

    }

    public override void SendEvent(UIEvent evt)
    {
        if (evt.Type == UIEventType.Touches) {
            var el = (UITouch)evt.AllTouches.AnyObject;

            if (el.Phase == UITouchPhase.Began)
            {
                if(OnTouchBegan != null)
                    OnTouchBegan(el.View, new TouchCommandArgs(evt.AllTouches, evt));
            }

            if (el.Phase == UITouchPhase.Moved)
            {
                if(OnTouchMoved != null)
                    OnTouchMoved(el.View, new TouchCommandArgs(evt.AllTouches, evt));
            }

            if (el.Phase == UITouchPhase.Ended)
            {
                if(OnTouchEnd != null)
                    OnTouchEnd(el.View, new TouchCommandArgs(evt.AllTouches, evt));
            }

            if (el.Phase == UITouchPhase.Cancelled)
            {
                if(OnTouchCancel != null)
                    OnTouchCancel(el.View, new TouchCommandArgs(evt.AllTouches, evt));
            }

        } else
            MvxTrace.Trace (evt.Type == null ?  "-" : evt.ToString ());
        base.SendEvent(evt);
    }

    public event TouchCommand OnTouchBegan;
    public event TouchCommand OnTouchEnd;
    public event TouchCommand OnTouchCancel;
    public event TouchCommand OnTouchMoved;

}

public class TouchCommandArgs : EventArgs
{
    public NSSet Touches { get; set; }
    public UIEvent Evt { get; set; }

    public TouchCommandArgs(NSSet touches, UIEvent evt)
    {
        Touches = touches;
        Evt = evt;
    }
}

:

var window = (MyWindow) UIApplication.SharedApplication.KeyWindow;
window.OnTouchBegan += view_OnTouchBegan;
window.OnTouchMoved += view_OnTouchMoved;
window.OnTouchCancel += view_OnTouchCancel;
window.OnTouchEnd += view_OnTouchEnd;

( -):

void view_OnTouchBegan(object sender, TouchCommandArgs args)
{
    // do your logic
}

( : UIScrollView), , , , , , ( , "Handled" ).

+1

UIScrollView touchhesBegan: :

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // If not dragging, send event to next responder
    if (!self.dragging)
    { 
        [self.nextResponder touchesBegan: touches withEvent:event]; 
    }
    else
    {
        [super touchesEnded: touches withEvent: event];
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // If not dragging, send event to next responder
    if(!self.dragging)
    { 
        [self.nextResponder touchesBegan: touches withEvent:event]; 
    }
    else
    {
        [super touchesEnded: touches withEvent: event];
    }
 }

 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    // If not dragging, send event to next responder
    if (!self.dragging)
    { 
        [self.nextResponder touchesBegan: touches withEvent:event]; 
    }
    else
    {
        [super touchesEnded: touches withEvent: event];
    }
}
0

scrollview canCancelContentTouches = NO .

0

UIScrollViewDelegate .

- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
- (void)scrollViewDidZoom:(UIScrollView *)scrollView;

zoomScale UIScrollView touchesBegan: & (touchesMoved: or touchesCancelled:) events.

0

Source: https://habr.com/ru/post/1728561/


All Articles