如何拦截UIScrollView滚动事件?

时间:2017-02-14 13:02:31

标签: ios xamarin uiscrollview xamarin.ios

在Xamarin! Scrolled被触发,但它不包含X和Y信息。我需要知道什么时候滚动视图来调整一些视图,使它们看起来不动。仅当滚动结束时才会触发DraggingEnded,而且为时已晚。如何在变更后立即收到变更?

2 个答案:

答案 0 :(得分:0)

UIScrollView子类中,覆盖ContentOffset属性并监视set,当用户滚动/平移时,您将获得视图的新X / Y.

public override CoreGraphics.CGPoint ContentOffset
{
    get
    {
        return base.ContentOffset;
    }
    set
    {
        Console.WriteLine($"{value.X}:{value.Y}");
        base.ContentOffset = value;
    }
}

输出:

39.0704398717043:41.4920965388485
35.3280093038005:36.4695722954986
31.736242067681:31.6492455873442
28.1199544514634:26.7960113338755
24.5949600095583:22.0652969703334
21.2827079155381:17.6200932075763
17.9479795925504:13.1447252499626

答案 1 :(得分:0)

public class ScrollViewDelegate : UIScrollViewDelegate
{
    public ScrollViewDelegate()
    {
        //...
    }
    public override void DraggingStarted(UIScrollView scrollView)
    {
        System.Diagnostics.Debug.WriteLine("Dragging started offset: " + scrollView.ContentOffset);
    }
}

[assembly: ExportRenderer(typeof(SomeScrollViewControl), typeof(SomeScrollView))]
namespace SomeNamespace
{
    public class SomeScrollView : ViewRenderer<SomeScrollViewControl, SomeScrollView>
    {
        public SomeScrollView()
        {
            //...
        }
        protected override void OnElementChanged(ElementChangedEventArgs<SomeScrollViewControl> e)
        {
            if (e.OldElement == null)
            {
                //Create someScrollView...
                someScrollView.Delegate = new ScrollViewDelegate();
            }
        }
    }
}