Xamarin Forms、ScrollView ScrollToAsync 速度

问题描述 投票:0回答:4

有人知道如何在 Xamarin.Forms.ScrollView 控件的 ScrollToAsync 方法中更改滚动动画的速度吗?

我正在使用 Xamarin Forms 开发 Android 应用程序。 谢谢

android xamarin.forms scrollview renderer
4个回答
10
投票

不幸的是,在撰写本文时,ScrollToAsync 速度被硬编码为 1000 毫秒(至少对于 Android)。

我可以通过自己制作滚动动画来解决这个问题:

Point point = scrollView.GetScrollPositionForElement(targetElement, ScrollToPosition.Start);

var animation = new Animation(
    callback: y => scrollView.ScrollToAsync(point.X, y, animated: false),
    start: scrollView.ScrollY,
    end: point.Y - 6);
animation.Commit(
    owner: this,
    name: "Scroll",
    length: 300,
    easing: Easing.CubicIn);

还有这里是动画的文档


4
投票

修改自 Taylor Lafrinere 的答案,这里是与 horizontal 滚动动画相同的片段:

Point point = scrollView.GetScrollPositionForElement(screenContent, ScrollToPosition.Start);

// insert fix for iOS jumping here

var animation = new Animation(
    callback: x => scrollView.ScrollToAsync(x, point.Y, animated: false),
    start: scrollView.ScrollX,
    end: point.X);

animation.Commit(
    owner: this,
    name: "Scroll",
    length: 300,
    easing: Easing.CubicIn);

值得指出的是,在 iOS 上,此代码也将视图移至顶部。一些你不想要的东西。我相信原因是 iOS 将动画的输入理解为滚动视图的下边缘,而 Android 将其理解为滚动视图的 upper 边缘。
为了避免这种跳转,请在 iOS 上将

point.Y
设置为 0:

// iOS seems to understand the input for the animation as the lower edge of the scrollview
// Android the upper edge.
if (Xamarin.Forms.Device.RuntimePlatform == Xamarin.Forms.Device.iOS) 
   {
       point.Y = 0;
   }

我仍然认为这个答案应该是一个编辑,但由于它被拒绝为“应该是一个答案或评论”并且它肯定不能是一个评论,所以这里它是一个答案。目的是为了更清楚地表明泰勒的答案涵盖了垂直动画,并展示了如何制作水平动画。


0
投票

为了模拟 AsyncToScroll 速度的增加,我使用了 ScrollView 的 FadeTo 方法,并将 AsyncToScroll 的参数“animated”设置为 false。 (我的示例是水平滚动视图)

await MyScrollView.FadeTo(1, 200, Easing.CubicIn);
await MyScrollView.ScrollToAsync(_newScrollX_value, 0, false);

0
投票

这就是我最终所做的,添加垂直也很容易。这是非常顺利和缓慢的。我将此嵌入到与多个滚动视图一起使用的行为中。

bool directionRight = true;
public void DoAnimate()
{
Dispatcher.DispatchDelayed(TimeSpan.FromSeconds(1), () =>
{
    Point startPoint = ScrollView.GetScrollPositionForElement(ScrollTarget, directionRight ? ScrollToPosition.Start : ScrollToPosition.End);
    Point endPoint = ScrollView.GetScrollPositionForElement(ScrollTarget, directionRight ? ScrollToPosition.End : ScrollToPosition.Start);
    var animation = new Animation(
        callback: x => ScrollView.ScrollToAsync(x++, 0, animated: false),
        start: startPoint.X,
        end: endPoint.X);
    animation.Commit(
        owner: AnimationContentOwner,
        name: "Scroll",
        length: 30000,
        easing: Easing.Linear,
        finished: ReverseAnimation);
    });
}

AnimationContentOwner - 页面或控件 ScrollTarget - 作为滚动视图子级的控件。在我的例子中,框架控件的大小是滚动视图的 5 倍。 DirectionRight - 向右滚动

我的从头到尾来回动画

© www.soinside.com 2019 - 2024. All rights reserved.