将子视图内容添加为 ScrollView

时间:2021-06-08 14:04:12

标签: c# xamarin.forms xamarin.ios absolutelayout

说明

在 Xamarin.Forms iOS 中将子视图内容添加为 ScrollView 时,在平移 y 位置后,AbsoluteLayout 子布局边界未正确更新。

代码

[XAML]

    <AbsoluteLayout AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="SizeProportional"
                    HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="LightBlue">
    <local:CustomView x:Name="viewcustom" AbsoluteLayout.LayoutFlags="SizeProportional" 
                    AbsoluteLayout.LayoutBounds="0,0,1,1" BackgroundColor="LightGray">
            <ScrollView x:Name="scrollView" >
            <StackLayout BackgroundColor="Red">
                <Button Margin="10" Text="Button 1" />
                <Button Margin="10" Text="Button 2" />
                <Button Margin="10" Text="Button 3"/>
                <Button Margin="10" Text="Button 4"/>
                <Button Margin="10" Text="Button 5"/>
                <Button Margin="10" Text="Button 6"/>
                <Button Margin="10" Text="Button 7"/>
                <Button Margin="10" Text="Button 8"/>
                <Button Margin="10" Text="Button 9"/>
                <Button Margin="10" Text="Button 10"/>
                <Button Margin="10" Text="Button 11"/>
                <Button Margin="10" Text="Button 12"/>
                <Button Margin="10" Text="Button 13"/>
                <Button Margin="10" Text="Button 14"/>
                <Button Margin="10" Text="Button 15"/>
            </StackLayout>
        </ScrollView>
 </local:CustomView>
    </AbsoluteLayout>

[CustomViewRenderer.cs]

    private void OnPanning()
    {
        var currentY = this.panGesture.TranslationInView(this).Y;
        Double screenHeigth = DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Density;

        switch (this.panGesture.State)
        {
            case UIGestureRecognizerState.Changed:
                DependencyService.Get<IAnimate>().TranslateYTo(this.view, this.prevTranslatedY, currentY, 300);
                UpdateHeight(currentY, 0);
                break;

            case UIGestureRecognizerState.Ended:
                ...
                DependencyService.Get<IAnimate>().TranslateYTo(this.view, this.prevTranslatedY, currentY, 300);
                UpdateHeight(currentY, 0);
                ...
                break;
        }
    }
    private void UpdateHeight(double y, double viewY) //&& this.backdrop.IsBackLayerRevealed && !isPanningEnabled
    {
        Double screenHeigth = DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Density;
        var heightPercentage = (screenHeigth - y) / screenHeigth;
        Xamarin.Forms.AbsoluteLayout.SetLayoutBounds(view, new Xamarin.Forms.Rectangle(0, viewY, 1, heightPercentage));
    }

重现步骤

  1. 选择按钮 1,然后向下拖动。
  2. 然后选择相同的按钮并向上拖动。

预期行为

鼠标点拖动视图

实际行为

由于边界未正确更新,视图无法用鼠标拖动。

基本信息

VS 2019 测试设备:iPhone模拟器 XF 版本:4.8

输出视频

OutputVideo.zip

示例链接

AbsoluteLayoutContentScrollDemo.zip

0 个答案:

没有答案