2017-02-14 163 views
0

在Xamarin! Scrolled被解僱,但它不包含X和Y信息。我需要知道何時滾動視圖以調整某些視圖,以使它們看起來不會移動。只有當滾動結束時纔會觸發DraggingEnded,這太遲了。如何儘快收到更改?如何攔截UIScrollView滾動事件?

回答

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(); 
      } 
     } 
    } 
} 
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